212 lines
5.7 KiB
Go
212 lines
5.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
|
|
appPlayletHomeCacheFmt = "app:playlet:home:%d:%s"
|
|
appPlayletHomeCacheKeyFmt = "app:playlet:home:%d"
|
|
appPlayletHomeCacheVersionFmt = "app:playlet:home:version:%d"
|
|
AppPlayletHomeCacheExpire = 10 * time.Minute
|
|
AppPlayletForwardHeatExpire = 3 * time.Second
|
|
|
|
//禁止登陆设置
|
|
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
|
|
|
|
// 访问 IP 去重统计:每个 appId 每天一个 SET
|
|
ipStatKey = "ipstat:%d:%s"
|
|
IpStatExpire = 49 * time.Hour
|
|
|
|
// m3u8 / enkey 防盗链
|
|
m3u8DayRateLimitIp = "m3u8dayrate:ip:%s:%s"
|
|
m3u8DayRateLimitUser = "m3u8dayrate:user:%d:%s"
|
|
m3u8DailyMediaIpFmt = "m3u8:daily:media:ip:%s:%d:%s"
|
|
m3u8DailyMediaUserFmt = "m3u8:daily:media:user:%d:%d:%s"
|
|
m3u8UserVipExpireFmt = "m3u8:user:vipExpire:%d"
|
|
M3U8DayRateLimitMax = int64(500) // 非有效会员/未登录:按 IP 日限
|
|
M3U8DayRateLimitVipMax = int64(1000) // 有效会员:按用户日限
|
|
M3U8UserVipExpireCache = 5 * time.Minute
|
|
)
|
|
|
|
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)
|
|
}
|
|
|
|
func MediaM3U8Key(name string) string {
|
|
return fmt.Sprintf(mediaM3U8Fmt, name)
|
|
}
|
|
|
|
func AppPlayletHomeCacheKey() string {
|
|
return fmt.Sprintf(appPlayletHomeCacheKeyFmt, pubdef.AppId)
|
|
}
|
|
|
|
func AppPlayletHomeCacheVersionKey() string {
|
|
return fmt.Sprintf(appPlayletHomeCacheVersionFmt, pubdef.AppId)
|
|
}
|
|
|
|
func AppPlayletHomeCacheDataKey(version string) string {
|
|
return fmt.Sprintf(appPlayletHomeCacheFmt, pubdef.AppId, version)
|
|
}
|
|
|
|
func AppPlayletForwardHeatKey(operator string, playletId int64) string {
|
|
operator = strings.ReplaceAll(operator, ":", "-")
|
|
return fmt.Sprintf("app:playlet:forward:heat:%s:%d", operator, playletId)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func IpStatKey(appId int64, date string) string {
|
|
return fmt.Sprintf(ipStatKey, appId, date)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func M3U8DayRateLimitUserKey(userId int64) string {
|
|
loc, err := time.LoadLocation("Asia/Shanghai")
|
|
if err != nil {
|
|
loc = time.Local
|
|
}
|
|
day := time.Now().In(loc).Format("20060102")
|
|
return fmt.Sprintf(m3u8DayRateLimitUser, userId, day)
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func normalizeM3U8Ip(ip string) string {
|
|
return strings.ReplaceAll(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)
|
|
}
|
|
|
|
func M3U8DailyMediaUserKey(userId int64, 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(m3u8DailyMediaUserFmt, userId, mediaId, day)
|
|
}
|
|
|
|
func M3U8UserVipExpireKey(userId int64) string {
|
|
return fmt.Sprintf(m3u8UserVipExpireFmt, userId)
|
|
}
|