Enhance AI and Redis integration for smoke logging features
- Added AI configuration options to .env.example and config.go for OpenAI integration. - Implemented Redis caching for session management in main.go and auth middleware. - Updated smoke logging service to support real smoking time (`smoke_at`) and AI advice retrieval. - Enhanced API routes to include endpoints for AI advice and unlock functionality for non-members. - Improved database schema with new tables for AI advice and unlock records. - Expanded documentation to cover new AI features and Redis caching implementation.
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"wx_service/internal/model"
|
||||
)
|
||||
|
||||
type SessionUserCache struct {
|
||||
rc *redis.Client
|
||||
keyPrefix string
|
||||
ttl time.Duration
|
||||
}
|
||||
|
||||
func NewSessionUserCache(rc *redis.Client, keyPrefix string, ttl time.Duration) *SessionUserCache {
|
||||
return &SessionUserCache{
|
||||
rc: rc,
|
||||
keyPrefix: keyPrefix,
|
||||
ttl: ttl,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *SessionUserCache) Get(ctx context.Context, token string) (*model.User, bool, error) {
|
||||
if c == nil || c.rc == nil || token == "" {
|
||||
return nil, false, nil
|
||||
}
|
||||
key := c.sessionKey(token)
|
||||
val, err := c.rc.Get(ctx, key).Bytes()
|
||||
if err != nil {
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return nil, false, nil
|
||||
}
|
||||
return nil, false, fmt.Errorf("redis get: %w", err)
|
||||
}
|
||||
|
||||
var user model.User
|
||||
if err := json.Unmarshal(val, &user); err != nil {
|
||||
_ = c.rc.Del(ctx, key).Err()
|
||||
return nil, false, nil
|
||||
}
|
||||
return &user, true, nil
|
||||
}
|
||||
|
||||
func (c *SessionUserCache) Set(ctx context.Context, token string, user *model.User) error {
|
||||
if c == nil || c.rc == nil || token == "" || user == nil {
|
||||
return nil
|
||||
}
|
||||
key := c.sessionKey(token)
|
||||
body, err := json.Marshal(user)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal user: %w", err)
|
||||
}
|
||||
if err := c.rc.Set(ctx, key, body, c.ttl).Err(); err != nil {
|
||||
return fmt.Errorf("redis set: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *SessionUserCache) sessionKey(token string) string {
|
||||
prefix := c.keyPrefix
|
||||
if prefix != "" && !strings.HasSuffix(prefix, ":") {
|
||||
prefix += ":"
|
||||
}
|
||||
return prefix + "session_user:" + token
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"wx_service/config"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
rc *redis.Client
|
||||
keyPrefix string
|
||||
sessionTTL time.Duration
|
||||
}
|
||||
|
||||
func NewClient(cfg config.RedisConfig) (*Client, error) {
|
||||
if cfg.Addr == "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
rc := redis.NewClient(&redis.Options{
|
||||
Addr: cfg.Addr,
|
||||
Password: cfg.Password,
|
||||
DB: cfg.DB,
|
||||
})
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
if err := rc.Ping(ctx).Err(); err != nil {
|
||||
return nil, fmt.Errorf("ping redis: %w", err)
|
||||
}
|
||||
|
||||
ttlSeconds := cfg.SessionTTLSeconds
|
||||
if ttlSeconds <= 0 {
|
||||
ttlSeconds = 86400
|
||||
}
|
||||
|
||||
return &Client{
|
||||
rc: rc,
|
||||
keyPrefix: cfg.KeyPrefix,
|
||||
sessionTTL: time.Duration(ttlSeconds) * time.Second,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Client) Redis() *redis.Client {
|
||||
return c.rc
|
||||
}
|
||||
|
||||
func (c *Client) KeyPrefix() string {
|
||||
return c.keyPrefix
|
||||
}
|
||||
|
||||
func (c *Client) SessionTTL() time.Duration {
|
||||
return c.sessionTTL
|
||||
}
|
||||
Reference in New Issue
Block a user