227 lines
6.7 KiB
Go
227 lines
6.7 KiB
Go
package redisconst
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
"videoweb/pub/pubdef"
|
|
)
|
|
|
|
const (
|
|
visitLogFmt = "bd_visitlog:%s:%d"
|
|
FiveMVisit = "bd_visitFmt:%s"
|
|
BloomUser = "bd_bloom_user:%d"
|
|
AppUserToken = "bd_app_user_token:%d"
|
|
captchaKey = "bd_captcha_mobile_%s"
|
|
CaptchaExpire = 5 * time.Minute
|
|
replayNonceKey = "bd_replay:nonce:_%s"
|
|
mediaM3U8Fmt = "m3u8cache:%s"
|
|
MediaM3U8CacheExpire = time.Hour * 24 * 7
|
|
|
|
//禁止登陆设置
|
|
userForbit = "userForbit:%d"
|
|
ipForbit = "ipForbit:%s"
|
|
|
|
//每分钟Ip注册次数
|
|
registerLimist = "registerLimit:%s"
|
|
RegisterLimistExpire = time.Minute * 30
|
|
|
|
//用户关注限制
|
|
careLimitKey = "carelimit:%d"
|
|
CareLimitKeyExpire = 15 * time.Second
|
|
|
|
//用户评论限制
|
|
commentLimitKey = "commentlimit:%s"
|
|
CommentLimitKeyExpire = 5 * time.Second
|
|
CommentDayLimitKeyExpire = 10 * time.Hour
|
|
|
|
ContactUsLimitKey = "contactuslimit:%s"
|
|
ContactusLimitKeyExpire = 1 * time.Minute
|
|
ContactusLimitKeyDayExpire = 10
|
|
//每用户访问次数限制
|
|
userCallLimit = "userCall:%d"
|
|
ipCallLimit = "ipCall:%s"
|
|
CallLimitExpire = time.Second * 10
|
|
|
|
userSidKey = "userSid:%d"
|
|
UserSidKeyExpire = time.Minute * 30
|
|
|
|
// 评论缓存key和过期时间
|
|
CommentListKey = "commentList_%d_%d"
|
|
CommentListKeyExpire = time.Minute * 5
|
|
|
|
// 视频收藏列表分页缓存key、版本key和过期时间
|
|
FavoriteListVersionKeyExpire = time.Hour * 24
|
|
|
|
FavoriteVideoListKey = "favoriteVideoList:v%d:%d:%d:%d"
|
|
FavoriteVideoListVersionKey = "favoriteVideoListVersion:%d"
|
|
FavoriteVideoListKeyExpire = time.Minute * 30
|
|
|
|
// 问卷提交频率限制(同一 IP / 用户每分钟只能提交一次)
|
|
questionnaireLimitIp = "questionnaireLimit:ip:%s"
|
|
questionnaireLimitUser = "questionnaireLimit:user:%d"
|
|
QuestionnaireLimitExpire = time.Minute
|
|
|
|
// m3u8 / enkey 防盗链
|
|
// IP 自然日正片去重计数(key 含日期,次日自动换桶)
|
|
m3u8DayRateLimitIp = "m3u8dayrate:ip:%s:%s" // %s=ip, %s=日期 20060102
|
|
m3u8DailyMediaIpFmt = "m3u8:daily:media:ip:%s:%d:%s" // ip, mediaId, 日期
|
|
M3U8DayRateLimitMax = int64(80) // 每 IP 每天最多 80 个不同正片(sys_cfg 未配置时的默认值)
|
|
// enkey 一次性票据:/h5/m3u8 鉴权通过后写入,/enkey 消费后删除
|
|
enkeyTicketKey = "enkey:ticket:%s" // %s = token 前16位
|
|
EnkeyTicketExpire = 15 * time.Minute
|
|
|
|
// 访问IP去重统计:每个 appId 每天一个 SET
|
|
ipStatKey = "ipstat:%d:%s" // %d=appId, %s=日期 "20060102"
|
|
IpStatExpire = 49 * time.Hour // 保留49小时,确保昨日数据在今天仍可读
|
|
)
|
|
|
|
func BloomUserKey(userId int64) string {
|
|
return fmt.Sprintf(BloomUser, userId)
|
|
}
|
|
|
|
func FiveMVisitKey(time string) string {
|
|
return fmt.Sprintf(FiveMVisit, time)
|
|
}
|
|
|
|
func UserVisitLogKey(uid int64, t time.Time) string {
|
|
return fmt.Sprintf(visitLogFmt, t.Format("20060102"), uid)
|
|
}
|
|
|
|
func MobileCaptchaKey(mobile string) string {
|
|
return fmt.Sprintf(captchaKey, mobile)
|
|
}
|
|
|
|
func AppUserTokenKey(userId int64) string {
|
|
return fmt.Sprintf(AppUserToken, userId)
|
|
}
|
|
|
|
// AppPlayletHomeCacheVersionKey 用于后台变更后切换短剧首页缓存版本。
|
|
func AppPlayletHomeCacheVersionKey() string {
|
|
return fmt.Sprintf("app:playlet:home:version:%d", pubdef.AppId)
|
|
}
|
|
|
|
// AppPlayletRankCacheVersionKey 用于短剧删除后刷新榜单缓存
|
|
func AppPlayletRankCacheVersionKey() string {
|
|
return fmt.Sprintf("app:playlet:rank:version:%d", pubdef.AppId)
|
|
}
|
|
|
|
func MediaM3U8Key(name string) string {
|
|
return fmt.Sprintf(mediaM3U8Fmt, name)
|
|
}
|
|
func UserForbitKey(userId int64) string {
|
|
return fmt.Sprintf(userForbit, userId)
|
|
}
|
|
|
|
func RegisterLimitKey(ip string) string {
|
|
ip = strings.ReplaceAll(ip, ":", "-")
|
|
return fmt.Sprintf(registerLimist, ip)
|
|
}
|
|
|
|
func CareLimitKey(userId int64) string {
|
|
return fmt.Sprintf(careLimitKey, userId)
|
|
}
|
|
func ReplayNonceKey(nonce string) string {
|
|
return fmt.Sprintf(replayNonceKey, nonce)
|
|
}
|
|
|
|
func CommentLimitKey(ip string) string {
|
|
return fmt.Sprintf(commentLimitKey, ip)
|
|
}
|
|
func CommentOneDayLimitKey(ip string) string {
|
|
return fmt.Sprintf("commentdaylimit:%s", ip)
|
|
}
|
|
|
|
func UserCallLimitKey(userId int64) string {
|
|
return fmt.Sprintf(userCallLimit, userId)
|
|
}
|
|
|
|
func IpCallLimitKey(ip string) string {
|
|
return fmt.Sprintf(ipCallLimit, ip)
|
|
}
|
|
|
|
func IpForbitKey(ip string) string {
|
|
ip = strings.ReplaceAll(ip, ":", "-")
|
|
return fmt.Sprintf(ipForbit, ip)
|
|
}
|
|
|
|
func UserSidKey(userId int64) string {
|
|
return fmt.Sprintf(userSidKey, userId)
|
|
}
|
|
|
|
// CommentListCacheKey 返回评论列表缓存key
|
|
func CommentListCacheKey(objectId int64, objectType pubdef.CommentObjectType) string {
|
|
return fmt.Sprintf(CommentListKey, objectId, objectType)
|
|
}
|
|
|
|
func FavoriteVideoListCacheKey(userId, version, pageNum, pageSize int64) string {
|
|
return fmt.Sprintf(FavoriteVideoListKey, version, userId, pageNum, pageSize)
|
|
}
|
|
|
|
func FavoriteVideoListVersionCacheKey(userId int64) string {
|
|
return fmt.Sprintf(FavoriteVideoListVersionKey, userId)
|
|
}
|
|
|
|
func QuestionnaireLimitIpKey(ip string) string {
|
|
ip = strings.ReplaceAll(ip, ":", "-")
|
|
return fmt.Sprintf(questionnaireLimitIp, ip)
|
|
}
|
|
|
|
func QuestionnaireLimitUserKey(userId int64) string {
|
|
return fmt.Sprintf(questionnaireLimitUser, userId)
|
|
}
|
|
|
|
// M3U8DayRateLimitIpKey 按自然日(Asia/Shanghai)生成 IP 日限流 key
|
|
func M3U8DayRateLimitIpKey(ip string) string {
|
|
ip = normalizeM3U8Ip(ip)
|
|
loc, err := time.LoadLocation("Asia/Shanghai")
|
|
if err != nil {
|
|
loc = time.Local
|
|
}
|
|
day := time.Now().In(loc).Format("20060102")
|
|
return fmt.Sprintf(m3u8DayRateLimitIp, ip, day)
|
|
}
|
|
|
|
// M3U8DayRateLimitExpire 返回距下一个自然日 0 点的时长,用于 key TTL
|
|
func M3U8DayRateLimitExpire() time.Duration {
|
|
loc, err := time.LoadLocation("Asia/Shanghai")
|
|
if err != nil {
|
|
loc = time.Local
|
|
}
|
|
now := time.Now().In(loc)
|
|
next := time.Date(now.Year(), now.Month(), now.Day()+1, 0, 0, 0, 0, loc)
|
|
d := next.Sub(now)
|
|
if d < time.Minute {
|
|
return time.Minute
|
|
}
|
|
return d
|
|
}
|
|
|
|
// EnkeyTicketKey token 的前 16 个字符作为 key,避免存完整 token 太长
|
|
func EnkeyTicketKey(token string) string {
|
|
if len(token) > 16 {
|
|
token = token[:16]
|
|
}
|
|
return fmt.Sprintf(enkeyTicketKey, token)
|
|
}
|
|
|
|
func normalizeM3U8Ip(ip string) string {
|
|
return strings.ReplaceAll(ip, ":", "-")
|
|
}
|
|
|
|
// M3U8DailyMediaIpKey 同一 IP 同一正片当天只计一次
|
|
func M3U8DailyMediaIpKey(ip string, mediaId int64) string {
|
|
loc, err := time.LoadLocation("Asia/Shanghai")
|
|
if err != nil {
|
|
loc = time.Local
|
|
}
|
|
day := time.Now().In(loc).Format("20060102")
|
|
return fmt.Sprintf(m3u8DailyMediaIpFmt, normalizeM3U8Ip(ip), mediaId, day)
|
|
}
|
|
|
|
// IpStatKey 返回指定 appId 在某天的访问IP集合 key
|
|
// date 格式: "20060102"
|
|
func IpStatKey(appId int64, date string) string {
|
|
return fmt.Sprintf(ipStatKey, appId, date)
|
|
}
|