diff --git a/const.go b/const.go index 5099679..9f6697e 100644 --- a/const.go +++ b/const.go @@ -1,3 +1,3 @@ package gocache -const Version = "1.0.10" +const Version = "1.0.11" diff --git a/go.mod b/go.mod index 74f6296..f5794f0 100644 --- a/go.mod +++ b/go.mod @@ -4,9 +4,7 @@ go 1.19 require ( github.com/allegro/bigcache/v3 v3.0.2 - github.com/bradfitz/gomemcache v0.0.0-20220106215444-fb4bf637b56d github.com/dgraph-io/ristretto v0.1.0 - github.com/go-redis/redis/v9 v9.0.0-beta.1 github.com/patrickmn/go-cache v2.1.0+incompatible go.dtapp.net/dorm v1.0.14 go.etcd.io/etcd/client/v3 v3.5.4 @@ -19,6 +17,7 @@ require ( github.com/coreos/go-systemd/v22 v22.3.2 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/dustin/go-humanize v1.0.0 // indirect + github.com/go-redis/redis/v9 v9.0.0-beta.1 // indirect github.com/go-sql-driver/mysql v1.6.0 // indirect github.com/go-stack/stack v1.8.1 // indirect github.com/goccy/go-json v0.8.1 // indirect diff --git a/go.sum b/go.sum index e087a37..61ebd98 100644 --- a/go.sum +++ b/go.sum @@ -36,8 +36,6 @@ github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+Ce github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/bitly/go-simplejson v0.5.0 h1:6IH+V8/tVMab511d5bn4M7EwGXZf9Hj6i2xSwkNEM+Y= -github.com/bradfitz/gomemcache v0.0.0-20220106215444-fb4bf637b56d h1:pVrfxiGfwelyab6n21ZBkbkmbevaf+WvMIiR7sr97hw= -github.com/bradfitz/gomemcache v0.0.0-20220106215444-fb4bf637b56d/go.mod h1:H0wQNHz2YrLsuXOZozoeDmnHXkNCRmMW0gwFWDfEZDA= github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= diff --git a/memcached.go b/memcached.go deleted file mode 100644 index 7a9d679..0000000 --- a/memcached.go +++ /dev/null @@ -1,51 +0,0 @@ -package gocache - -import ( - "github.com/bradfitz/gomemcache/memcache" -) - -// MemcachedConfig 配置 -type MemcachedConfig struct { - Dns string // 连接地址,可选 - Db *memcache.Client // 驱动,可选 -} - -// Memcached https://github.com/bradfitz/gomemcache -type Memcached struct { - db *memcache.Client // 驱动 -} - -// NewMemcached 实例化 -func NewMemcached(config *MemcachedConfig) *Memcached { - if config.Dns == "" { - return &Memcached{db: config.Db} - } else { - mc := memcache.New(config.Dns) - if mc == nil { - panic("连接失败") - } - return &Memcached{db: mc} - } -} - -// Set 插入数据 -func (m *Memcached) Set(key string, value []byte) error { - return m.db.Set(&memcache.Item{Key: key, Value: value}) -} - -// Get 获取单个数据 -func (m *Memcached) Get(key string) (string, error) { - it, err := m.db.Get(key) - if err == memcache.ErrCacheMiss { - return "", memcache.ErrCacheMiss - } - if it.Key == key { - return string(it.Value), nil - } - return "", memcache.ErrCacheMiss -} - -// Del 删除单个数据 -func (m *Memcached) Del(key string) error { - return m.db.Delete(key) -} diff --git a/memcached_cache.go b/memcached_cache.go deleted file mode 100644 index 069dcf7..0000000 --- a/memcached_cache.go +++ /dev/null @@ -1,57 +0,0 @@ -package gocache - -import ( - "encoding/json" - "github.com/bradfitz/gomemcache/memcache" -) - -// MemcachedCache https://github.com/bradfitz/gomemcache -type MemcachedCache struct { - db *Memcached // 驱动 - GetterString GttStringFunc // 不存在的操作 - GetterInterface GttInterfaceFunc // 不存在的操作 -} - -// NewCache 实例化 -func (m *Memcached) NewCache() *MemcachedCache { - return &MemcachedCache{db: m} -} - -// GetString 缓存操作 -func (mc *MemcachedCache) GetString(key string) (ret string) { - - f := func() string { - return mc.GetterString() - } - - // 如果不存在,则调用GetterString - ret, err := mc.db.Get(key) - - if err == memcache.ErrCacheMiss { - mc.db.Set(key, []byte(f())) - ret, _ = mc.db.Get(key) - } - - return -} - -// GetInterface 缓存操作 -func (mc *MemcachedCache) GetInterface(key string, result interface{}) { - - f := func() string { - marshal, _ := json.Marshal(mc.GetterInterface()) - return string(marshal) - } - - // 如果不存在,则调用GetterInterface - ret, err := mc.db.Get(key) - - if err == memcache.ErrCacheMiss { - mc.db.Set(key, []byte(f())) - ret, _ = mc.db.Get(key) - } - - err = json.Unmarshal([]byte(ret), result) - - return -} diff --git a/redis.go b/redis.go index 21be8d6..3be3fad 100644 --- a/redis.go +++ b/redis.go @@ -3,94 +3,66 @@ package gocache import ( "context" "encoding/json" - "errors" - "fmt" - "github.com/go-redis/redis/v9" "go.dtapp.net/dorm" "time" ) // RedisConfig 配置 type RedisConfig struct { - Addr string // 地址,可选 - Password string // 密码,可选 - DbName int // 数据库,可选 DefaultExpiration time.Duration // 默认过期时间 - RedisClient *dorm.RedisClient // 驱动,可选 + Client *dorm.RedisClient // 驱动,可选 Debug bool // 调试,可选 } // Redis https://github.com/go-redis/redis type Redis struct { - RedisConfig - db *redis.Client // 驱动 - ctx context.Context // 上下文内容 + config *RedisConfig // 配置 + Client *dorm.RedisClient // 驱动 } // NewRedis 实例化 func NewRedis(config *RedisConfig) *Redis { - app := &Redis{} - app.DefaultExpiration = config.DefaultExpiration - app.ctx = context.Background() - if config.Addr == "" { - app.db = config.RedisClient.Db - return app - } else { - app.db = redis.NewClient(&redis.Options{ - Addr: config.Addr, // 地址 - Password: config.Password, // 密码 - DB: config.DbName, // 数据库 - PoolSize: 100, // 连接池大小 - }) - - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - - _, err := app.db.Ping(ctx).Result() - if err != nil { - panic(errors.New(fmt.Sprintf("连接失败,%s", err.Error()))) - } - - return app - } + c := &Redis{config: config} + c.Client = config.Client + return c } // Set 设置一个key的值 func (r *Redis) Set(key string, value interface{}, expiration time.Duration) (string, error) { r.setLog(key) - return r.db.Set(r.ctx, key, value, expiration).Result() + return r.Client.Db.Set(context.Background(), key, value, expiration).Result() } // SetInterface 设置一个key的值 func (r *Redis) SetInterface(key string, value interface{}, expiration time.Duration) (string, error) { r.setLog(key) marshal, _ := json.Marshal(value) - return r.db.Set(r.ctx, key, marshal, expiration).Result() + return r.Client.Db.Set(context.Background(), key, marshal, expiration).Result() } // SetDefaultExpiration 设置一个key的值,使用全局默认过期时间 func (r *Redis) SetDefaultExpiration(key string, value interface{}) (string, error) { r.setLog(key) - return r.db.Set(r.ctx, key, value, r.DefaultExpiration).Result() + return r.Client.Db.Set(context.Background(), key, value, r.config.DefaultExpiration).Result() } // SetInterfaceDefaultExpiration 设置一个key的值,使用全局默认过期时间 func (r *Redis) SetInterfaceDefaultExpiration(key string, value interface{}) (string, error) { r.setLog(key) marshal, _ := json.Marshal(value) - return r.db.Set(r.ctx, key, marshal, r.DefaultExpiration).Result() + return r.Client.Db.Set(context.Background(), key, marshal, r.config.DefaultExpiration).Result() } // Get 查询key的值 func (r *Redis) Get(key string) (string, error) { r.getLog(key) - return r.db.Get(r.ctx, key).Result() + return r.Client.Db.Get(context.Background(), key).Result() } // GetInterface 查询key的值 func (r *Redis) GetInterface(key string, result interface{}) error { r.getLog(key) - ret, err := r.db.Get(r.ctx, key).Result() + ret, err := r.Client.Db.Get(context.Background(), key).Result() if err != nil { return err } @@ -100,52 +72,52 @@ func (r *Redis) GetInterface(key string, result interface{}) error { // GetSet 设置一个key的值,并返回这个key的旧值 func (r *Redis) GetSet(key string, value interface{}) (string, error) { - return r.db.GetSet(r.ctx, key, value).Result() + return r.Client.Db.GetSet(context.Background(), key, value).Result() } // SetNX 如果key不存在,则设置这个key的值 func (r *Redis) SetNX(key string, value interface{}, expiration time.Duration) error { - return r.db.SetNX(r.ctx, key, value, expiration).Err() + return r.Client.Db.SetNX(context.Background(), key, value, expiration).Err() } // SetNXDefaultExpiration 如果key不存在,则设置这个key的值,使用全局默认过期时间 func (r *Redis) SetNXDefaultExpiration(key string, value interface{}) error { - return r.db.SetNX(r.ctx, key, value, r.DefaultExpiration).Err() + return r.Client.Db.SetNX(context.Background(), key, value, r.config.DefaultExpiration).Err() } // MGet 批量查询key的值 func (r *Redis) MGet(keys ...string) ([]interface{}, error) { - return r.db.MGet(r.ctx, keys...).Result() + return r.Client.Db.MGet(context.Background(), keys...).Result() } // MSet 批量设置key的值 // MSet(map[string]interface{}{"key1": "value1", "key2": "value2"}) func (r *Redis) MSet(values map[string]interface{}) error { - return r.db.MSet(r.ctx, values).Err() + return r.Client.Db.MSet(context.Background(), values).Err() } // Incr 针对一个key的数值进行递增操作 func (r *Redis) Incr(key string) (int64, error) { - return r.db.Incr(r.ctx, key).Result() + return r.Client.Db.Incr(context.Background(), key).Result() } // IncrBy 针对一个key的数值进行递增操作,指定每次递增多少 func (r *Redis) IncrBy(key string, value int64) (int64, error) { - return r.db.IncrBy(r.ctx, key, value).Result() + return r.Client.Db.IncrBy(context.Background(), key, value).Result() } // Decr 针对一个key的数值进行递减操作 func (r *Redis) Decr(key string) (int64, error) { - return r.db.Decr(r.ctx, key).Result() + return r.Client.Db.Decr(context.Background(), key).Result() } // DecrBy 针对一个key的数值进行递减操作,指定每次递减多少 func (r *Redis) DecrBy(key string, value int64) (int64, error) { - return r.db.DecrBy(r.ctx, key, value).Result() + return r.Client.Db.DecrBy(context.Background(), key, value).Result() } // Del 删除key操作,支持批量删除 func (r *Redis) Del(keys ...string) error { r.delLog(keys...) - return r.db.Del(r.ctx, keys...).Err() + return r.Client.Db.Del(context.Background(), keys...).Err() } diff --git a/redis_cache.go b/redis_cache.go index 9416c24..48e04bb 100644 --- a/redis_cache.go +++ b/redis_cache.go @@ -12,26 +12,25 @@ type RedisCacheConfig struct { // RedisCache https://github.com/go-redis/redis type RedisCache struct { - RedisCacheConfig - db *Redis // 驱动 + config *RedisCacheConfig + operation *Redis // 操作 GetterString GttStringFunc // 不存在的操作 GetterInterface GttInterfaceFunc // 不存在的操作 } // NewCache 实例化 func (r *Redis) NewCache(config *RedisCacheConfig) *RedisCache { - app := &RedisCache{} - app.expiration = config.expiration - app.db = r - return app + c := &RedisCache{config: config} + c.operation = r + return c } // NewCacheDefaultExpiration 实例化 func (r *Redis) NewCacheDefaultExpiration() *RedisCache { - app := &RedisCache{} - app.expiration = r.DefaultExpiration - app.db = r - return app + c := &RedisCache{} + c.config.expiration = r.config.DefaultExpiration + c.operation = r + return c } // GetString 缓存操作 @@ -42,10 +41,10 @@ func (rc *RedisCache) GetString(key string) (ret string) { } // 如果不存在,则调用GetterString - ret, err := rc.db.Get(key) + ret, err := rc.operation.Get(key) if err != nil { - rc.db.Set(key, f(), rc.expiration) - ret, _ = rc.db.Get(key) + rc.operation.Set(key, f(), rc.config.expiration) + ret, _ = rc.operation.Get(key) } return @@ -60,11 +59,11 @@ func (rc *RedisCache) GetInterface(key string, result interface{}) { } // 如果不存在,则调用GetterInterface - ret, err := rc.db.Get(key) + ret, err := rc.operation.Get(key) if err != nil { - rc.db.Set(key, f(), rc.expiration) - ret, _ = rc.db.Get(key) + rc.operation.Set(key, f(), rc.config.expiration) + ret, _ = rc.operation.Get(key) } err = json.Unmarshal([]byte(ret), result) diff --git a/redis_log.go b/redis_log.go index 81f14c7..d0f5a60 100644 --- a/redis_log.go +++ b/redis_log.go @@ -3,17 +3,17 @@ package gocache import "log" func (r *Redis) setLog(key string) { - if r.Debug == true { + if r.config.Debug == true { log.Printf("gocache [%s] set\n", key) } } func (r *Redis) getLog(key string) { - if r.Debug == true { + if r.config.Debug == true { log.Printf("gocache [%s] get\n", key) } } func (r *Redis) delLog(key ...string) { - if r.Debug == true { + if r.config.Debug == true { log.Printf("gocache [%s] del\n", key) } }