Files
new-api/common/redis.go

108 lines
2.5 KiB
Go
Raw Normal View History

2023-04-22 20:39:27 +08:00
package common
import (
"context"
"github.com/go-redis/redis/v8"
"os"
"time"
)
var RDB *redis.Client
var RedisEnabled = true
// InitRedisClient This function is called after init()
func InitRedisClient() (err error) {
if os.Getenv("REDIS_CONN_STRING") == "" {
RedisEnabled = false
SysLog("REDIS_CONN_STRING not set, Redis is not enabled")
return nil
}
if os.Getenv("SYNC_FREQUENCY") == "" {
2023-06-22 20:12:33 +08:00
RedisEnabled = false
SysLog("SYNC_FREQUENCY not set, Redis is disabled")
2023-06-22 20:12:33 +08:00
return nil
}
2023-06-22 01:12:28 +08:00
SysLog("Redis is enabled")
2023-04-22 20:39:27 +08:00
opt, err := redis.ParseURL(os.Getenv("REDIS_CONN_STRING"))
if err != nil {
2023-06-22 10:59:01 +08:00
FatalLog("failed to parse Redis connection string: " + err.Error())
2023-04-22 20:39:27 +08:00
}
RDB = redis.NewClient(opt)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err = RDB.Ping(ctx).Result()
2023-06-22 10:59:01 +08:00
if err != nil {
FatalLog("Redis ping test failed: " + err.Error())
}
2023-04-22 20:39:27 +08:00
return err
}
func ParseRedisOption() *redis.Options {
opt, err := redis.ParseURL(os.Getenv("REDIS_CONN_STRING"))
if err != nil {
2023-06-22 10:59:01 +08:00
FatalLog("failed to parse Redis connection string: " + err.Error())
2023-04-22 20:39:27 +08:00
}
return opt
}
func RedisSet(key string, value string, expiration time.Duration) error {
ctx := context.Background()
return RDB.Set(ctx, key, value, expiration).Err()
}
func RedisGet(key string) (string, error) {
ctx := context.Background()
return RDB.Get(ctx, key).Result()
}
2024-01-26 15:52:23 +08:00
func RedisExpire(key string, expiration time.Duration) error {
ctx := context.Background()
return RDB.Expire(ctx, key, expiration).Err()
}
func RedisGetEx(key string, expiration time.Duration) (string, error) {
ctx := context.Background()
return RDB.GetSet(ctx, key, expiration).Result()
}
func RedisDel(key string) error {
ctx := context.Background()
return RDB.Del(ctx, key).Err()
}
func RedisDecrease(key string, value int64) error {
// 检查键的剩余生存时间
ttlCmd := RDB.TTL(context.Background(), key)
ttl, err := ttlCmd.Result()
if err != nil {
// 失败则尝试直接减少
return RDB.DecrBy(context.Background(), key, value).Err()
}
// 如果剩余生存时间大于0则进行减少操作
if ttl > 0 {
ctx := context.Background()
// 开始一个Redis事务
txn := RDB.TxPipeline()
// 减少余额
decrCmd := txn.DecrBy(ctx, key, value)
if err := decrCmd.Err(); err != nil {
return err // 如果减少失败,则直接返回错误
}
// 重新设置过期时间,使用原来的过期时间
txn.Expire(ctx, key, ttl)
// 执行事务
_, err = txn.Exec(ctx)
return err
} else {
_ = RedisDel(key)
}
return nil
}