2025-12-19 23:39:28 +08:00
|
|
|
|
package repository
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"log"
|
2026-02-07 22:13:45 +08:00
|
|
|
|
"math/rand/v2"
|
2025-12-19 23:39:28 +08:00
|
|
|
|
"strconv"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
2025-12-25 17:15:01 +08:00
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
2025-12-19 23:39:28 +08:00
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
2026-03-03 15:25:44 +08:00
|
|
|
|
billingBalanceKeyPrefix = "billing:balance:"
|
|
|
|
|
|
billingSubKeyPrefix = "billing:sub:"
|
2026-03-03 15:01:10 +08:00
|
|
|
|
billingRateLimitKeyPrefix = "apikey:rate:"
|
2026-03-03 15:25:44 +08:00
|
|
|
|
billingCacheTTL = 5 * time.Minute
|
|
|
|
|
|
billingCacheJitter = 30 * time.Second
|
|
|
|
|
|
rateLimitCacheTTL = 7 * 24 * time.Hour // 7 days matches the longest window
|
2025-12-19 23:39:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2026-02-07 19:46:42 +08:00
|
|
|
|
// jitteredTTL 返回带随机抖动的 TTL,防止缓存雪崩
|
|
|
|
|
|
func jitteredTTL() time.Duration {
|
2026-02-07 21:18:03 +08:00
|
|
|
|
// 只做“减法抖动”,确保实际 TTL 不会超过 billingCacheTTL(避免上界预期被打破)。
|
|
|
|
|
|
if billingCacheJitter <= 0 {
|
|
|
|
|
|
return billingCacheTTL
|
|
|
|
|
|
}
|
2026-02-07 22:13:45 +08:00
|
|
|
|
jitter := time.Duration(rand.IntN(int(billingCacheJitter)))
|
2026-02-07 21:18:03 +08:00
|
|
|
|
return billingCacheTTL - jitter
|
2026-02-07 19:46:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-26 16:33:20 +08:00
|
|
|
|
// billingBalanceKey generates the Redis key for user balance cache.
|
|
|
|
|
|
func billingBalanceKey(userID int64) string {
|
|
|
|
|
|
return fmt.Sprintf("%s%d", billingBalanceKeyPrefix, userID)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// billingSubKey generates the Redis key for subscription cache.
|
|
|
|
|
|
func billingSubKey(userID, groupID int64) string {
|
|
|
|
|
|
return fmt.Sprintf("%s%d:%d", billingSubKeyPrefix, userID, groupID)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-19 23:39:28 +08:00
|
|
|
|
const (
|
|
|
|
|
|
subFieldStatus = "status"
|
|
|
|
|
|
subFieldExpiresAt = "expires_at"
|
|
|
|
|
|
subFieldDailyUsage = "daily_usage"
|
|
|
|
|
|
subFieldWeeklyUsage = "weekly_usage"
|
|
|
|
|
|
subFieldMonthlyUsage = "monthly_usage"
|
|
|
|
|
|
subFieldVersion = "version"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-03-03 15:01:10 +08:00
|
|
|
|
// billingRateLimitKey generates the Redis key for API key rate limit cache.
|
|
|
|
|
|
func billingRateLimitKey(keyID int64) string {
|
|
|
|
|
|
return fmt.Sprintf("%s%d", billingRateLimitKeyPrefix, keyID)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
rateLimitFieldUsage5h = "usage_5h"
|
|
|
|
|
|
rateLimitFieldUsage1d = "usage_1d"
|
|
|
|
|
|
rateLimitFieldUsage7d = "usage_7d"
|
|
|
|
|
|
rateLimitFieldWindow5h = "window_5h"
|
|
|
|
|
|
rateLimitFieldWindow1d = "window_1d"
|
|
|
|
|
|
rateLimitFieldWindow7d = "window_7d"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-12-19 23:39:28 +08:00
|
|
|
|
var (
|
|
|
|
|
|
deductBalanceScript = redis.NewScript(`
|
|
|
|
|
|
local current = redis.call('GET', KEYS[1])
|
|
|
|
|
|
if current == false then
|
|
|
|
|
|
return 0
|
|
|
|
|
|
end
|
|
|
|
|
|
local newVal = tonumber(current) - tonumber(ARGV[1])
|
|
|
|
|
|
redis.call('SET', KEYS[1], newVal)
|
|
|
|
|
|
redis.call('EXPIRE', KEYS[1], ARGV[2])
|
|
|
|
|
|
return 1
|
|
|
|
|
|
`)
|
|
|
|
|
|
|
|
|
|
|
|
updateSubUsageScript = redis.NewScript(`
|
|
|
|
|
|
local exists = redis.call('EXISTS', KEYS[1])
|
|
|
|
|
|
if exists == 0 then
|
|
|
|
|
|
return 0
|
|
|
|
|
|
end
|
|
|
|
|
|
local cost = tonumber(ARGV[1])
|
|
|
|
|
|
redis.call('HINCRBYFLOAT', KEYS[1], 'daily_usage', cost)
|
|
|
|
|
|
redis.call('HINCRBYFLOAT', KEYS[1], 'weekly_usage', cost)
|
|
|
|
|
|
redis.call('HINCRBYFLOAT', KEYS[1], 'monthly_usage', cost)
|
|
|
|
|
|
redis.call('EXPIRE', KEYS[1], ARGV[2])
|
|
|
|
|
|
return 1
|
|
|
|
|
|
`)
|
2026-03-03 15:01:10 +08:00
|
|
|
|
|
|
|
|
|
|
// updateRateLimitUsageScript atomically increments all three rate limit usage counters.
|
|
|
|
|
|
// Returns 0 if the key doesn't exist (cache miss), 1 on success.
|
|
|
|
|
|
updateRateLimitUsageScript = redis.NewScript(`
|
|
|
|
|
|
local exists = redis.call('EXISTS', KEYS[1])
|
|
|
|
|
|
if exists == 0 then
|
|
|
|
|
|
return 0
|
|
|
|
|
|
end
|
|
|
|
|
|
local cost = tonumber(ARGV[1])
|
|
|
|
|
|
redis.call('HINCRBYFLOAT', KEYS[1], 'usage_5h', cost)
|
|
|
|
|
|
redis.call('HINCRBYFLOAT', KEYS[1], 'usage_1d', cost)
|
|
|
|
|
|
redis.call('HINCRBYFLOAT', KEYS[1], 'usage_7d', cost)
|
|
|
|
|
|
redis.call('EXPIRE', KEYS[1], ARGV[2])
|
|
|
|
|
|
return 1
|
|
|
|
|
|
`)
|
2025-12-19 23:39:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type billingCache struct {
|
|
|
|
|
|
rdb *redis.Client
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-25 17:15:01 +08:00
|
|
|
|
func NewBillingCache(rdb *redis.Client) service.BillingCache {
|
2025-12-19 23:39:28 +08:00
|
|
|
|
return &billingCache{rdb: rdb}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *billingCache) GetUserBalance(ctx context.Context, userID int64) (float64, error) {
|
2025-12-26 16:33:20 +08:00
|
|
|
|
key := billingBalanceKey(userID)
|
2025-12-19 23:39:28 +08:00
|
|
|
|
val, err := c.rdb.Get(ctx, key).Result()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return 0, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return strconv.ParseFloat(val, 64)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *billingCache) SetUserBalance(ctx context.Context, userID int64, balance float64) error {
|
2025-12-26 16:33:20 +08:00
|
|
|
|
key := billingBalanceKey(userID)
|
2026-02-07 19:46:42 +08:00
|
|
|
|
return c.rdb.Set(ctx, key, balance, jitteredTTL()).Err()
|
2025-12-19 23:39:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *billingCache) DeductUserBalance(ctx context.Context, userID int64, amount float64) error {
|
2025-12-26 16:33:20 +08:00
|
|
|
|
key := billingBalanceKey(userID)
|
2026-02-07 19:46:42 +08:00
|
|
|
|
_, err := deductBalanceScript.Run(ctx, c.rdb, []string{key}, amount, int(jitteredTTL().Seconds())).Result()
|
2025-12-19 23:39:28 +08:00
|
|
|
|
if err != nil && !errors.Is(err, redis.Nil) {
|
|
|
|
|
|
log.Printf("Warning: deduct balance cache failed for user %d: %v", userID, err)
|
2026-02-07 19:46:42 +08:00
|
|
|
|
return err
|
2025-12-19 23:39:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *billingCache) InvalidateUserBalance(ctx context.Context, userID int64) error {
|
2025-12-26 16:33:20 +08:00
|
|
|
|
key := billingBalanceKey(userID)
|
2025-12-19 23:39:28 +08:00
|
|
|
|
return c.rdb.Del(ctx, key).Err()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-25 17:15:01 +08:00
|
|
|
|
func (c *billingCache) GetSubscriptionCache(ctx context.Context, userID, groupID int64) (*service.SubscriptionCacheData, error) {
|
2025-12-26 16:33:20 +08:00
|
|
|
|
key := billingSubKey(userID, groupID)
|
2025-12-19 23:39:28 +08:00
|
|
|
|
result, err := c.rdb.HGetAll(ctx, key).Result()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(result) == 0 {
|
|
|
|
|
|
return nil, redis.Nil
|
|
|
|
|
|
}
|
|
|
|
|
|
return c.parseSubscriptionCache(result)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-25 17:15:01 +08:00
|
|
|
|
func (c *billingCache) parseSubscriptionCache(data map[string]string) (*service.SubscriptionCacheData, error) {
|
|
|
|
|
|
result := &service.SubscriptionCacheData{}
|
2025-12-19 23:39:28 +08:00
|
|
|
|
|
|
|
|
|
|
result.Status = data[subFieldStatus]
|
|
|
|
|
|
if result.Status == "" {
|
|
|
|
|
|
return nil, errors.New("invalid cache: missing status")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if expiresStr, ok := data[subFieldExpiresAt]; ok {
|
|
|
|
|
|
expiresAt, err := strconv.ParseInt(expiresStr, 10, 64)
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
result.ExpiresAt = time.Unix(expiresAt, 0)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if dailyStr, ok := data[subFieldDailyUsage]; ok {
|
|
|
|
|
|
result.DailyUsage, _ = strconv.ParseFloat(dailyStr, 64)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if weeklyStr, ok := data[subFieldWeeklyUsage]; ok {
|
|
|
|
|
|
result.WeeklyUsage, _ = strconv.ParseFloat(weeklyStr, 64)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if monthlyStr, ok := data[subFieldMonthlyUsage]; ok {
|
|
|
|
|
|
result.MonthlyUsage, _ = strconv.ParseFloat(monthlyStr, 64)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if versionStr, ok := data[subFieldVersion]; ok {
|
|
|
|
|
|
result.Version, _ = strconv.ParseInt(versionStr, 10, 64)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-25 17:15:01 +08:00
|
|
|
|
func (c *billingCache) SetSubscriptionCache(ctx context.Context, userID, groupID int64, data *service.SubscriptionCacheData) error {
|
2025-12-19 23:39:28 +08:00
|
|
|
|
if data == nil {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-26 16:33:20 +08:00
|
|
|
|
key := billingSubKey(userID, groupID)
|
2025-12-19 23:39:28 +08:00
|
|
|
|
|
2025-12-20 16:19:40 +08:00
|
|
|
|
fields := map[string]any{
|
2025-12-19 23:39:28 +08:00
|
|
|
|
subFieldStatus: data.Status,
|
|
|
|
|
|
subFieldExpiresAt: data.ExpiresAt.Unix(),
|
|
|
|
|
|
subFieldDailyUsage: data.DailyUsage,
|
|
|
|
|
|
subFieldWeeklyUsage: data.WeeklyUsage,
|
|
|
|
|
|
subFieldMonthlyUsage: data.MonthlyUsage,
|
|
|
|
|
|
subFieldVersion: data.Version,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pipe := c.rdb.Pipeline()
|
|
|
|
|
|
pipe.HSet(ctx, key, fields)
|
2026-02-07 19:46:42 +08:00
|
|
|
|
pipe.Expire(ctx, key, jitteredTTL())
|
2025-12-19 23:39:28 +08:00
|
|
|
|
_, err := pipe.Exec(ctx)
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *billingCache) UpdateSubscriptionUsage(ctx context.Context, userID, groupID int64, cost float64) error {
|
2025-12-26 16:33:20 +08:00
|
|
|
|
key := billingSubKey(userID, groupID)
|
2026-02-07 19:46:42 +08:00
|
|
|
|
_, err := updateSubUsageScript.Run(ctx, c.rdb, []string{key}, cost, int(jitteredTTL().Seconds())).Result()
|
2025-12-19 23:39:28 +08:00
|
|
|
|
if err != nil && !errors.Is(err, redis.Nil) {
|
|
|
|
|
|
log.Printf("Warning: update subscription usage cache failed for user %d group %d: %v", userID, groupID, err)
|
2026-02-07 19:46:42 +08:00
|
|
|
|
return err
|
2025-12-19 23:39:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *billingCache) InvalidateSubscriptionCache(ctx context.Context, userID, groupID int64) error {
|
2025-12-26 16:33:20 +08:00
|
|
|
|
key := billingSubKey(userID, groupID)
|
2025-12-19 23:39:28 +08:00
|
|
|
|
return c.rdb.Del(ctx, key).Err()
|
|
|
|
|
|
}
|
2026-03-03 15:01:10 +08:00
|
|
|
|
|
|
|
|
|
|
func (c *billingCache) GetAPIKeyRateLimit(ctx context.Context, keyID int64) (*service.APIKeyRateLimitCacheData, error) {
|
|
|
|
|
|
key := billingRateLimitKey(keyID)
|
|
|
|
|
|
result, err := c.rdb.HGetAll(ctx, key).Result()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(result) == 0 {
|
|
|
|
|
|
return nil, redis.Nil
|
|
|
|
|
|
}
|
|
|
|
|
|
data := &service.APIKeyRateLimitCacheData{}
|
|
|
|
|
|
if v, ok := result[rateLimitFieldUsage5h]; ok {
|
|
|
|
|
|
data.Usage5h, _ = strconv.ParseFloat(v, 64)
|
|
|
|
|
|
}
|
|
|
|
|
|
if v, ok := result[rateLimitFieldUsage1d]; ok {
|
|
|
|
|
|
data.Usage1d, _ = strconv.ParseFloat(v, 64)
|
|
|
|
|
|
}
|
|
|
|
|
|
if v, ok := result[rateLimitFieldUsage7d]; ok {
|
|
|
|
|
|
data.Usage7d, _ = strconv.ParseFloat(v, 64)
|
|
|
|
|
|
}
|
|
|
|
|
|
if v, ok := result[rateLimitFieldWindow5h]; ok {
|
|
|
|
|
|
data.Window5h, _ = strconv.ParseInt(v, 10, 64)
|
|
|
|
|
|
}
|
|
|
|
|
|
if v, ok := result[rateLimitFieldWindow1d]; ok {
|
|
|
|
|
|
data.Window1d, _ = strconv.ParseInt(v, 10, 64)
|
|
|
|
|
|
}
|
|
|
|
|
|
if v, ok := result[rateLimitFieldWindow7d]; ok {
|
|
|
|
|
|
data.Window7d, _ = strconv.ParseInt(v, 10, 64)
|
|
|
|
|
|
}
|
|
|
|
|
|
return data, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *billingCache) SetAPIKeyRateLimit(ctx context.Context, keyID int64, data *service.APIKeyRateLimitCacheData) error {
|
|
|
|
|
|
if data == nil {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
key := billingRateLimitKey(keyID)
|
|
|
|
|
|
fields := map[string]any{
|
|
|
|
|
|
rateLimitFieldUsage5h: data.Usage5h,
|
|
|
|
|
|
rateLimitFieldUsage1d: data.Usage1d,
|
|
|
|
|
|
rateLimitFieldUsage7d: data.Usage7d,
|
|
|
|
|
|
rateLimitFieldWindow5h: data.Window5h,
|
|
|
|
|
|
rateLimitFieldWindow1d: data.Window1d,
|
|
|
|
|
|
rateLimitFieldWindow7d: data.Window7d,
|
|
|
|
|
|
}
|
|
|
|
|
|
pipe := c.rdb.Pipeline()
|
|
|
|
|
|
pipe.HSet(ctx, key, fields)
|
|
|
|
|
|
pipe.Expire(ctx, key, rateLimitCacheTTL)
|
|
|
|
|
|
_, err := pipe.Exec(ctx)
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *billingCache) UpdateAPIKeyRateLimitUsage(ctx context.Context, keyID int64, cost float64) error {
|
|
|
|
|
|
key := billingRateLimitKey(keyID)
|
|
|
|
|
|
_, err := updateRateLimitUsageScript.Run(ctx, c.rdb, []string{key}, cost, int(rateLimitCacheTTL.Seconds())).Result()
|
|
|
|
|
|
if err != nil && !errors.Is(err, redis.Nil) {
|
|
|
|
|
|
log.Printf("Warning: update rate limit usage cache failed for api key %d: %v", keyID, err)
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *billingCache) InvalidateAPIKeyRateLimit(ctx context.Context, keyID int64) error {
|
|
|
|
|
|
key := billingRateLimitKey(keyID)
|
|
|
|
|
|
return c.rdb.Del(ctx, key).Err()
|
|
|
|
|
|
}
|