代码拉取完成,页面将自动刷新
package cache
import (
"encoding/json"
"time"
"gitee.com/vgos/plugin/database"
"github.com/go-redis/redis"
"github.com/pkg/errors"
)
type RedisCache struct {
client *redis.Client
}
func NewRedisCache(conf string) *RedisCache {
instance := &RedisCache{}
instance.client = database.RedisGet(conf)
return instance
}
func (r *RedisCache) Set(key string, value interface{}, ttl time.Duration) error {
return r.client.Set(key, value, ttl).Err()
}
func (r *RedisCache) Get(key string) string {
return r.client.Get(key).String()
}
func (r *RedisCache) GetInt(key string) (int, error) {
return r.client.Get(key).Int()
}
func (r *RedisCache) GetTtl(key string) time.Duration {
return r.client.TTL(key).Val()
}
func (r *RedisCache) SetTtl(key string, ttl time.Duration) bool {
return r.client.Expire(key, ttl).Val()
}
func (r *RedisCache) Remove(key string) int {
i := r.client.Del(key).Val()
return int(i)
}
func (r *RedisCache) Forever(key string, value interface{}) error {
return r.client.Set(key, value, 0).Err()
}
func (r *RedisCache) SetStruct(k string, d interface{}, ttl time.Duration) error {
jsonStr, err := json.Marshal(d)
if err != nil {
return errors.Errorf("save key `%s` json marshal error", k)
}
err = r.Set(k, string(jsonStr), ttl)
if err != nil {
return errors.Errorf("save key `%s` fail:%s", k, err.Error())
}
return nil
}
func (r *RedisCache) GetStruct(k string, data interface{}) error {
valStr, err := r.client.Get(k).Result()
if redis.Nil == err {
return errors.Errorf("key `%s` not found", k)
}
if err != nil {
return errors.Errorf("get key `%s` fail:%s", k, err.Error())
}
return json.Unmarshal([]byte(valStr), &data)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。