first commit
This commit is contained in:
174
pkg/cache/RedisSingleton.go
vendored
Normal file
174
pkg/cache/RedisSingleton.go
vendored
Normal file
@ -0,0 +1,174 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
redis "github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// 公共redis单例
|
||||
var commonInstance *singleton
|
||||
var commonOnce sync.Once
|
||||
|
||||
func CommonClient() *redis.Client {
|
||||
return GetCommonRedisInstance().RedisClient
|
||||
}
|
||||
|
||||
// 获取公用redis连接
|
||||
func GetCommonRedisInstance() *singleton {
|
||||
commonOnce.Do(func() {
|
||||
commonInstance = &singleton{}
|
||||
commonInstance.RedisClient = createCommonmRedisClient()
|
||||
})
|
||||
return commonInstance
|
||||
}
|
||||
|
||||
// 用户redis单例
|
||||
var userInstance *singleton
|
||||
var userOnce sync.Once
|
||||
|
||||
func UserClient() *redis.Client {
|
||||
return GetUserRedisInstance().RedisClient
|
||||
}
|
||||
|
||||
// 获取公用redis连接
|
||||
func GetUserRedisInstance() *singleton {
|
||||
userOnce.Do(func() {
|
||||
userInstance = &singleton{}
|
||||
userInstance.RedisClient = createUserRedisClient()
|
||||
})
|
||||
return userInstance
|
||||
}
|
||||
|
||||
// 用户关系redis单例
|
||||
var relationInstance *singleton
|
||||
var relationOnce sync.Once
|
||||
|
||||
// 获取relation redis连接
|
||||
func GetUserRelationRedisInstance() *singleton {
|
||||
relationOnce.Do(func() {
|
||||
relationInstance = &singleton{}
|
||||
relationInstance.RedisClient = createUserRelationRedisClient()
|
||||
})
|
||||
return relationInstance
|
||||
}
|
||||
|
||||
// 动态redis单例
|
||||
var timelineInstance *singleton
|
||||
var timelineOnce sync.Once
|
||||
|
||||
// 获取动态redis连接
|
||||
func GetTimelineRedisInstance() *singleton {
|
||||
timelineOnce.Do(func() {
|
||||
timelineInstance = &singleton{}
|
||||
timelineInstance.RedisClient = createTimelineRedisClient()
|
||||
})
|
||||
return timelineInstance
|
||||
}
|
||||
|
||||
// 活动redis单例
|
||||
var activityInstance *singleton
|
||||
var activityOnce sync.Once
|
||||
|
||||
func ActivityClient() *redis.Client {
|
||||
return GetActivityRedisInstance().RedisClient
|
||||
}
|
||||
|
||||
// 获取活动redis连接
|
||||
func GetActivityRedisInstance() *singleton {
|
||||
activityOnce.Do(func() {
|
||||
activityInstance = &singleton{}
|
||||
activityInstance.RedisClient = createActivityRedisClient()
|
||||
})
|
||||
return activityInstance
|
||||
}
|
||||
|
||||
// 公会redis单例
|
||||
var guildInstance *singleton
|
||||
var guildOnce sync.Once
|
||||
|
||||
func GuildClient() *redis.Client {
|
||||
return GetGuildRedisInstance().RedisClient
|
||||
}
|
||||
|
||||
// 获取公会redis连接
|
||||
func GetGuildRedisInstance() *singleton {
|
||||
guildOnce.Do(func() {
|
||||
guildInstance = &singleton{}
|
||||
guildInstance.RedisClient = createGuildRedisClient()
|
||||
})
|
||||
return guildInstance
|
||||
}
|
||||
|
||||
// 聊天室redis单例
|
||||
var chatroomInstance *singleton
|
||||
var chatroomOnce sync.Once
|
||||
|
||||
func RoomClient() *redis.Client {
|
||||
return GetChatroomRedisInstance().RedisClient
|
||||
}
|
||||
|
||||
// 获取chatroom redis连接
|
||||
func GetChatroomRedisInstance() *singleton {
|
||||
chatroomOnce.Do(func() {
|
||||
chatroomInstance = &singleton{}
|
||||
chatroomInstance.RedisClient = createChatroomRedisClient()
|
||||
})
|
||||
return chatroomInstance
|
||||
}
|
||||
|
||||
// SESSION redis单例
|
||||
var sessionInstance *singleton
|
||||
var sessionOnce sync.Once
|
||||
|
||||
// 获取Session redis连接
|
||||
func GetSessionRedisInstance() *singleton {
|
||||
sessionOnce.Do(func() {
|
||||
sessionInstance = &singleton{}
|
||||
sessionInstance.RedisClient = createSessionRedisClient()
|
||||
})
|
||||
return sessionInstance
|
||||
}
|
||||
|
||||
// 技能订单redis单例
|
||||
var skillInstance *singleton
|
||||
var skillOnce sync.Once
|
||||
|
||||
// 获取用户关系redis连接
|
||||
func GetSkillRedisInstance() *singleton {
|
||||
skillOnce.Do(func() {
|
||||
skillInstance = &singleton{}
|
||||
skillInstance.RedisClient = createSkillRedisClient()
|
||||
})
|
||||
return skillInstance
|
||||
}
|
||||
|
||||
// 活动redis单例
|
||||
var imInstance *singleton
|
||||
var imOnce sync.Once
|
||||
|
||||
// 获取用户关系redis连接
|
||||
func GetIMRedisInstance() *singleton {
|
||||
imOnce.Do(func() {
|
||||
imInstance = &singleton{}
|
||||
imInstance.RedisClient = createIMRedisClient()
|
||||
})
|
||||
return imInstance
|
||||
}
|
||||
|
||||
// Cms系统 redis单例
|
||||
var cmsInstance *singleton
|
||||
var cmsOnce sync.Once
|
||||
|
||||
func CmsClient() *redis.Client {
|
||||
return GetCmsRedisInstance().RedisClient
|
||||
}
|
||||
|
||||
// 获取公用redis连接
|
||||
func GetCmsRedisInstance() *singleton {
|
||||
cmsOnce.Do(func() {
|
||||
cmsInstance = &singleton{}
|
||||
cmsInstance.RedisClient = createCmsRedisClient()
|
||||
})
|
||||
return cmsInstance
|
||||
}
|
||||
163
pkg/cache/redis_client.go
vendored
Normal file
163
pkg/cache/redis_client.go
vendored
Normal file
@ -0,0 +1,163 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
logs "servicebase/pkg/log"
|
||||
"context"
|
||||
|
||||
redis "github.com/redis/go-redis/v9"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type singleton struct {
|
||||
RedisClient *redis.Client
|
||||
}
|
||||
|
||||
func Ctx() context.Context {
|
||||
return context.Background()
|
||||
}
|
||||
|
||||
// 创建公共的redis
|
||||
func createCommonmRedisClient() *redis.Client {
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: viper.GetString("redis.default.addr"),
|
||||
Password: viper.GetString("redis.default.pass"),
|
||||
DB: 0,
|
||||
PoolSize: 32,
|
||||
MinIdleConns: 4,
|
||||
})
|
||||
logs.Info("#REDIS#" + "创建一个 common redis连接池,返回一个连接" + "#REDIS#")
|
||||
return client
|
||||
}
|
||||
|
||||
// 创建user的redis
|
||||
func createUserRedisClient() *redis.Client {
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: viper.GetString("redis.default.addr"),
|
||||
Password: viper.GetString("redis.default.pass"), // no password set
|
||||
DB: 1,
|
||||
PoolSize: 32,
|
||||
MinIdleConns: 4,
|
||||
})
|
||||
|
||||
logs.Info("#REDIS#" + "创建一个 user redis连接池,返回一个连接" + "#REDIS#")
|
||||
return client
|
||||
}
|
||||
|
||||
// 创建relation的redis
|
||||
func createUserRelationRedisClient() *redis.Client {
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: viper.GetString("redis.default.addr"),
|
||||
Password: viper.GetString("redis.default.pass"), // no password set
|
||||
DB: 2,
|
||||
PoolSize: 32,
|
||||
MinIdleConns: 4,
|
||||
})
|
||||
logs.Info("#REDIS#" + "创建一个 relation redis连接池,返回一个连接" + "#REDIS#")
|
||||
return client
|
||||
}
|
||||
|
||||
// 创建timeline的redis
|
||||
func createTimelineRedisClient() *redis.Client {
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: viper.GetString("redis.default.addr"),
|
||||
Password: viper.GetString("redis.default.pass"), // no password set
|
||||
DB: 3,
|
||||
PoolSize: 32,
|
||||
MinIdleConns: 4,
|
||||
})
|
||||
logs.Info("#REDIS#" + "创建一个 timeline redis连接池,返回一个连接" + "#REDIS#")
|
||||
return client
|
||||
}
|
||||
|
||||
// 创建活动的redis
|
||||
func createActivityRedisClient() *redis.Client {
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: viper.GetString("redis.activity.addr"),
|
||||
Password: viper.GetString("redis.activity.pass"), // no password set
|
||||
DB: viper.GetInt("redis.activity.db"),
|
||||
PoolSize: 32,
|
||||
MinIdleConns: 4,
|
||||
})
|
||||
logs.Info("#REDIS#" + "创建一个 activity redis连接池,返回一个连接" + "#REDIS#")
|
||||
return client
|
||||
}
|
||||
|
||||
// 创建俱乐部的redis
|
||||
func createGuildRedisClient() *redis.Client {
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: viper.GetString("redis.guild.addr"),
|
||||
Password: viper.GetString("redis.guild.pass"), // no password set
|
||||
DB: viper.GetInt("redis.guild.db"),
|
||||
PoolSize: 32,
|
||||
MinIdleConns: 4,
|
||||
})
|
||||
logs.Info("#REDIS#" + "创建一个 guild redis连接池,返回一个连接" + "#REDIS#")
|
||||
return client
|
||||
}
|
||||
|
||||
// 创建chatroom的redis
|
||||
func createChatroomRedisClient() *redis.Client {
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: viper.GetString("redis.default.addr"),
|
||||
Password: viper.GetString("redis.default.pass"), // no password set
|
||||
DB: 6,
|
||||
PoolSize: 32,
|
||||
MinIdleConns: 4,
|
||||
})
|
||||
logs.Info("#REDIS#" + "创建一个 chatroom redis连接池,返回一个连接" + "#REDIS#")
|
||||
return client
|
||||
}
|
||||
|
||||
// 创建session的redis
|
||||
func createSessionRedisClient() *redis.Client {
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: viper.GetString("redis.default.addr"),
|
||||
Password: viper.GetString("redis.default.pass"), // no password set
|
||||
DB: 7,
|
||||
PoolSize: 32,
|
||||
MinIdleConns: 4,
|
||||
})
|
||||
logs.Info("#REDIS#" + "创建一个 session redis连接池,返回一个连接" + "#REDIS#")
|
||||
return client
|
||||
}
|
||||
|
||||
// 创建用户关系的redis
|
||||
func createSkillRedisClient() *redis.Client {
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: viper.GetString("redis.default.addr"),
|
||||
Password: viper.GetString("redis.default.pass"), // no password set
|
||||
DB: 8,
|
||||
PoolSize: 32,
|
||||
MinIdleConns: 4,
|
||||
})
|
||||
logs.Info("#REDIS#" + "创建一个 skill redis连接池,返回一个连接" + "#REDIS#")
|
||||
return client
|
||||
}
|
||||
|
||||
// 创建IM的redis
|
||||
func createIMRedisClient() *redis.Client {
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: viper.GetString("redis.default.addr"),
|
||||
Password: viper.GetString("redis.default.pass"), // no password set
|
||||
DB: 10,
|
||||
PoolSize: 32,
|
||||
MinIdleConns: 4,
|
||||
})
|
||||
logs.Info("#REDIS#" + "创建一个 im redis连接池,返回一个连接" + "#REDIS#")
|
||||
return client
|
||||
}
|
||||
|
||||
// 创建CMS的redis
|
||||
func createCmsRedisClient() *redis.Client {
|
||||
redisHost := viper.GetString("redis.default.addr")
|
||||
redisPwd := viper.GetString("redis.default.pass")
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: redisHost,
|
||||
Password: redisPwd,
|
||||
DB: 11,
|
||||
PoolSize: 32,
|
||||
MinIdleConns: 4,
|
||||
})
|
||||
logs.Info("#REDIS#" + "创建一个 cms redis连接池,返回一个连接" + "#REDIS#")
|
||||
return client
|
||||
}
|
||||
511
pkg/cache/redis_key.go
vendored
Normal file
511
pkg/cache/redis_key.go
vendored
Normal file
@ -0,0 +1,511 @@
|
||||
package cache
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
// 验证码的RedisKEY + biz_type + mobile = verification code
|
||||
AUTH_CODE_KEY_PREV = "AUTH_CODE_KEY_"
|
||||
SMS_CODE_EXPIRATION_KEY = "SMS_CODE_EXPIRATION_"
|
||||
)
|
||||
|
||||
const (
|
||||
//微信登录用户的KEY + WX_SESSION_ID = WxUser json
|
||||
WX_SESSION_USER_KEY_PREV = "WX_SESSION_USER_KEY_"
|
||||
//微信unionID绑定的嗯嗯ID + WX_UNION_ID = USER_ID
|
||||
WX_UNIONID_BINDED_ID_PREV = "WX_UNIONID_BINDED_ID_"
|
||||
)
|
||||
|
||||
const (
|
||||
//管理员Session + staff_id = staffmodel
|
||||
SESSION_STAFF_PREV = "SESSION_STAFF_"
|
||||
|
||||
BANNER_KEY_PREV = "BANNER_LIST_KEY_PREV_"
|
||||
LuckBoxPrize = "LUCK_BOX_PRIZE"
|
||||
HotBoxPrize = "HOT_BOX_PRIZE"
|
||||
HotBoxAngerPrize = "HOT_BOX_ANGER_PRIZE"
|
||||
PetPrize = "PET_PRIZE"
|
||||
// 下载页
|
||||
COMMON_DOWNLOAD_DATA = "COMMON_DOWNLOAD_DATA"
|
||||
WITHDRAW_WHITE_LIST = "WITHDRAW_SPEC_ACCOUNT_LIST_KEY"
|
||||
JACKPOT_WHITE_LIST = "HIT_EGG_SINGLE_POOL_USER"
|
||||
|
||||
HomeDataConfig = "HOME_DATA_CONFIG"
|
||||
|
||||
AppSettingKey = "APP_SETTING_KEY"
|
||||
|
||||
PRODUCT_KEY_PREV = "PRODUCT_LIST_KEY"
|
||||
ProductNCoin_KEY_PREV = "PRODUCT_N_COIN_LIST_KEY"
|
||||
|
||||
//礼物列表 = string
|
||||
GIFT_WALL_VIEW_LIST_KEY = "TOTALLY_GIFT_LIST_KEY"
|
||||
|
||||
// 审核帐号列表
|
||||
APPLE_APPROVE_ID_LIST_KEY = "APPLE_APPROVE_ID_LIST"
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
// 验证码的RedisKEY +biz_type+mobile = verification code
|
||||
VERICODE_KEY_PREV = "VERICODE_KEY_"
|
||||
|
||||
// 验证码的默认过期时间 5分钟
|
||||
VERICODE_DEFAULT_EXPIRE_DURATION = time.Minute * 5
|
||||
|
||||
// 登录验证码的过期时间 5分钟
|
||||
SIGN_IN_VERICODE_DURATION = time.Minute * 5
|
||||
|
||||
// 验证码发送时间 + biz_type + mobile = 时间
|
||||
VERICODE_SEND_TIME_PREV = "VERICODE_SEND_TIME_"
|
||||
|
||||
// 授权令牌的redisKEY +access_token = user_id
|
||||
// ACCESSTOKEN_KEY_PREV = "ACCESSTOKEN_"
|
||||
|
||||
//accesstoken 过期时间为30天
|
||||
// ACCESSTOKEN_EXPIRE_DURATION = time.Hour * 24 * 180
|
||||
|
||||
// 用户信息的RedisKEY +user_id = user_model_json
|
||||
USER_DETAIL_KEY_PREV = "USER_DETAIL_"
|
||||
|
||||
// 用户VIP信息的RedisKEY + user_id = user_vip_model_json
|
||||
USER_VIP_MODEL_KEY_PREV = "USER_VIP_MODEL_"
|
||||
|
||||
// 用户粉丝列表 + user_id = zset 按关注时间
|
||||
USER_FAN_LIST_KEY_PREV = "USER_FAN_LIST_KEY_"
|
||||
|
||||
// 用户关注列表 + user_id = zset 按关注时间
|
||||
USER_FOLLOW_LIST_KEY_PREV = "USER_FOLLOW_LIST_KEY_"
|
||||
|
||||
// 用户黑名单列表 + user_id = set
|
||||
USER_BLACK_LIST_KEY_PREV = "USER_BLACK_LIST_KEY_"
|
||||
|
||||
// 数据版本号缓存
|
||||
DATA_VERSION_KEY = "DATA_VERSION_KEY"
|
||||
|
||||
// 兴趣列表的KEY
|
||||
DATA_HOBBIES_KEY = "DATA_HOBBIES_KEY"
|
||||
|
||||
// 黑名单关键词的KEY
|
||||
DATA_BLACK_KEYWORD_KEY = "DATA_BLACK_KEYWORD_KEY"
|
||||
|
||||
//AppSetting的KEY
|
||||
APP_SETTING_KEY = "APP_SETTING_KEY"
|
||||
|
||||
// 首页内容配置
|
||||
HOME_DATA_CONFIG = "HOME_DATA_CONFIG"
|
||||
|
||||
// 七牛图片上传的TOKEN EKY
|
||||
QINIU_PHOTO_UPLOAD_KEY = "QINIU_PHOTO_UPLOAD_KEY"
|
||||
|
||||
// 运营后台七牛图片上传的TOKEN EKY
|
||||
QINIU_PHOTO_UPLOAD_KEY_FOR_OPERATION = "QINIU_PHOTO_UPLOAD_KEY_FOR_OPERATION"
|
||||
|
||||
// 七牛audio上传的TOKEN EKY
|
||||
QINIU_AUDIO_UPLOAD_KEY = "QINIU_AUDIO_UPLOAD_KEY"
|
||||
|
||||
// 七牛VIDEO上传的TOKEN EKY
|
||||
QINIU_VIDEO_UPLOAD_KEY = "QINIU_VIDEO_UPLOAD_KEY"
|
||||
|
||||
// 用户照片墙 + user_id = string
|
||||
USER_PHOTO_LIST_KEY_PREV = "USER_PHOTO_LIST_KEY_"
|
||||
|
||||
// 用户兴趣 + user_id = string
|
||||
USER_HOBBIES_LIST_KEY_PREV = "USER_HOBBIES_LIST_KEY_"
|
||||
|
||||
// 用户Tag + user_id = string
|
||||
USER_TAG_LIST_KEY_PREV = "USER_TAG_LIST_KEY_"
|
||||
|
||||
// 用户Tag + user_id = string
|
||||
USER_CS_SET = "USER_CS_SET"
|
||||
|
||||
// 用户属性 + user_id = string
|
||||
USER_PROP_LIST_KEY_PREV = "USER_PROP_LIST_KEY_"
|
||||
|
||||
// 用户别名 + user_id = hash
|
||||
USER_ALIAS_NAME_KEY_PREV = "USER_ALIAS_NAME_KEY_"
|
||||
|
||||
// 用户最后活跃时间
|
||||
USER_LAST_HEART_TIME = "USER_LAST_HEART_TIME"
|
||||
|
||||
// 商品列表 = string
|
||||
PRODUCT_LIST_KEY = "PRODUCT_LIST_KEY"
|
||||
PRODUCT_N_COIN_LIST_KEY = "PRODUCT_N_COIN_LIST_KEY"
|
||||
|
||||
// 正常的礼物列表 = string
|
||||
GIFT_LIST_KEY = "GIFT_LIST_KEY"
|
||||
|
||||
GENERAL_RANK_REWARD_KEY = "GENERAL_RANK_CHARM_REWARD_KEY"
|
||||
|
||||
GENERAL_RANK_BALLER_KEY = "GENERAL_RANK_BALLER_REWARD_KEY"
|
||||
|
||||
// 全部礼物列表 包括过期的
|
||||
TOTALLY_GIFT_LIST_KEY = "TOTALLY_GIFT_LIST_KEY"
|
||||
|
||||
// 礼物详情 + giftId = DBModel
|
||||
GIFT_DETAIL_KEY_PREV = "GIFT_DETAIL_"
|
||||
|
||||
// 首页tab列表 = string
|
||||
HOME_TAB_LIST_KEY = "HOME_TAB_LIST_KEY"
|
||||
|
||||
//banner列表 + position_code = string
|
||||
BANNER_LIST_KEY_PREV = "app:data:banner"
|
||||
|
||||
// 银行列表
|
||||
BANK_LIST_KEY = "BANK_LIST_KEY"
|
||||
|
||||
// 聊天室模板列表
|
||||
CHATROOM_TEMPLATE_LIST_KEY = "CHATROOM_TEMPLATE_LIST_KEY"
|
||||
|
||||
// 用户收藏的聊天室KEY + user_id
|
||||
USER_COLLECT_CHATROOM_LIST_KEY_PREV = "USER_COLLECT_CHATROOM_LIST_"
|
||||
|
||||
// 用户访问的聊天室KEY + user_id
|
||||
USER_VISITED_CHATROOM_LIST_KEY_PREV = "USER_VISITED_CHATROOM_LIST_"
|
||||
|
||||
// 聊天室周榜key + chatroomid + 年 + 周 = 总魅力 某个聊天室当前周总魅力值
|
||||
CHATROOM_WEEKLY_TOTAL_INCOME_KEY = "CHATROOM_WEEKLY_TOTAL_INCOME_"
|
||||
|
||||
// 聊天室周榜key + chatroomid + 年 + 周 = 总消费 某个聊天室当前周总贡献(流水)
|
||||
CHATROOM_WEEKLY_TOTAL_CONSUME_KEY = "CHATROOM_WEEKLY_TOTAL_CONSUME_"
|
||||
|
||||
// 聊天室周榜key + 年 + 周 = ZSCORE 周聊天室收益排行榜
|
||||
CHATROOM_WEEKLY_ROOM_REVENUE_TOP_LIST = "CHATROOM_WEEKLY_ROOM_REVENUE_TOP_LIST"
|
||||
|
||||
// 房间周贡献榜KEY + chatroomid + 年 + 周 = zset集合 某个聊天室的周贡献榜单
|
||||
CHATROOM_WEEKLY_CONSUME_USER_LIST_KEY_PREV = "CHATROOM_WEEKLY_CONSUME_USER_LIST_KEY_"
|
||||
|
||||
// 房间月贡献榜KEY + chatroomid + 年月 = zset集合 某个聊天室的月贡献榜单
|
||||
CHATROOM_MONTH_CONSUME_USER_LIST_KEY_PREV = "CHATROOM_MONTH_CONSUME_USER_LIST_KEY_"
|
||||
|
||||
// 房间总贡献榜KEY + chatroomid = zset集合 某个聊天室的贡献榜单
|
||||
CHATROOM_ALL_CONSUME_USER_LIST_KEY_PREV = "CHATROOM_ALL_CONSUME_USER_LIST_KEY_"
|
||||
|
||||
// 房间日贡献榜KEY + chatroomid = zset集合 某个聊天室的贡献榜单
|
||||
CHATROOM_DAY_CONSUME_USER_LIST_KEY_PREV = "CHATROOM_DAY_CONSUME_USER_LIST_KEY_"
|
||||
|
||||
// 房间周魅力KEY + chatroomid + 年 + 周 = zset集合 某个聊天室的周魅力榜单
|
||||
CHATROOM_WEEKLY_CHARM_USER_LIST_KEY_PREV = "CHATROOM_WEEKLY_CHARM_USER_LIST_KEY_"
|
||||
|
||||
// 房间月魅力KEY + chatroomid + 年月 = zset集合 某个聊天室的月魅力榜单
|
||||
CHATROOM_MONTH_CHARM_USER_LIST_KEY_PREV = "CHATROOM_MONTH_CHARM_USER_LIST_KEY_"
|
||||
|
||||
// 房间总魅力KEY + chatroomid + 年月 = zset集合 某个聊天室的总魅力榜单
|
||||
CHATROOM_ALL_CHARM_USER_LIST_KEY_PREV = "CHATROOM_ALL_CHARM_USER_LIST_KEY_"
|
||||
|
||||
// 房间日魅力KEY + chatroomid + 日 = zset集合 某个聊天室的日魅力榜单
|
||||
CHATROOM_DAY_CHARM_USER_LIST_KEY_PREV = "CHATROOM_DAY_CHARM_USER_LIST_KEY_"
|
||||
|
||||
// 聊天室超级管理员列表 SET
|
||||
CHATROOM_SUPER_ADMIN_USER_LIST_KEY = "CHATROOM_SUPER_ADMIN_USER_LIST"
|
||||
|
||||
// 聊天室房管列表key+chatroomId SET 某个聊天室的管理员列表
|
||||
CHATROOM_ADMIN_USER_LIST_PREV = "CHATROOM_ADMIN_USER_LIST_"
|
||||
|
||||
// 聊天室主持人列表key+chatroomId SET 某个聊天室的主持人列表
|
||||
CHATROOM_HOST_USER_LIST_PREV = "CHATROOM_HOST_USER_LIST_"
|
||||
|
||||
// 聊天室黑名单列表key+chatroomId SET 某个聊天室的黑名单列表
|
||||
CHATROOM_BLACK_USER_LIST_PREV = "CHATROOM_BLACK_USER_LIST_"
|
||||
|
||||
// 聊天室在线人数 + chatroomId 某个聊天室的在线人数
|
||||
CHATROOM_ONLINE_USER_COUNT_PREV = "CHATROOM_ONLINE_USER_COUNT_"
|
||||
|
||||
// 聊天室在线人数 + chatroomId 某个跟聊天室的在线人数(包含子房间)
|
||||
CHATROOM_ROOT_ONLINE_USER_SET__PREV = "CHATROOM_ROOT_ONLINE_USER_SET_"
|
||||
|
||||
// 开启的聊天室列表key 全服正在开厅的聊天室列表
|
||||
CHATROOM_OPENING_LIST_KEY = "CHATROOM_OPENING_LIST_KEY"
|
||||
|
||||
// 推荐到首页的房间
|
||||
SET_RECOMMEND_ROOM_IDS_KEY = "SET_RECOMMEND_ROOM"
|
||||
|
||||
CHATROOM_EMOJI_RESULT_PREV = "CHATROOM_EMOJI_RESULT_"
|
||||
|
||||
// 聊天室当前主持人 + chatroomId = user_id 某个聊天室当前的主持人ID
|
||||
CHATROOM_CURRENT_HOST_USER_PREV = "CHATROOM_CURRENT_HOST_USER_"
|
||||
|
||||
// 聊天室DBModel缓存 某个聊天室的DB MODEL缓存
|
||||
// CHATROOM_DB_MODEL_KEY_PREV = "CHATROOM_DB_MODEL_KEY_"
|
||||
|
||||
// 聊天室DBModel缓存 某个聊天室的DB MODEL缓存
|
||||
CHATROOM_HOT_SET = "CHATROOM_HOT_SET_"
|
||||
|
||||
// 聊天室EXT DBModel缓存 某个聊天室的EXT DB MODEL缓存
|
||||
CHATROOM_EXT_DB_MODEL_KEY_PREV = "CHATROOM_EXT_DB_MODEL_KEY_PREV"
|
||||
|
||||
// 聊天室全服周贡献榜KEY + 年 + 周 = zset 全服周贡献榜
|
||||
CHATROOM_TOTAL_CONSUME_USER_LIST_PREV = "CHATROOM_TOTAL_CONSUME_USER_LIST_"
|
||||
|
||||
// 聊天室全服周魅力榜KEY + 年 + 周 = zset 全服周魅力榜
|
||||
CHATROOM_TOTAL_CHARM_USER_LIST_PREV = "CHATROOM_TOTAL_CHARM_USER_LIST_"
|
||||
|
||||
// 牵手榜
|
||||
CHATROOM_HAND_PREV = "CHATROOM_HAND_PREV_"
|
||||
|
||||
// 聊天室打赏连击次数key + chatroomId + userId + giftId + touserId = 连击次数
|
||||
CHATROOM_USER_COMBO_HIT_COUNT_PREV = "CHATROOM_USER_COMBO_HIT_COUNT_"
|
||||
|
||||
// 连击已发送的全服消息数 + chatroomId + userId + giftId + touserId = 已发送的飞机数
|
||||
CHATROOM_COMBOHIT_SENDED_FULL_MSG_COUNT_PREV = "CHATROOM_COMBOHIT_SENDED_FULL_MSG_COUNT_"
|
||||
|
||||
// 聊天室表情列表
|
||||
CHATROOM_EMOJI_LIST_KEY = "CHATROOM_EMOJI_LIST_KEY"
|
||||
|
||||
// 聊天室座位缓存 key+chatroomId HASH 某个聊天室的座位列表
|
||||
CHATROOM_SEAT_LIST_HASH_KEY_PREV = "CHATROOM_SEAT_LIST_HASH_KEY_"
|
||||
// 管理员麦序
|
||||
CHATROOM_SEAT_LIST_ADMIN_KEY_PREV = "CHATROOM_SEAT_LIST_ADMIN_"
|
||||
CHATROOM_SEAT_HASH_ADMIN_KEY_PREV = "CHATROOM_SEAT_HASG_ADMIN_"
|
||||
// 自由麦序
|
||||
CHATROOM_SEAT_LIST_FREE_KEY_PREV = "CHATROOM_SEAT_LIST_FREE_"
|
||||
CHATROOM_SEAT_HASH_FREE_KEY_PREV = "CHATROOM_SEAT_HASH_FREE_"
|
||||
// 聊天室排队列表对象缓存 key+chatroomId Hset 某个聊天室的申请上麦列表
|
||||
CHATROOM_WAITING_QUEUE_LIST_REVE = "CHATROOM_WAITING_QUEUE_LIST_"
|
||||
// 聊天室排队列表 有序userId列表
|
||||
CHATROOM_WAITING_QUEUE_SORTED_USERID_PREV = "CHATROOM_WAITING_QUEUE_SORTED_USERID_"
|
||||
CHATROOM_COMMON_SEAT_COUNTDOWN_LOCK = "CHATROOM_COMMON_SEAT_COUNTDOWN_LOCK"
|
||||
CHATROOM_COMMON_SEAT_COUNTDOWN_ZSET = "CHATROOM_COMMON_SEAT_COUNTDOWN"
|
||||
|
||||
// 聊天室的命令编号
|
||||
CHATROOM_CMD_SEQUENCE_NUMBER_PREV = "CHATROOM_CMD_SEQUENCE_NUMBER_"
|
||||
|
||||
// VIP配置列表
|
||||
VIP_CONFIG_MODEL_LIST_KEY = "VIP_CONFIG_MODEL_LIST"
|
||||
|
||||
// VIP配置列表
|
||||
VIP_RIGHT_MODEL_LIST_KEY = "VIP_RIGHT_MODEL_LIST"
|
||||
|
||||
// 用户当前所在的房间ID + userid = chatroomid
|
||||
// USER_CURRENT_IN_CHATROOM_ID_PREV = "USER_CURRENT_IN_CHATROOM_ID_"
|
||||
|
||||
// 用户在聊天室的心跳时间 + userid = datetime
|
||||
USER_IN_CHATROOM_HEARTBEAT_TIME_PREV = "USER_IN_CHATROOM_HEARTBEAT_TIME_"
|
||||
|
||||
// 交友房间的阶段 开始时间 等数据 HASH + chatroomId = hash
|
||||
FRIEND_CHATROOM_GAME_DATA_PREV = "FRIEND_CHATROOM_GAME_DATA_"
|
||||
|
||||
// 被踢出房间的用户KEY +chatroom_id + user_id = 1
|
||||
KICKED_USER_FROM_CHATROOM_PREV = "KICKED_USER_FROM_CHATROOM_"
|
||||
|
||||
// 交友模板 房间当轮魅力值 + chatroomId = zset
|
||||
CHATROOM_CURRENT_ROUND_CHARM_ZSET = "CHATROOM_CURRENT_ROUND_CHARM_ZSET"
|
||||
|
||||
// 聊天室关闭时间 10秒过期,如果存在不能创建 +chatroomId = close_time
|
||||
CHATROOM_CLOSE_TIME_PREV = "CHATROOM_CLOSE_TIME_PREV"
|
||||
|
||||
// 聊天室在线成员列表 + chatroomId = set
|
||||
CHATROOM_ONLINE_MEMBER_LIST_PREV = "CHATROOM_ONLINE_MEMBER_LIST_PREV"
|
||||
|
||||
// 聊天室机器人列表 + chatroomId = set
|
||||
CHATROOM_ROBOT_LIST_PREV = "CHATROOM_ROBOT_LIST_PREV"
|
||||
|
||||
// 聊天室守护配置 string
|
||||
CHATROOM_GUARD_CONFIG_KEY = "CHATROOM_GUARD_CONFIG_KEY"
|
||||
|
||||
// 聊天室活动列表KEY
|
||||
CHATROOM_ACTIVITY_LIST_KEY = "CHATROOM_ACTIVITY_LIST_KEY"
|
||||
|
||||
// 用户隐私设置的KEY +userId = string
|
||||
USER_PRIVACY_SETTING_KEY = "USER_PRIVACY_SETTING_KEY_"
|
||||
|
||||
// 用户头像框的KEY +userId = string
|
||||
USER_AVATAR_DECORATION_KEY = "USER_AVATAR_DECORATION_KEY_"
|
||||
|
||||
// 聊天室背景图 + chatroomID = string
|
||||
CHATROOM_BACKGROUDN_IMG_URL_PREV = "CHATROOM_BACKGROUDN_IMG_URL_"
|
||||
|
||||
// 聊天室每分钟的流水 + chatroomID + 分钟 = int64
|
||||
CHATROOM_MINUTE_REVENUE_PREV = "CHATROOM_MINUTE_REVENUE_"
|
||||
|
||||
// 个人房间在线人数排序SET + CHATROOMID = ZSET SCORE = 人数
|
||||
PERSONAL_ROOM_SORTED_SET = "PERSONAL_ROOM_SORTED_SET"
|
||||
|
||||
// 用户在聊天室里的签结果 + chatroomId + useId = string
|
||||
ROCK_SKEWER_RESULT_PREV = "ROCK_SKEWER_RESULT_"
|
||||
|
||||
// 礼物对应的头饰 + GIFTID = string
|
||||
GIFT_INCLUDED_AVATAR_DECORATION_LIST_PREV = "GIFT_INCLUDED_AVATAR_DECORATION_LIST_"
|
||||
|
||||
// 商品详情 + goodsId = string
|
||||
GOODS_DETAIL_DB_MODEL_PREV = "GOODS_DETAIL_DB_MODEL_"
|
||||
|
||||
// 用户当前使用的头饰或座驾 + GOODS_TYPE + "_" + USER_ID = USER_STORE MODEL STR
|
||||
USER_CURRENT_USED_STORE_MODEL_PREV = "USER_CURRENT_USED_STORE_MODEL_"
|
||||
|
||||
// 订单是否正在支付 + order_id = 1
|
||||
PAY_ORDER_IS_PAYING_PREV = "PAY_ORDER_IS_PAYING_"
|
||||
|
||||
//============= 技能订单相关 ==================
|
||||
|
||||
// 技能对象缓存的KEY + skill_id = string
|
||||
SKILL_MODEL_KEY_PREV = "SKILL_MODEL_"
|
||||
|
||||
// 技能配置
|
||||
SKILL_CONFIG_TREE_HASH_KEY = "SKILL_CONFIG_TREE_HASH"
|
||||
|
||||
// 技能配置
|
||||
SKILL_CONFIG_KEY = "SKILL_CONFIG_"
|
||||
|
||||
// 用户审核中的技能 +user_id = SET
|
||||
USER_APPLYING_SKILL_ID_LIST_PREV = "USER_APPLYING_SKILL_ID_LIST_"
|
||||
|
||||
// /////////////////////////
|
||||
// 派单 1、添加到房间的派单列表 CHATROOM_CURRENT_DISPATCH_ORDER_LIST
|
||||
// 2、追加接单的用户 DISPATCH_ORDER_TO_USER_LIST DISPATCH_ORDER_TO_USER_MAP
|
||||
// 3、用户可接单 DISPATCH_ORDER_USER_GET_LIST
|
||||
// 陪玩接单
|
||||
// 1、移除用户可接单 DISPATCH_ORDER_USER_GET_LIST
|
||||
// 2、添加用户已接单 DISPATCH_ORDER_USER_GET_TAKED_LIST
|
||||
// 3、添加派单已接单用户 DISPATCH_ORDER_TO_USER_TAKED_LIST
|
||||
// 派单结束
|
||||
// 1、删除房间派单列表中的数据 CHATROOM_CURRENT_DISPATCH_ORDER_LIST
|
||||
// 2、用户可解的单中删除该派单 DISPATCH_ORDER_USER_GET_LIST DISPATCH_ORDER_USER_GET_TAKED_LIST
|
||||
// 3、删除派给的用户列表 DISPATCH_ORDER_TO_USER_LIST DISPATCH_ORDER_TO_USER_MAP
|
||||
// /////////////////////////
|
||||
|
||||
// 聊天室当前派单ID + chatroomid = order_id
|
||||
CHATROOM_CURRENT_DISPATCH_ORDER_ID = "CHATROOM_CURRENT_DISPATCH_ORDER_ID_"
|
||||
// 聊天室当前派单列表 + chatroomid = order_id
|
||||
CHATROOM_CURRENT_DISPATCH_ORDER_LIST = "CHATROOM_CURRENT_DISPATCH_ORDER_LIST_"
|
||||
// 当前派单已派的用户LIST + order_id = userIds
|
||||
DISPATCH_ORDER_TO_USER_LIST = "DISPATCH_ORDER_TO_USER_LIST_"
|
||||
// 当前派单已派的用户SET + order_id = userIds (实际是Set)
|
||||
DISPATCH_ORDER_TO_USER_MAP = "DISPATCH_ORDER_TO_USER_MAP_"
|
||||
// 当前用户可接的派单 + user_id = orderIds
|
||||
DISPATCH_ORDER_USER_GET_LIST = "DISPATCH_ORDER_USER_GET_LIST_"
|
||||
// 当前派单接单的用户 + order_id = userIds
|
||||
DISPATCH_ORDER_TO_USER_TAKED_LIST = "DISPATCH_ORDER_TO_USER_TAKED_LIST_"
|
||||
// 当前用户已接的派单 + order_id = userIds
|
||||
DISPATCH_ORDER_USER_GET_TAKED_LIST = "DISPATCH_ORDER_USER_GET_TAKED_LIST_"
|
||||
|
||||
// ============= 定时任务ID ==============
|
||||
|
||||
// 定时清理异常掉线的聊天室用户
|
||||
TASK_ID_CLEAR_CHATROOM_DEADED_USER = "TASK_ID_CLEAR_CHATROOM_DEADED_USER"
|
||||
TASK_ID_CLEAR_DISPATCH_CHATROOM_DEADED_USER = "TASK_ID_CLEAR_DISPATCH_CHATROOM_DEADED_USER"
|
||||
|
||||
// 自动解冻帐号
|
||||
TASK_ID_AUTO_UNFROZEN_USER = "TASK_ID_AUTO_UNFROZEN_USER"
|
||||
|
||||
// 定时清理房间麦序
|
||||
TASK_ID_CLEAR_CHATROOM_SEAT_USER = "TASK_ID_CLEAR_CHATROOM_SEAT_USER"
|
||||
|
||||
// 定时清理个人厅异常掉线的聊天室用户
|
||||
TASK_ID_CLEAR_PERSONAL_CHATROOM_DEADED_USER = "TASK_ID_CLEAR_PERSONAL_CHATROOM_DEADED_USER"
|
||||
|
||||
// ============ 打赏特殊帐号 刷空单 ==================
|
||||
// 老板号
|
||||
DASHANG_SPEC_ACCOUNT_BOSS_LIST_KEY = "DASHANG_SPEC_ACCOUNT_BOSS_LIST"
|
||||
|
||||
// 服务员号
|
||||
DASHANG_SPEC_ACCOUNT_WAITTER_LIST_KEY = "DASHANG_SPEC_ACCOUNT_WAITTER_LIST"
|
||||
|
||||
// 指定厅
|
||||
DASHANG_SPEC_BOSS_WAITTER_ROOM_LIST_KEY = "DASHANG_SPEC_BOSS_WAITTER_ROOM_LIST_KEY"
|
||||
|
||||
// 置顶厅
|
||||
SET_TOP_CHATROOM_ID_LIST_KEY = "SET_TOP_CHATROOM_ID_LIST_KEY"
|
||||
|
||||
// 提现特殊帐号 可日提的
|
||||
WITHDRAW_SPEC_ACCOUNT_LIST_KEY = "WITHDRAW_SPEC_ACCOUNT_LIST_KEY"
|
||||
|
||||
// SVIP用户每次钻石打赏限额 + user_id = 已用
|
||||
HIT_EGG_SVIP_USER_DAY_USED_MONEY = "HIT_EGG_SVIP_USER_DAY_USED_MONEY_"
|
||||
|
||||
// 用户动态数 + user_id = value
|
||||
USER_TIMELINE_COUNT = "USER_TIMELINE_COUNT_"
|
||||
|
||||
// 用户位置的集合
|
||||
LOCATION_USER = "LOCATION_USER"
|
||||
|
||||
// 用户最后获取广播的时间 + user_id = string
|
||||
USER_LAST_GET_BROADCAST_TIME = "USER_LAST_GET_BROADCAST_TIME_"
|
||||
|
||||
// 动态详情model + timelineId = model json
|
||||
TIMELINE_MODEL_PREV = "TIMELINE_MODEL_"
|
||||
|
||||
// 日注册用户数 + YYYY-MM-DD = string
|
||||
DAY_SIGNUP_USER_COUNT_PREV = "DAY_SIGNUP_USER_COUNT_"
|
||||
|
||||
// 技能等级配置
|
||||
SKILL_LEVEL_CONFIG = "SKILL_LEVEL_CONFIG"
|
||||
|
||||
// 动态Location的集合
|
||||
TIMELINE_LOCATION_COLLECTION_KEY = "TIMELINE_LOCATION_COLLECTION"
|
||||
|
||||
// 动态的点赞用户列表 + TIMELINEID = LIST
|
||||
TIMELINE_PRAISED_USER_LIST_PREV = "TIMELINE_PRAISED_USER_LIST_"
|
||||
|
||||
// 动态的点赞数 = string
|
||||
TIMELINE_PRAISED_USER_COUNT_PREV = "TIMELINE_PRAISED_USER_COUNT_"
|
||||
|
||||
// 关注的动态 +userid = list
|
||||
TIMELINE_FOLLOWED_KEY_PREV = "TIMELINE_FOLLOWED_KEY_"
|
||||
|
||||
// 关注的动态 +userid = list
|
||||
TIMELINE_FOLLOWED_NEW_CNT_KEY_PREV = "TIMELINE_FOLLOWED_NEW_CNT_KEY_"
|
||||
|
||||
// 用户勋章缓存 + USER_ID = STR
|
||||
USER_MEDAL_LIST = "USER_MEDAL_LIST_"
|
||||
|
||||
// SKU勋章缓存 + SKU_ID = STR
|
||||
SKU_MEDAL_LIST = "SKU_MEDAL_LIST_"
|
||||
|
||||
// 勋章缓存 = STR
|
||||
MEDAL_LIST_KEY = "MEDAL_LIST_KEY"
|
||||
|
||||
// 进入场景对象缓存 + sceneId = str
|
||||
ENTER_ROOM_SCENE_MODEL_PREV = "ENTER_ROOM_SCENE_MODEL_"
|
||||
|
||||
// VIP SCENE 列表
|
||||
VIP_SCENE_LIST = "VIP_SCENE_LIST"
|
||||
|
||||
// MainTabBar 列表 = string
|
||||
MAIN_TAB_BAR_LIST_KEY = "MAIN_TAB_BAR_LIST"
|
||||
|
||||
// 青少年模式集合
|
||||
CHILD_MODE_SET_KEY = "CHILD_MODE_SET"
|
||||
|
||||
// 爵位配置Hash
|
||||
ALL_NOBILITY_CONFIG_LIST_KEY = "ALL_NOBILITY_CONFIG_LIST"
|
||||
|
||||
// 用户贵族信息的RedisKEY + user_id = user_nobility_model_json
|
||||
USER_NOBILITY_MODEL_KEY_PREV = "USER_NOBILITY_MODEL_"
|
||||
|
||||
// 开箱排行榜KEY
|
||||
USER_OPEN_BOX_RANK_PREV = "USER_OPEN_BOX_RANK_"
|
||||
|
||||
// 易盾TOKEN对应手机号
|
||||
YIDUN_TOKEN_PREV = "YIDUN_TOKEN_"
|
||||
|
||||
//APPLE登录AUTH CODE的key
|
||||
APPLE_AUTH_CODE_PREV = "APPLE_AUTH_CODE_"
|
||||
|
||||
// 用户支付宝充值的支付宝帐号hash + USERID = HASH
|
||||
USER_ALIPAY_RECHARGE_PAY_ACCOUNT_HASH_PREV = "USER_ALIPAY_RECHARGE_PAY_ACCOUNT_HASH_"
|
||||
USER_ALIPAY_RECHARGE_N_COIN_PAY_ACCOUNT_HASH_PREV = "USER_ALI_RECHARGE_N_COIN_ACCOUNT_HASH_"
|
||||
|
||||
// 谁给用户发了多少信息 + USER_ID = HASH
|
||||
USER_MSG_MAP_FROM_PREV = "USER_MSG_MAP_FROM_"
|
||||
|
||||
// 用户特殊颜色缓存
|
||||
USER_SPEC_COLOR_MODEL_PREV = "USER_SPEC_COLOR_MODEL_"
|
||||
|
||||
// 用户IAP充值 预检查通过的key
|
||||
USER_IAP_RECHARGE_PASS_KEY = "USER_IAP_RECHARGE_PASS_"
|
||||
|
||||
// 用户最后获取访客记录的时间
|
||||
USER_LAST_GET_VISITED_LIST_TIME_PREV = "USER_LAST_GET_VISITED_LIST_TIME_"
|
||||
|
||||
// 用户新访客set
|
||||
USER_NEW_VISITOR_SET_PREV = "USER_NEW_VISITOR_SET_"
|
||||
|
||||
// 用户修改昵称缓存记录
|
||||
USER_EDIT_NICKNAME_COUNT_PREV = "USER_EDIT_NICKNAME_COUNT_"
|
||||
|
||||
// 公会相关的
|
||||
// 公会信息
|
||||
GUILD_MODEL_KEY_PREV = "guild:model:"
|
||||
|
||||
// 周星礼物发放定时任务的礼物列表
|
||||
WEEK_STAR_GRANT_GIFT_KEY = "WEEK_STAR_GRANT_GIFT_KEY"
|
||||
)
|
||||
68
pkg/cache/redis_lock.go
vendored
Normal file
68
pkg/cache/redis_lock.go
vendored
Normal file
@ -0,0 +1,68 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"servicebase/pkg/log"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Lock(key string) (e error) {
|
||||
var success bool
|
||||
TryMax := 6
|
||||
redisClient := GetCommonRedisInstance().RedisClient
|
||||
for i := 0; i < TryMax; i++ {
|
||||
success, e = redisClient.SetNX(Ctx(), key, time.Now(), time.Millisecond*1000*3).Result()
|
||||
if e != nil {
|
||||
log.Info("Lock GetCommonRedisInstance error: " + e.Error())
|
||||
}
|
||||
if success {
|
||||
return nil
|
||||
}
|
||||
time.Sleep(time.Millisecond * 50)
|
||||
}
|
||||
return errors.New("操作太频繁了~")
|
||||
}
|
||||
|
||||
func Free(key string) {
|
||||
client := GetCommonRedisInstance().RedisClient
|
||||
_ = client.Del(Ctx(), key).Err()
|
||||
}
|
||||
|
||||
const (
|
||||
TryMax = int(30)
|
||||
TryDuration = time.Millisecond * 13
|
||||
LockTimeout = time.Millisecond * 700
|
||||
)
|
||||
|
||||
func RedisLockGetTime(key string, duration time.Duration) (e error) {
|
||||
redisClient := GetCommonRedisInstance().RedisClient
|
||||
success, e := redisClient.SetNX(Ctx(), key, "1", duration).Result()
|
||||
if !success {
|
||||
e = errors.New("(LOCK_FAILED)系统繁忙,请稍后再试!")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func RedisLockGet(key string) (e error) {
|
||||
redisClient := GetCommonRedisInstance().RedisClient
|
||||
success := false
|
||||
for i := 0; i < TryMax; i++ {
|
||||
success, e = redisClient.SetNX(Ctx(), key, time.Now(), LockTimeout).Result()
|
||||
if nil != e {
|
||||
return
|
||||
}
|
||||
if success {
|
||||
break
|
||||
}
|
||||
time.Sleep(TryDuration)
|
||||
}
|
||||
if !success {
|
||||
e = errors.New("(LOCK_FAILED)系统繁忙,请稍后再试!")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func RedisLockDel(key string) {
|
||||
redisClient := GetCommonRedisInstance().RedisClient
|
||||
_ = redisClient.Del(Ctx(), key).Err()
|
||||
}
|
||||
68
pkg/cache/user/user.go
vendored
Normal file
68
pkg/cache/user/user.go
vendored
Normal file
@ -0,0 +1,68 @@
|
||||
package cache_user
|
||||
|
||||
import (
|
||||
"servicebase/pkg/cache"
|
||||
"servicebase/pkg/tools"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/anxpp/beego/logs"
|
||||
"github.com/anxpp/common-utils/str"
|
||||
)
|
||||
|
||||
const key = "user:models"
|
||||
const keyToken = "user:token"
|
||||
|
||||
// 用户当前所在的房间ID + userid = chatroomid
|
||||
const ACCESSTOKEN_KEY_PREV = "ACCESSTOKEN_"
|
||||
const ACCESSTOKEN_EXPIRE_DURATION = time.Hour * 24 * 180
|
||||
|
||||
// 通过token获取userId
|
||||
func IdByToken(token string) (userId string) {
|
||||
if len(token) == 0 {
|
||||
return
|
||||
}
|
||||
var e error
|
||||
if userId, e = cache.CommonClient().HGet(cache.Ctx(), keyToken, token).Result(); e != nil || len(userId) == 0 {
|
||||
userId, e = cache.CommonClient().Get(cache.Ctx(), ACCESSTOKEN_KEY_PREV+token).Result()
|
||||
if e != nil {
|
||||
logs.Error("GetUserIdByAccessToken redisError=%v token=%v", e.Error(), token)
|
||||
return
|
||||
} else if len(userId) > 0 {
|
||||
// 迁移token
|
||||
ttl, err := cache.CommonClient().TTL(cache.Ctx(), ACCESSTOKEN_KEY_PREV+token).Result()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
value := fmt.Sprintf("%s#%s", userId, str.TimeToString(time.Now().Add(ttl)))
|
||||
if err := cache.CommonClient().HSet(cache.Ctx(), keyToken, token, value).Err(); err == nil {
|
||||
cache.CommonClient().Del(cache.Ctx(), ACCESSTOKEN_KEY_PREV+token)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
arr := strings.Split(userId, "#")
|
||||
if len(arr) != 2 {
|
||||
_ = IdTokenDel(token)
|
||||
return ""
|
||||
}
|
||||
over := tools.StrToDateTime(arr[1])
|
||||
if over.Before(time.Now()) {
|
||||
_ = IdTokenDel(token)
|
||||
return ""
|
||||
}
|
||||
userId = arr[0]
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func IdTokenSet(token, userId string) (e error) {
|
||||
value := fmt.Sprintf("%s#%s", userId, str.TimeToString(time.Now().Add(ACCESSTOKEN_EXPIRE_DURATION)))
|
||||
return cache.CommonClient().HSet(cache.Ctx(), keyToken, token, value).Err()
|
||||
}
|
||||
|
||||
func IdTokenDel(token string) (e error) {
|
||||
_, e = cache.CommonClient().HDel(cache.Ctx(), keyToken, token).Result()
|
||||
cache.CommonClient().Del(cache.Ctx(), ACCESSTOKEN_KEY_PREV+token)
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user