|
@@ -1,4 +1,4 @@
|
|
|
-package redis
|
|
|
|
|
|
|
+package cache
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
"context"
|
|
"context"
|
|
@@ -29,13 +29,6 @@ var CONFIG = Redis{
|
|
|
PoolSize: 0,
|
|
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"`
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
func init() {
|
|
func init() {
|
|
|
if Instance() == nil {
|
|
if Instance() == nil {
|
|
|
log.Fatal(ErrRedisInit)
|
|
log.Fatal(ErrRedisInit)
|
|
@@ -56,38 +49,6 @@ func Instance() redis.UniversalClient {
|
|
|
return cacheClient
|
|
return cacheClient
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-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
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func GetCache(key string) (interface{}, error) {
|
|
|
|
|
- return Instance().Get(context.Background(), key).Result()
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func DeleteCache(key string) (int64, error) {
|
|
|
|
|
- return Instance().Del(context.Background(), key).Result()
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func GetCacheString(key string) (string, error) {
|
|
|
|
|
- value, err := GetCacheBytes(key)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return "", err
|
|
|
|
|
- }
|
|
|
|
|
- return string(value), nil
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func GetCacheBytes(key string) ([]byte, error) {
|
|
|
|
|
- return Instance().Get(context.Background(), key).Bytes()
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func GetCacheUint(key string) (uint64, error) {
|
|
|
|
|
- return Instance().Get(context.Background(), key).Uint64()
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
func SetUserData(userId int64, userData *sysViewModel.UserData) error {
|
|
func SetUserData(userId int64, userData *sysViewModel.UserData) error {
|
|
|
keyStr := fmt.Sprintf("user_%d", userId)
|
|
keyStr := fmt.Sprintf("user_%d", userId)
|
|
|
return SetCache(keyStr, userData, 0)
|
|
return SetCache(keyStr, userData, 0)
|