Initial commit
This commit is contained in:
Vendored
+72
@@ -0,0 +1,72 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"pet-house.com/core/server/viper_server"
|
||||
)
|
||||
|
||||
var ErrRedisInit = errors.New("缓存初始化失败")
|
||||
var (
|
||||
once sync.Once
|
||||
cacheClient redis.UniversalClient
|
||||
)
|
||||
|
||||
// init initialize
|
||||
func init() {
|
||||
viper_server.Init(getViperConfig())
|
||||
}
|
||||
|
||||
// Instance get instance
|
||||
func Instance() redis.UniversalClient {
|
||||
once.Do(func() {
|
||||
universalOptions := &redis.UniversalOptions{
|
||||
Addrs: strings.Split(CONFIG.Addr, ","),
|
||||
Password: CONFIG.Password,
|
||||
PoolSize: CONFIG.PoolSize,
|
||||
DialTimeout: 300 * time.Second,
|
||||
DB: CONFIG.DB,
|
||||
}
|
||||
cacheClient = redis.NewUniversalClient(universalOptions)
|
||||
})
|
||||
|
||||
return cacheClient
|
||||
}
|
||||
|
||||
// SetCache
|
||||
func SetCache(key string, value interface{}, expiration time.Duration) error {
|
||||
err := Instance().Set(context.Background(), key, value, expiration).Err()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteCache
|
||||
func DeleteCache(key string) (int64, error) {
|
||||
return Instance().Del(context.Background(), key).Result()
|
||||
}
|
||||
|
||||
// GetCacheString
|
||||
func GetCacheString(key string) (string, error) {
|
||||
value, err := GetCacheBytes(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(value), nil
|
||||
}
|
||||
|
||||
// GetCacheBytes
|
||||
func GetCacheBytes(key string) ([]byte, error) {
|
||||
return Instance().Get(context.Background(), key).Bytes()
|
||||
}
|
||||
|
||||
// GetCacheUint
|
||||
func GetCacheUint(key string) (uint64, error) {
|
||||
return Instance().Get(context.Background(), key).Uint64()
|
||||
}
|
||||
Reference in New Issue
Block a user