refactor: user cache logic

This commit is contained in:
CalciumIon
2024-12-29 16:50:26 +08:00
parent a1b864bc5e
commit ed435e5c8f
20 changed files with 548 additions and 225 deletions

View File

@@ -2,9 +2,11 @@ package common
import (
"context"
"github.com/go-redis/redis/v8"
"fmt"
"os"
"time"
"github.com/go-redis/redis/v8"
)
var RDB *redis.Client
@@ -104,3 +106,21 @@ func RedisDecrease(key string, value int64) error {
}
return nil
}
// RedisIncr Add this function to handle atomic increments
func RedisIncr(key string, delta int) error {
ctx := context.Background()
// 检查键是否存在
exists, err := RDB.Exists(ctx, key).Result()
if err != nil {
return err
}
if exists == 0 {
return fmt.Errorf("key does not exist") // 键不存在,返回错误
}
// 键存在执行INCRBY操作
result := RDB.IncrBy(ctx, key, int64(delta))
return result.Err()
}