Version:0.0.6.dev.260204
feat: 🏗️ 在 sv/dao 层引入 ctx 支持链路追踪与超时控制(预留扩展) - 架构改造:在 sv 与 dao 层统一引入 ctx 🧩 - 为后续链路追踪、超时控制等能力提供支持 ⏱️(未来开发) perf: 🚀 为获取任务类列表接口引入 Redis 缓存并保证一致性 - 获取任务类列表接口新增 Redis 缓存提升访问速度 ⚡ - 通过新增任务类时主动删除缓存,确保主从一致性 ✅ - 防止缓存与数据库列表不一致问题 🛡️
This commit is contained in:
@@ -2,9 +2,12 @@ package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/LoveLosita/smartflow/backend/model"
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
@@ -31,3 +34,34 @@ func (dao *CacheDAO) IsBlacklisted(jti string) (bool, error) {
|
||||
}
|
||||
return result == "1", nil // 在黑名单
|
||||
}
|
||||
|
||||
func (dao *CacheDAO) AddTaskClassList(ctx context.Context, userID int, list *model.UserGetTaskClassesResponse) error {
|
||||
// 1. 定义 Key,使用 userID 隔离不同用户的数据
|
||||
key := fmt.Sprintf("smartflow:task_classes:%d", userID)
|
||||
// 2. 序列化:将结构体转为 []byte
|
||||
data, err := json.Marshal(list)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 3. 存储:设置 30 分钟过期(根据业务灵活调整)
|
||||
return dao.client.Set(ctx, key, data, 30*time.Minute).Err()
|
||||
}
|
||||
|
||||
func (dao *CacheDAO) GetTaskClassList(ctx context.Context, userID int) (model.UserGetTaskClassesResponse, error) {
|
||||
key := fmt.Sprintf("smartflow:task_classes:%d", userID)
|
||||
var resp model.UserGetTaskClassesResponse
|
||||
// 1. 从 Redis 获取字符串
|
||||
val, err := dao.client.Get(ctx, key).Result()
|
||||
if err != nil {
|
||||
// 注意:如果是 redis.Nil,交给 Service 层处理查库逻辑
|
||||
return resp, err
|
||||
}
|
||||
// 2. 反序列化:将 JSON 还原回结构体
|
||||
err = json.Unmarshal([]byte(val), &resp)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (dao *CacheDAO) DeleteTaskClassList(ctx context.Context, userID int) error {
|
||||
key := fmt.Sprintf("smartflow:task_classes:%d", userID)
|
||||
return dao.client.Del(ctx, key).Err()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user