package research import ( "context" "encoding/json" "strconv" "github.com/redis/go-redis/v9" ) const ( taskQueueKey = "research:tasks" statusKeyPrefix = "research:status:" ) // redisBackend 同时实现 Queue 与 Cache:任务下发到列表队列、快速状态读自 hash。 // 与 research-worker 的 TASK_QUEUE / TASK_STATUS_PREFIX 约定保持一致。 type redisBackend struct { rdb *redis.Client } func NewRedisBackend(rdb *redis.Client) *redisBackend { return &redisBackend{rdb: rdb} } func (b *redisBackend) Enqueue(ctx context.Context, taskID string) error { msg, _ := json.Marshal(map[string]string{"task_id": taskID}) return b.rdb.LPush(ctx, taskQueueKey, msg).Err() } func (b *redisBackend) GetStatus(ctx context.Context, taskID string) (*CachedStatus, bool) { m, err := b.rdb.HGetAll(ctx, statusKeyPrefix+taskID).Result() if err != nil || len(m) == 0 { return nil, false } progress, _ := strconv.Atoi(m["progress"]) return &CachedStatus{ Status: m["status"], Progress: progress, Message: m["message"], }, true }