feat: 实现公共工具包,包括日志、错误处理、数据库和缓存
This commit is contained in:
120
backend/shared/pkg/cache/redis.go
vendored
Normal file
120
backend/shared/pkg/cache/redis.go
vendored
Normal file
@@ -0,0 +1,120 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"backend/shared/pkg/logger"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
type RedisConfig struct {
|
||||
Addr string
|
||||
Password string
|
||||
DB int
|
||||
}
|
||||
|
||||
type RedisCache struct {
|
||||
Client *redis.Client
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
// NewRedisCache 创建新的 Redis 缓存连接
|
||||
func NewRedisCache(config RedisConfig) (*RedisCache, error) {
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: config.Addr,
|
||||
Password: config.Password,
|
||||
DB: config.DB,
|
||||
})
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// 测试连接
|
||||
if _, err := client.Ping(ctx).Result(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logger.Info("Connected to Redis cache")
|
||||
|
||||
return &RedisCache{
|
||||
Client: client,
|
||||
ctx: ctx,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Close 关闭 Redis 连接
|
||||
func (r *RedisCache) Close() error {
|
||||
if r.Client != nil {
|
||||
return r.Client.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Set 设置缓存
|
||||
func (r *RedisCache) Set(key string, value interface{}, expiration time.Duration) error {
|
||||
return r.Client.Set(r.ctx, key, value, expiration).Err()
|
||||
}
|
||||
|
||||
// Get 获取缓存
|
||||
func (r *RedisCache) Get(key string) (string, error) {
|
||||
return r.Client.Get(r.ctx, key).Result()
|
||||
}
|
||||
|
||||
// Delete 删除缓存
|
||||
func (r *RedisCache) Delete(key string) error {
|
||||
return r.Client.Del(r.ctx, key).Err()
|
||||
}
|
||||
|
||||
// Exists 检查键是否存在
|
||||
func (r *RedisCache) Exists(key string) (bool, error) {
|
||||
result, err := r.Client.Exists(r.ctx, key).Result()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return result > 0, nil
|
||||
}
|
||||
|
||||
// Expire 设置键的过期时间
|
||||
func (r *RedisCache) Expire(key string, expiration time.Duration) error {
|
||||
return r.Client.Expire(r.ctx, key, expiration).Err()
|
||||
}
|
||||
|
||||
// TTL 获取键的剩余过期时间
|
||||
func (r *RedisCache) TTL(key string) (time.Duration, error) {
|
||||
return r.Client.TTL(r.ctx, key).Result()
|
||||
}
|
||||
|
||||
// Incr 递增键的值
|
||||
func (r *RedisCache) Incr(key string) (int64, error) {
|
||||
return r.Client.Incr(r.ctx, key).Result()
|
||||
}
|
||||
|
||||
// Decr 递减键的值
|
||||
func (r *RedisCache) Decr(key string) (int64, error) {
|
||||
return r.Client.Decr(r.ctx, key).Result()
|
||||
}
|
||||
|
||||
// HashSet 设置哈希表字段
|
||||
func (r *RedisCache) HashSet(key, field string, value interface{}) error {
|
||||
return r.Client.HSet(r.ctx, key, field, value).Err()
|
||||
}
|
||||
|
||||
// HashGet 获取哈希表字段
|
||||
func (r *RedisCache) HashGet(key, field string) (string, error) {
|
||||
return r.Client.HGet(r.ctx, key, field).Result()
|
||||
}
|
||||
|
||||
// HashDelete 删除哈希表字段
|
||||
func (r *RedisCache) HashDelete(key string, fields ...string) error {
|
||||
return r.Client.HDel(r.ctx, key, fields...).Err()
|
||||
}
|
||||
|
||||
// HashExists 检查哈希表字段是否存在
|
||||
func (r *RedisCache) HashExists(key, field string) (bool, error) {
|
||||
result, err := r.Client.HExists(r.ctx, key, field).Result()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user