Files
junhong_cmp_fiber/pkg/constants/redis.go
huang 931e140e8e
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 6m35s
feat: 实现 IoT 卡轮询系统(支持千万级卡规模)
实现功能:
- 实名状态检查轮询(可配置间隔)
- 卡流量检查轮询(支持跨月流量追踪)
- 套餐检查与超额自动停机
- 分布式并发控制(Redis 信号量)
- 手动触发轮询(单卡/批量/条件筛选)
- 数据清理配置与执行
- 告警规则与历史记录
- 实时监控统计(队列/性能/并发)

性能优化:
- Redis 缓存卡信息,减少 DB 查询
- Pipeline 批量写入 Redis
- 异步流量记录写入
- 渐进式初始化(10万卡/批)

压测工具(scripts/benchmark/):
- Mock Gateway 模拟上游服务
- 测试卡生成器
- 配置初始化脚本
- 实时监控脚本

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-05 17:32:44 +08:00

248 lines
8.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package constants
import "fmt"
// ========================================
// 认证相关 Redis Key
// ========================================
// RedisAuthTokenKey 生成访问令牌的 Redis 键
// 用途:存储用户 access token 信息
// 过期时间24 小时(可配置)
func RedisAuthTokenKey(token string) string {
return fmt.Sprintf("auth:token:%s", token)
}
// RedisRefreshTokenKey 生成刷新令牌的 Redis 键
// 用途:存储用户 refresh token 信息
// 过期时间7 天(可配置)
func RedisRefreshTokenKey(token string) string {
return fmt.Sprintf("auth:refresh:%s", token)
}
// RedisUserTokensKey 生成用户令牌列表的 Redis 键
// 用途:维护用户的所有有效 token 列表Set 结构)
// 过期时间7 天(可配置)
func RedisUserTokensKey(userID uint) string {
return fmt.Sprintf("auth:user:%d:tokens", userID)
}
// RedisRateLimitKey 生成限流的 Redis 键
func RedisRateLimitKey(ip string) string {
return fmt.Sprintf("ratelimit:%s", ip)
}
// RedisTaskLockKey 生成任务锁的 Redis 键
// 用途:幂等性控制,防止重复执行
// 过期时间24 小时
func RedisTaskLockKey(requestID string) string {
return fmt.Sprintf("task:lock:%s", requestID)
}
// RedisTaskStatusKey 生成任务状态的 Redis 键
// 用途:存储任务执行状态
// 过期时间7 天
func RedisTaskStatusKey(taskID string) string {
return fmt.Sprintf("task:status:%s", taskID)
}
// RedisShopSubordinatesKey 生成店铺下级 ID 列表的 Redis 键
// 用途:缓存递归查询的下级店铺 ID 列表
// 过期时间30 分钟
func RedisShopSubordinatesKey(shopID uint) string {
return fmt.Sprintf("shop:subordinates:%d", shopID)
}
// RedisVerificationCodeKey 生成验证码的 Redis 键
// 用途:存储手机验证码
// 过期时间5 分钟
func RedisVerificationCodeKey(phone string) string {
return fmt.Sprintf("verification:code:%s", phone)
}
// RedisVerificationCodeLimitKey 生成验证码发送频率限制的 Redis 键
// 用途:限制验证码发送频率
// 过期时间60 秒
func RedisVerificationCodeLimitKey(phone string) string {
return fmt.Sprintf("verification:limit:%s", phone)
}
// ========================================
// 钱包相关 Redis Key
// ========================================
// RedisWalletLockKey 生成钱包操作锁的 Redis 键
// 用途:钱包余额变更时的分布式锁,防止并发冲突
// 过期时间10 秒
func RedisWalletLockKey(walletID uint) string {
return fmt.Sprintf("wallet:lock:%d", walletID)
}
// RedisWalletBalanceKey 生成钱包余额缓存的 Redis 键
// 用途:缓存钱包余额,减少数据库查询
// 过期时间5 分钟
func RedisWalletBalanceKey(walletID uint) string {
return fmt.Sprintf("wallet:balance:%d", walletID)
}
// RedisRechargeOrderKey 生成充值订单缓存的 Redis 键
// 用途:充值订单状态查询缓存
// 过期时间1 小时
func RedisRechargeOrderKey(rechargeNo string) string {
return fmt.Sprintf("recharge:order:%s", rechargeNo)
}
// RedisWalletTransactionKey 生成钱包交易幂等性的 Redis 键
// 用途:防止重复交易
// 过期时间24 小时
func RedisWalletTransactionKey(requestID string) string {
return fmt.Sprintf("wallet:transaction:%s", requestID)
}
// ========================================
// 标签相关 Redis Key
// ========================================
// RedisTagCacheKey 生成标签缓存的 Redis 键
// 用途:缓存热门标签列表
// 过期时间1 小时
func RedisTagCacheKey() string {
return "tag:cache:list"
}
// RedisResourceTagsKey 生成资源标签关联缓存的 Redis 键
// 用途:缓存资源的标签列表
// 过期时间30 分钟
func RedisResourceTagsKey(resourceType string, resourceID uint) string {
return fmt.Sprintf("resource:tags:%s:%d", resourceType, resourceID)
}
// ========================================
// 权限相关 Redis Key
// ========================================
// RedisUserPermissionsKey 生成用户权限列表缓存的 Redis 键
// 用途:缓存用户的所有权限列表(包含 permCode 和 platform 信息)
// 格式JSON 数组 [{"perm_code":"user:list","platform":"web"},...]
// 过期时间30 分钟
func RedisUserPermissionsKey(userID uint) string {
return fmt.Sprintf("permission:user:%d:list", userID)
}
// ========================================
// 佣金统计相关 Redis Key
// ========================================
// RedisCommissionStatsKey 生成佣金统计缓存的 Redis 键
// 用途缓存梯度返佣统计数据Hash 结构: total_count, total_amount
// 过期时间:周期结束后 7 天
func RedisCommissionStatsKey(allocationID uint, period string) string {
return fmt.Sprintf("commission:stats:%d:%s", allocationID, period)
}
// RedisCommissionStatsLockKey 生成佣金统计同步锁的 Redis 键
// 用途:定时同步任务的分布式锁,防止并发同步
// 过期时间5 分钟
func RedisCommissionStatsLockKey() string {
return "commission:stats:sync:lock"
}
// ========================================
// 系列分配相关 Redis Key
// ========================================
// RedisShopSeriesAllocationKey 生成店铺系列分配缓存的 Redis 键
// 用途:缓存店铺+系列的分配配置
// 过期时间30 分钟
func RedisShopSeriesAllocationKey(shopID, seriesID uint) string {
return fmt.Sprintf("shop_series_alloc:%d:%d", shopID, seriesID)
}
// ========================================
// 轮询系统相关 Redis Key
// ========================================
// RedisPollingQueueRealnameKey 生成实名检查轮询队列的 Redis 键
// 用途Sorted Set 存储待检查实名状态的卡Score 为下次检查的 Unix 时间戳
// 过期时间:无(持久化数据)
func RedisPollingQueueRealnameKey() string {
return "polling:queue:realname"
}
// RedisPollingQueueCarddataKey 生成卡流量检查轮询队列的 Redis 键
// 用途Sorted Set 存储待检查流量的卡Score 为下次检查的 Unix 时间戳
// 过期时间:无(持久化数据)
func RedisPollingQueueCarddataKey() string {
return "polling:queue:carddata"
}
// RedisPollingQueuePackageKey 生成套餐检查轮询队列的 Redis 键
// 用途Sorted Set 存储待检查套餐的卡Score 为下次检查的 Unix 时间戳
// 过期时间:无(持久化数据)
func RedisPollingQueuePackageKey() string {
return "polling:queue:package"
}
// RedisPollingCardInfoKey 生成卡信息缓存的 Redis 键
// 用途Hash 存储卡的基本信息和轮询状态
// 过期时间7 天
func RedisPollingCardInfoKey(cardID uint) string {
return fmt.Sprintf("polling:card:%d", cardID)
}
// RedisPollingConfigsCacheKey 生成轮询配置缓存的 Redis 键
// 用途String 存储所有轮询配置JSON 格式)
// 过期时间10 分钟
func RedisPollingConfigsCacheKey() string {
return "polling:configs"
}
// RedisPollingConfigCardsKey 生成配置匹配索引的 Redis 键
// 用途Set 存储匹配该配置的所有卡 ID
// 过期时间1 小时
func RedisPollingConfigCardsKey(configID uint) string {
return fmt.Sprintf("polling:config:cards:%d", configID)
}
// RedisPollingConcurrencyConfigKey 生成并发控制配置的 Redis 键
// 用途String 存储任务类型的最大并发数
// 过期时间:无(持久化配置)
func RedisPollingConcurrencyConfigKey(taskType string) string {
return fmt.Sprintf("polling:concurrency:config:%s", taskType)
}
// RedisPollingConcurrencyCurrentKey 生成并发控制当前值的 Redis 键
// 用途String 存储任务类型的当前并发数(计数器)
// 过期时间:无(实时数据)
func RedisPollingConcurrencyCurrentKey(taskType string) string {
return fmt.Sprintf("polling:concurrency:current:%s", taskType)
}
// RedisPollingManualQueueKey 生成手动触发队列的 Redis 键
// 用途List 存储手动触发的卡 IDFIFO 队列)
// 过期时间:无(临时队列)
func RedisPollingManualQueueKey(taskType string) string {
return fmt.Sprintf("polling:manual:%s", taskType)
}
// RedisPollingManualDedupeKey 生成手动触发去重的 Redis 键
// 用途Set 存储已加入手动触发队列的卡 ID用于去重
// 过期时间1小时
func RedisPollingManualDedupeKey(taskType string) string {
return fmt.Sprintf("polling:manual:dedupe:%s", taskType)
}
// RedisPollingStatsKey 生成监控统计的 Redis 键
// 用途Hash 存储监控指标(成功数、失败数、总耗时等)
// 过期时间:无(持久化统计)
func RedisPollingStatsKey(taskType string) string {
return fmt.Sprintf("polling:stats:%s", taskType)
}
// RedisPollingInitProgressKey 生成初始化进度的 Redis 键
// 用途Hash 存储初始化进度信息(总数、已处理数、状态)
// 过期时间:无(初始化完成后自动删除)
func RedisPollingInitProgressKey() string {
return "polling:init:progress"
}