update redis
continuous-integration/drone/push Build is passing Details

master
李光春 2 years ago
parent 7820e49c36
commit 447278865d

@ -2,6 +2,8 @@ package gocache
import "time" import "time"
const Version = "1.0.1"
var ( var (
DefaultExpiration = time.Minute * 30 // 默认过期时间 DefaultExpiration = time.Minute * 30 // 默认过期时间
) )

@ -44,14 +44,14 @@ func (mc *MemcachedCache) GetInterface(key string, result interface{}) {
} }
// 如果不存在则调用GetterInterface // 如果不存在则调用GetterInterface
get, err := mc.db.Get(key) ret, err := mc.db.Get(key)
if err == memcache.ErrCacheMiss { if err == memcache.ErrCacheMiss {
mc.db.Set(key, []byte(f())) mc.db.Set(key, []byte(f()))
get, _ = mc.db.Get(key) ret, _ = mc.db.Get(key)
} }
err = json.Unmarshal([]byte(get), result) err = json.Unmarshal([]byte(ret), result)
return return
} }

@ -2,84 +2,106 @@ package gocache
import ( import (
"context" "context"
"errors"
"fmt"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
"time" "time"
) )
// Redis https://github.com/go-redis/redis // Redis https://github.com/go-redis/redis
type Redis struct { type Redis struct {
redis *redis.Client // 驱动 db *redis.Client // 驱动
ctx context.Context // 上下文内容 ctx context.Context // 上下文内容
expiration time.Duration // 默认过期时间 expiration time.Duration // 默认过期时间
} }
// NewRedis 实例化 // NewRedis 实例化
func NewRedis(db *redis.Client, expiration time.Duration) *Redis { func NewRedis(addr, password string, dbName int, expiration time.Duration) *Redis {
return &Redis{redis: db, ctx: context.Background(), expiration: expiration} db := redis.NewClient(&redis.Options{
Addr: addr, // 地址
Password: password, // 密码
DB: dbName, // 数据库
PoolSize: 100, // 连接池大小
})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err := db.Ping(ctx).Result()
if err != nil {
panic(errors.New(fmt.Sprintf("连接失败,%s", err.Error())))
}
return &Redis{db: db, ctx: context.Background(), expiration: expiration}
}
// NewRedisDb 实例化
func NewRedisDb(db *redis.Client, expiration time.Duration) *Redis {
return &Redis{db: db, ctx: context.Background(), expiration: expiration}
} }
// Set 设置一个key的值 // Set 设置一个key的值
func (r *Redis) Set(key string, value interface{}, expiration time.Duration) (string, error) { func (r *Redis) Set(key string, value interface{}, expiration time.Duration) (string, error) {
return r.redis.Set(r.ctx, key, value, expiration).Result() return r.db.Set(r.ctx, key, value, expiration).Result()
} }
// SetDefaultExpiration 设置一个key的值使用全局默认过期时间 // SetDefaultExpiration 设置一个key的值使用全局默认过期时间
func (r *Redis) SetDefaultExpiration(key string, value interface{}) (string, error) { func (r *Redis) SetDefaultExpiration(key string, value interface{}) (string, error) {
return r.redis.Set(r.ctx, key, value, r.expiration).Result() return r.db.Set(r.ctx, key, value, r.expiration).Result()
} }
// Get 查询key的值 // Get 查询key的值
func (r *Redis) Get(key string) (string, error) { func (r *Redis) Get(key string) (string, error) {
return r.redis.Get(r.ctx, key).Result() return r.db.Get(r.ctx, key).Result()
} }
// GetSet 设置一个key的值并返回这个key的旧值 // GetSet 设置一个key的值并返回这个key的旧值
func (r *Redis) GetSet(key string, value interface{}) (string, error) { func (r *Redis) GetSet(key string, value interface{}) (string, error) {
return r.redis.GetSet(r.ctx, key, value).Result() return r.db.GetSet(r.ctx, key, value).Result()
} }
// SetNX 如果key不存在则设置这个key的值 // SetNX 如果key不存在则设置这个key的值
func (r *Redis) SetNX(key string, value interface{}, expiration time.Duration) error { func (r *Redis) SetNX(key string, value interface{}, expiration time.Duration) error {
return r.redis.SetNX(r.ctx, key, value, expiration).Err() return r.db.SetNX(r.ctx, key, value, expiration).Err()
} }
// SetNXDefaultExpiration 如果key不存在则设置这个key的值使用全局默认过期时间 // SetNXDefaultExpiration 如果key不存在则设置这个key的值使用全局默认过期时间
func (r *Redis) SetNXDefaultExpiration(key string, value interface{}) error { func (r *Redis) SetNXDefaultExpiration(key string, value interface{}) error {
return r.redis.SetNX(r.ctx, key, value, r.expiration).Err() return r.db.SetNX(r.ctx, key, value, r.expiration).Err()
} }
// MGet 批量查询key的值 // MGet 批量查询key的值
func (r *Redis) MGet(keys ...string) ([]interface{}, error) { func (r *Redis) MGet(keys ...string) ([]interface{}, error) {
return r.redis.MGet(r.ctx, keys...).Result() return r.db.MGet(r.ctx, keys...).Result()
} }
// MSet 批量设置key的值 // MSet 批量设置key的值
// MSet(map[string]interface{}{"key1": "value1", "key2": "value2"}) // MSet(map[string]interface{}{"key1": "value1", "key2": "value2"})
func (r *Redis) MSet(values map[string]interface{}) error { func (r *Redis) MSet(values map[string]interface{}) error {
return r.redis.MSet(r.ctx, values).Err() return r.db.MSet(r.ctx, values).Err()
} }
// Incr 针对一个key的数值进行递增操作 // Incr 针对一个key的数值进行递增操作
func (r *Redis) Incr(key string) (int64, error) { func (r *Redis) Incr(key string) (int64, error) {
return r.redis.Incr(r.ctx, key).Result() return r.db.Incr(r.ctx, key).Result()
} }
// IncrBy 针对一个key的数值进行递增操作指定每次递增多少 // IncrBy 针对一个key的数值进行递增操作指定每次递增多少
func (r *Redis) IncrBy(key string, value int64) (int64, error) { func (r *Redis) IncrBy(key string, value int64) (int64, error) {
return r.redis.IncrBy(r.ctx, key, value).Result() return r.db.IncrBy(r.ctx, key, value).Result()
} }
// Decr 针对一个key的数值进行递减操作 // Decr 针对一个key的数值进行递减操作
func (r *Redis) Decr(key string) (int64, error) { func (r *Redis) Decr(key string) (int64, error) {
return r.redis.Decr(r.ctx, key).Result() return r.db.Decr(r.ctx, key).Result()
} }
// DecrBy 针对一个key的数值进行递减操作指定每次递减多少 // DecrBy 针对一个key的数值进行递减操作指定每次递减多少
func (r *Redis) DecrBy(key string, value int64) (int64, error) { func (r *Redis) DecrBy(key string, value int64) (int64, error) {
return r.redis.DecrBy(r.ctx, key, value).Result() return r.db.DecrBy(r.ctx, key, value).Result()
} }
// Del 删除key操作支持批量删除 // Del 删除key操作支持批量删除
func (r *Redis) Del(keys ...string) error { func (r *Redis) Del(keys ...string) error {
return r.redis.Del(r.ctx, keys...).Err() return r.db.Del(r.ctx, keys...).Err()
} }

