feat 初次提交
This commit is contained in:
53
db/redis/client.go
Normal file
53
db/redis/client.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"common/config"
|
||||
"common/log"
|
||||
"context"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"time"
|
||||
)
|
||||
|
||||
var instance *Client
|
||||
|
||||
type Client struct {
|
||||
cli *redis.Client
|
||||
}
|
||||
|
||||
func Init(cfg *config.RedisConfig) error {
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: cfg.Addr,
|
||||
Password: cfg.Password,
|
||||
DB: cfg.DB,
|
||||
})
|
||||
instance = &Client{
|
||||
cli: client,
|
||||
}
|
||||
cacheInstance = &CacheClient{
|
||||
cli: client,
|
||||
logger: log.GetLogger().Named("CACHE"),
|
||||
}
|
||||
|
||||
_, err := client.Ping(context.Background()).Result()
|
||||
return err
|
||||
}
|
||||
|
||||
func Close() error {
|
||||
if instance != nil && instance.cli != nil {
|
||||
return instance.cli.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetClient() *Client {
|
||||
return instance
|
||||
}
|
||||
|
||||
func (c *Client) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.StatusCmd {
|
||||
return c.cli.Set(ctx, key, value, expiration)
|
||||
}
|
||||
|
||||
// Get 获取数据
|
||||
func (c *Client) Get(ctx context.Context, key string) *redis.StringCmd {
|
||||
return c.cli.Get(ctx, key)
|
||||
}
|
||||
Reference in New Issue
Block a user