Files
zt-closet-backend/internal/redisdao/horoscope.go
T
2026-09-11 01:41:52 +08:00

38 lines
1.1 KiB
Go

package redisdao
import (
"context"
"fmt"
"time"
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/global"
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/internal/redismodel"
"github.com/redis/go-redis/v9"
)
// HoroscopeSet 缓存星座运势内容(zodiac + 日期维度,data 由上层负责序列化)
func HoroscopeSet(ctx context.Context, zodiac string, statDate int64, data []byte, ttl time.Duration) error {
ctx, span := global.Tracer.Start(ctx, "redisdao.HoroscopeSet")
defer span.End()
key := fmt.Sprintf(redismodel.KeyHoroscope, zodiac, statDate)
if err := global.RedisEngine.Set(ctx, key, data, ttl).Err(); err != nil {
span.RecordError(err)
return err
}
return nil
}
// HoroscopeGet 读取星座运势缓存,未命中返回 redis.Nil
func HoroscopeGet(ctx context.Context, zodiac string, statDate int64) ([]byte, error) {
ctx, span := global.Tracer.Start(ctx, "redisdao.HoroscopeGet")
defer span.End()
key := fmt.Sprintf(redismodel.KeyHoroscope, zodiac, statDate)
data, err := global.RedisEngine.Get(ctx, key).Bytes()
if err != nil && err != redis.Nil {
span.RecordError(err)
}
return data, err
}