@ -1,6 +1,7 @@
package gocache package gocache
import ( import (
"encoding/json"
"time" "time"
) )
@ -14,39 +15,43 @@ type RedisCache struct {
// NewCache 返回Redis缓存实例 // NewCache 返回Redis缓存实例
func (r *Redis) NewCache(expiration time.Duration) *RedisCache { func (r *Redis) NewCache(expiration time.Duration) *RedisCache {
return &RedisCache{ return &RedisCache{db: r, expiration: expiration}
db: r, // 操作类
expiration: expiration, // 过期时间
}
} }
func (rc *RedisCache) GetInterface(key string) (ret string) { // GetString 缓存操作
func (rc *RedisCache) GetString(key string) (ret string) {
f := func() interface{} { f := func() string {
return rc.GetterInterface() return rc.GetterString()
} }
// 如果不存在则调用GetterString
ret, err := rc.db.Get(key) ret, err := rc.db.Get(key)
if err != nil { if err != nil {
rc.db.Set(key, f, rc.expiration) rc.db.Set(key, f(), rc.expiration)
ret, _ = rc.db.Get(key) ret, _ = rc.db.Get(key)
} }
return return
} }
func (rc *RedisCache) GetString(key string) (ret string) { // GetInterface 缓存操作
func (rc *RedisCache) GetInterface(key string, result interface{}) {
f := func() string { f := func() string {
return rc.GetterString() marshal, _ := json.Marshal(rc.GetterInterface())
return string(marshal)
} }
// 如果不存在则调用GetterInterface
ret, err := rc.db.Get(key) ret, err := rc.db.Get(key)
if err != nil { if err != nil {
rc.db.Set(key, f, rc.expiration) rc.db.Set(key, f(), rc.expiration)
ret = f() ret, _ = rc.db.Get(key)
} }
err = json.Unmarshal([]byte(ret), result)
return return
} }

Loading…
Cancel
Save