first commit
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
package redismodel
|
||||
|
||||
// 认证模块:token 与用户会话的 Redis key。
|
||||
const (
|
||||
KeyAuthAccess = RedisPrefix + "auth:access:%s" // access_token -> user_id(String)
|
||||
KeyAuthRefresh = RedisPrefix + "auth:refresh:%s" // refresh_token -> user_id(String)
|
||||
KeyAuthUserTokens = RedisPrefix + "auth:user:tokens:%d" // user_id -> token 集合(Set,支持多端登录)
|
||||
KeyAuthSessionKey = RedisPrefix + "auth:session_key:%d" // user_id -> session_key(String,虚拟支付 signature 用)
|
||||
)
|
||||
@@ -0,0 +1,4 @@
|
||||
package redismodel
|
||||
|
||||
// 衣柜分享模块:分享访问计数。
|
||||
const KeyClosetShareView = RedisPrefix + "closet:share:view:%s" // share_uuid,衣柜分享访问计数(INCR)
|
||||
@@ -0,0 +1,4 @@
|
||||
package redismodel
|
||||
|
||||
// 收藏模块:按业务类型 + 目标 ID 分桶的收藏集合。
|
||||
const KeyCollectSet = RedisPrefix + "collect:set:%d:%d" // collect_type:target_id,成员为 user_id(Set,1=社区穿搭)
|
||||
@@ -0,0 +1,4 @@
|
||||
package redismodel
|
||||
|
||||
// 星座模块:按星座 + 日期缓存的运势内容。
|
||||
const KeyHoroscope = RedisPrefix + "horoscope:%s:%d" // zodiac:stat_date(YYYYMMDD),星座运势缓存(String/JSON)
|
||||
@@ -0,0 +1,41 @@
|
||||
package redismodel
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/pkg/setting"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// RedisPrefix 是所有 Redis key 的全局命名空间前缀,避免多服务共享 Redis 实例时发生 key 冲突。
|
||||
const RedisPrefix = "ztcloset:"
|
||||
|
||||
// NewRedisClient 根据配置创建 Redis 客户端并验证连接。
|
||||
func NewRedisEngine(s *setting.RedisSettingS) (*redis.Client, error) {
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("%s:%d", s.Host, s.Port),
|
||||
Password: s.Password,
|
||||
DB: s.DB,
|
||||
PoolSize: s.PoolSize,
|
||||
MinIdleConns: s.MinIdleConns,
|
||||
MaxIdleConns: s.MaxIdleConns,
|
||||
MinRetryBackoff: time.Duration(s.MinRetryBackoff) * time.Millisecond,
|
||||
MaxRetryBackoff: time.Duration(s.MaxRetryBackoff) * time.Millisecond,
|
||||
MaxRetries: s.MaxRetries,
|
||||
// 连接/读写/池超时配置单位为秒(与 config.yaml 注释一致)。
|
||||
DialTimeout: time.Duration(s.DialTimeout) * time.Second,
|
||||
ReadTimeout: time.Duration(s.ReadTimeout) * time.Second,
|
||||
WriteTimeout: time.Duration(s.WriteTimeout) * time.Second,
|
||||
PoolTimeout: time.Duration(s.PoolTimeout) * time.Second,
|
||||
})
|
||||
|
||||
if err := client.Ping(context.Background()).Err(); err != nil {
|
||||
return nil, fmt.Errorf("redis ping failed: %w", err)
|
||||
}
|
||||
|
||||
slog.Info("redis connected", "addr", fmt.Sprintf("%s:%d", s.Host, s.Port))
|
||||
return client, nil
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package redismodel
|
||||
|
||||
// 点赞模块:按业务类型 + 目标 ID 分桶的点赞集合。
|
||||
const KeyLikeSet = RedisPrefix + "like:set:%d:%d" // like_type:target_id,成员为 user_id(Set,1=社区穿搭)
|
||||
@@ -0,0 +1,13 @@
|
||||
package redismodel
|
||||
|
||||
// 分布式锁与幂等模块:对齐 mysqlmodel 的 Order、AIRecommend、AIHoroscope、审核等业务。
|
||||
const (
|
||||
KeyLockOrderCreate = RedisPrefix + "lock:order:create:%d" // order_id,下单防重
|
||||
KeyLockPayCallback = RedisPrefix + "lock:pay:callback:%s" // order_no,支付回调幂等
|
||||
KeyLockRefundCallback = RedisPrefix + "lock:refund:callback:%s" // refund_no,退款回调幂等
|
||||
KeyLockAIRecommend = RedisPrefix + "lock:ai:recommend:%d" // user_id,AI推荐防并发
|
||||
KeyLockAIHoroscope = RedisPrefix + "lock:ai:horoscope:%d:%s" // user_id:zodiac,AI星座防并发
|
||||
KeyLockClosetAudit = RedisPrefix + "lock:audit:closet:%d" // target_id,衣柜分享审核防重
|
||||
KeyLockOutfitAudit = RedisPrefix + "lock:audit:outfit:%d" // target_id,穿搭审核防重
|
||||
KeyLockOutfitPublic = RedisPrefix + "lock:outfit:public:%s" // outfit_uuid,穿搭公开/取消公开串行化
|
||||
)
|
||||
@@ -0,0 +1,4 @@
|
||||
package redismodel
|
||||
|
||||
// 通知模块:用户未读消息计数。
|
||||
const KeyNotificationUnread = RedisPrefix + "notification:unread:%d" // user_id,未读消息数(INCR/DECR)
|
||||
@@ -0,0 +1,4 @@
|
||||
package redismodel
|
||||
|
||||
// 穿搭模块:公开穿搭浏览计数。
|
||||
const KeyOutfitView = RedisPrefix + "outfit:view:%d" // outfit_id,公开穿搭浏览计数(INCR)
|
||||
@@ -0,0 +1,9 @@
|
||||
package redismodel
|
||||
|
||||
// 配额模块:对齐 mysqlmodel 的 Quota(总配额)与 UserDailyUsage(每日使用计数)。
|
||||
// 说明:每日使用计数使用 Redis INCR 做当日限流统计,权威值仍以 MySQL 为准。
|
||||
const (
|
||||
KeyQuotaDailyAI = RedisPrefix + "quota:daily:ai:%d:%d" // user_id:date(YYYYMMDD),当日AI推荐次数(INCR)
|
||||
KeyQuotaDailyOutfitShare = RedisPrefix + "quota:daily:outfit_share:%d:%d" // user_id:date(YYYYMMDD),当日社区分享次数(搭配分享,INCR)
|
||||
KeyQuotaDailyClosetShare = RedisPrefix + "quota:daily:closet_share:%d:%d" // user_id:date(YYYYMMDD),当日衣柜分享次数(INCR)
|
||||
)
|
||||
@@ -0,0 +1,4 @@
|
||||
package redismodel
|
||||
|
||||
// 用户模块:用户鉴权核心信息缓存。
|
||||
const KeyUserInfo = RedisPrefix + "user:info:%d" // user_id -> 用户鉴权核心信息(String/JSON,含 Status)
|
||||
Reference in New Issue
Block a user