Initial commit
This commit is contained in:
Vendored
+75
@@ -0,0 +1,75 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"pet-house.com/core/server/viper_server"
|
||||
)
|
||||
|
||||
// InitConfig initialize redis's config file
|
||||
func InitConfig() error {
|
||||
var cover string
|
||||
if IsExist() {
|
||||
fmt.Println("Your redis config is initialized , reinitialized redis will cover your redis config.")
|
||||
fmt.Println("Did you want to do it ? [Y/N]")
|
||||
fmt.Scanln(&cover)
|
||||
switch strings.ToUpper(cover) {
|
||||
case "Y":
|
||||
case "N":
|
||||
return nil
|
||||
default:
|
||||
}
|
||||
} else {
|
||||
fmt.Println("Redis config file is not exist!")
|
||||
}
|
||||
|
||||
err := Remove()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = initConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println("redis initialized finished!")
|
||||
return nil
|
||||
}
|
||||
|
||||
func initConfig() error {
|
||||
var addr, dbPwd string
|
||||
var db, poolSize int
|
||||
fmt.Println("Please input your redis addr: ")
|
||||
fmt.Printf("Redis addr default is '%s'\n", CONFIG.Addr)
|
||||
fmt.Scanln(&addr)
|
||||
if addr != "" {
|
||||
CONFIG.Addr = addr
|
||||
}
|
||||
|
||||
fmt.Println("Please input your redis db: ")
|
||||
fmt.Printf("Redis db default is '%d'\n", CONFIG.DB)
|
||||
fmt.Scanln(&db)
|
||||
if db > 0 {
|
||||
CONFIG.DB = db
|
||||
}
|
||||
|
||||
fmt.Println("Please input your redis password: ")
|
||||
fmt.Printf("Redis password default is '%s'\n", CONFIG.Password)
|
||||
fmt.Scanln(&dbPwd)
|
||||
if dbPwd != "" {
|
||||
CONFIG.Password = dbPwd
|
||||
}
|
||||
|
||||
fmt.Println("Please input your redis pool size: ")
|
||||
fmt.Scanln(&poolSize)
|
||||
if poolSize > 0 {
|
||||
CONFIG.PoolSize = poolSize
|
||||
}
|
||||
viper_server.Init(getViperConfig())
|
||||
if Instance() == nil {
|
||||
return ErrRedisInit
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Vendored
+27
@@ -0,0 +1,27 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"pet-house.com/core/g"
|
||||
"pet-house.com/core/server/zap_server"
|
||||
)
|
||||
|
||||
func TestInitConfig(t *testing.T) {
|
||||
defer zap_server.Remove()
|
||||
t.Run("test redis's file initialize", func(t *testing.T) {
|
||||
CONFIG.Password = g.TestRedisPwd
|
||||
err := InitConfig()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if !IsExist() {
|
||||
t.Errorf("config's files is not exist.")
|
||||
return
|
||||
}
|
||||
if err := Remove(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
Vendored
+70
@@ -0,0 +1,70 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"pet-house.com/core/g"
|
||||
"pet-house.com/core/server/viper_server"
|
||||
)
|
||||
|
||||
var CONFIG = Redis{
|
||||
DB: 0,
|
||||
Addr: "127.0.0.1:6379",
|
||||
Password: "",
|
||||
PoolSize: 0,
|
||||
}
|
||||
|
||||
type Redis struct {
|
||||
DB int `mapstructure:"db" json:"db" yaml:"db"`
|
||||
Addr string `mapstructure:"addr" json:"addr" yaml:"addr"`
|
||||
Password string `mapstructure:"password" json:"password" yaml:"password"`
|
||||
PoolSize int `mapstructure:"pool-size" json:"pool-size" yaml:"pool-size"`
|
||||
}
|
||||
|
||||
// IsExist config file is exist
|
||||
func IsExist() bool {
|
||||
return getViperConfig().IsFileExist()
|
||||
}
|
||||
|
||||
// Remove remove config file
|
||||
func Remove() error {
|
||||
return getViperConfig().Remove()
|
||||
}
|
||||
|
||||
// Recover
|
||||
func Recover() error {
|
||||
b, err := json.Marshal(CONFIG)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return getViperConfig().Recover(b)
|
||||
}
|
||||
|
||||
// getViperConfig get viper config
|
||||
func getViperConfig() viper_server.ViperConfig {
|
||||
configName := "redis"
|
||||
return viper_server.ViperConfig{
|
||||
Debug: true,
|
||||
Directory: g.ConfigDir,
|
||||
Name: configName,
|
||||
Type: g.ConfigType,
|
||||
Watch: func(vi *viper.Viper) error {
|
||||
if err := vi.Unmarshal(&CONFIG); err != nil {
|
||||
return fmt.Errorf("get Unarshal error: %v", err)
|
||||
}
|
||||
// watch config file change
|
||||
vi.SetConfigName(configName)
|
||||
return nil
|
||||
},
|
||||
Default: []byte(`
|
||||
{
|
||||
"db": ` + strconv.FormatInt(int64(CONFIG.DB), 10) + `,
|
||||
"addr": "` + CONFIG.Addr + `",
|
||||
"password": "` + CONFIG.Password + `",
|
||||
"pool-size": ` + strconv.FormatInt(int64(CONFIG.PoolSize), 10) + `
|
||||
}`),
|
||||
}
|
||||
}
|
||||
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsExist(t *testing.T) {
|
||||
t.Run("Test Remove function", func(t *testing.T) {
|
||||
if err := Remove(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if IsExist() {
|
||||
t.Errorf("config's files remove is fail.")
|
||||
}
|
||||
})
|
||||
}
|
||||
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()
|
||||
}
|
||||
Vendored
+87
@@ -0,0 +1,87 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"pet-house.com/core/g"
|
||||
"pet-house.com/core/server/zap_server"
|
||||
)
|
||||
|
||||
func TestSetCacheString(t *testing.T) {
|
||||
defer Remove()
|
||||
defer zap_server.Remove()
|
||||
CONFIG.Password = g.TestRedisPwd
|
||||
t.Run("test set cache string", func(t *testing.T) {
|
||||
key := "test_set_cache"
|
||||
want := "test_set_cache_value"
|
||||
err := SetCache(key, want, time.Duration(time.Second*3))
|
||||
if err != nil {
|
||||
t.Errorf("set cache get error %v\n", err)
|
||||
}
|
||||
get, err := GetCacheString(key)
|
||||
if err != nil {
|
||||
t.Errorf("set cache get error %v\n", err)
|
||||
}
|
||||
if get != want {
|
||||
t.Errorf("set cache want [%s] but get [%s]\n", want, get)
|
||||
}
|
||||
time.Sleep(time.Second * 5)
|
||||
_, err = GetCacheString(key)
|
||||
if err == nil {
|
||||
t.Error("set cache want error but get nil\n")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestSetCacheUint(t *testing.T) {
|
||||
defer Remove()
|
||||
defer zap_server.Remove()
|
||||
CONFIG.Password = g.TestRedisPwd
|
||||
t.Run("test set cache uint", func(t *testing.T) {
|
||||
key := "test_set_cache"
|
||||
var want uint64 = 123
|
||||
err := SetCache(key, want, time.Duration(time.Second*3))
|
||||
if err != nil {
|
||||
t.Errorf("set cache get error %v\n", err)
|
||||
}
|
||||
get, err := GetCacheUint(key)
|
||||
if err != nil {
|
||||
t.Errorf("set cache get error %v\n", err)
|
||||
}
|
||||
if get != want {
|
||||
t.Errorf("set cache want [%d] but get [%d]\n", want, get)
|
||||
}
|
||||
time.Sleep(time.Second * 5)
|
||||
data, err := GetCacheUint(key)
|
||||
if err == nil && data > 0 {
|
||||
t.Error("set cache want error but get nil\n")
|
||||
}
|
||||
})
|
||||
}
|
||||
func TestSetCacheBytes(t *testing.T) {
|
||||
defer Remove()
|
||||
defer zap_server.Remove()
|
||||
CONFIG.Password = g.TestRedisPwd
|
||||
t.Run("test set cache bytes", func(t *testing.T) {
|
||||
key := "test_set_cache"
|
||||
want := []byte("test_set_cache_value")
|
||||
err := SetCache(key, want, time.Duration(time.Second*3))
|
||||
if err != nil {
|
||||
t.Errorf("set cache get error %v\n", err)
|
||||
}
|
||||
get, err := GetCacheBytes(key)
|
||||
if err != nil {
|
||||
t.Errorf("set cache get error %v\n", err)
|
||||
}
|
||||
if !reflect.DeepEqual(get, want) {
|
||||
t.Errorf("set cache want [%s] but get [%s]\n", want, get)
|
||||
}
|
||||
time.Sleep(time.Second * 10)
|
||||
_, err = GetCacheBytes(key)
|
||||
if err == nil {
|
||||
t.Error("set cache want error but get nil\n")
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user