From 7df99a0ddfc0d51c43d626ac6a08e85da6f512b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Thu, 19 May 2022 00:04:47 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- memcached.go | 25 +++++++++++-------- redis.go | 66 +++++++++++++++++++++++++++++--------------------- redis_cache.go | 19 ++++++++++++--- 3 files changed, 69 insertions(+), 41 deletions(-) diff --git a/memcached.go b/memcached.go index beb2501..7a9d679 100644 --- a/memcached.go +++ b/memcached.go @@ -4,23 +4,28 @@ 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(dns string) *Memcached { - mc := memcache.New(dns) - if mc == nil { - panic("连接失败") +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} } - return &Memcached{db: mc} -} - -// NewMemcachedDb 实例化 -func NewMemcachedDb(memcached *memcache.Client) *Memcached { - return &Memcached{db: memcached} } // Set 插入数据 diff --git a/redis.go b/redis.go index 8208340..eca74e1 100644 --- a/redis.go +++ b/redis.go @@ -9,36 +9,48 @@ import ( "time" ) +// RedisConfig 配置 +type RedisConfig struct { + Addr string // 地址,可选 + Password string // 密码,可选 + DbName int // 数据库,可选 + DefaultExpiration time.Duration // 默认过期时间 + Db *redis.Client // 驱动,可选 +} + // Redis https://github.com/go-redis/redis type Redis struct { - db *redis.Client // 驱动 - ctx context.Context // 上下文内容 - expiration time.Duration // 默认过期时间 + RedisConfig + db *redis.Client // 驱动 + ctx context.Context // 上下文内容 } // NewRedis 实例化 -func NewRedis(addr, password string, dbName int, expiration time.Duration) *Redis { - 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()))) +func NewRedis(config *RedisConfig) *Redis { + app := &Redis{} + app.DefaultExpiration = config.DefaultExpiration + app.ctx = context.Background() + if config.Addr == "" { + app.db = config.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 } - - 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的值 @@ -54,13 +66,13 @@ func (r *Redis) SetInterface(key string, value interface{}, expiration time.Dura // SetDefaultExpiration 设置一个key的值,使用全局默认过期时间 func (r *Redis) SetDefaultExpiration(key string, value interface{}) (string, error) { - return r.db.Set(r.ctx, key, value, r.expiration).Result() + return r.db.Set(r.ctx, key, value, r.DefaultExpiration).Result() } // SetInterfaceDefaultExpiration 设置一个key的值,使用全局默认过期时间 func (r *Redis) SetInterfaceDefaultExpiration(key string, value interface{}) (string, error) { marshal, _ := json.Marshal(value) - return r.db.Set(r.ctx, key, marshal, r.expiration).Result() + return r.db.Set(r.ctx, key, marshal, r.DefaultExpiration).Result() } // Get 查询key的值 @@ -90,7 +102,7 @@ func (r *Redis) SetNX(key string, value interface{}, expiration time.Duration) e // SetNXDefaultExpiration 如果key不存在,则设置这个key的值,使用全局默认过期时间 func (r *Redis) SetNXDefaultExpiration(key string, value interface{}) error { - return r.db.SetNX(r.ctx, key, value, r.expiration).Err() + return r.db.SetNX(r.ctx, key, value, r.DefaultExpiration).Err() } // MGet 批量查询key的值 diff --git a/redis_cache.go b/redis_cache.go index bc3e24a..9416c24 100644 --- a/redis_cache.go +++ b/redis_cache.go @@ -5,22 +5,33 @@ import ( "time" ) +// RedisCacheConfig 配置 +type RedisCacheConfig struct { + expiration time.Duration // 过期时间 +} + // RedisCache https://github.com/go-redis/redis type RedisCache struct { + RedisCacheConfig db *Redis // 驱动 - expiration time.Duration // 默认过期时间 GetterString GttStringFunc // 不存在的操作 GetterInterface GttInterfaceFunc // 不存在的操作 } // NewCache 实例化 -func (r *Redis) NewCache(expiration time.Duration) *RedisCache { - return &RedisCache{db: r, expiration: expiration} +func (r *Redis) NewCache(config *RedisCacheConfig) *RedisCache { + app := &RedisCache{} + app.expiration = config.expiration + app.db = r + return app } // NewCacheDefaultExpiration 实例化 func (r *Redis) NewCacheDefaultExpiration() *RedisCache { - return &RedisCache{db: r, expiration: r.expiration} + app := &RedisCache{} + app.expiration = r.DefaultExpiration + app.db = r + return app } // GetString 缓存操作