- 优化
continuous-integration/drone/push Build is passing Details

master
李光春 2 years ago
parent 109543b406
commit 7df99a0ddf

@ -4,23 +4,28 @@ import (
"github.com/bradfitz/gomemcache/memcache" "github.com/bradfitz/gomemcache/memcache"
) )
// MemcachedConfig 配置
type MemcachedConfig struct {
Dns string // 连接地址,可选
Db *memcache.Client // 驱动,可选
}
// Memcached https://github.com/bradfitz/gomemcache // Memcached https://github.com/bradfitz/gomemcache
type Memcached struct { type Memcached struct {
db *memcache.Client // 驱动 db *memcache.Client // 驱动
} }
// NewMemcached 实例化 // NewMemcached 实例化
func NewMemcached(dns string) *Memcached { func NewMemcached(config *MemcachedConfig) *Memcached {
mc := memcache.New(dns) if config.Dns == "" {
if mc == nil { return &Memcached{db: config.Db}
panic("连接失败") } 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 插入数据 // Set 插入数据

@ -9,36 +9,48 @@ import (
"time" "time"
) )
// RedisConfig 配置
type RedisConfig struct {
Addr string // 地址,可选
Password string // 密码,可选
DbName int // 数据库,可选
DefaultExpiration time.Duration // 默认过期时间
Db *redis.Client // 驱动,可选
}
// Redis https://github.com/go-redis/redis // Redis https://github.com/go-redis/redis
type Redis struct { type Redis struct {
db *redis.Client // 驱动 RedisConfig
ctx context.Context // 上下文内容 db *redis.Client // 驱动
expiration time.Duration // 默认过期时间 ctx context.Context // 上下文内容
} }
// NewRedis 实例化 // NewRedis 实例化
func NewRedis(addr, password string, dbName int, expiration time.Duration) *Redis { func NewRedis(config *RedisConfig) *Redis {
db := redis.NewClient(&redis.Options{ app := &Redis{}
Addr: addr, // 地址 app.DefaultExpiration = config.DefaultExpiration
Password: password, // 密码 app.ctx = context.Background()
DB: dbName, // 数据库 if config.Addr == "" {
PoolSize: 100, // 连接池大小 app.db = config.Db
}) return app
} else {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) app.db = redis.NewClient(&redis.Options{
defer cancel() Addr: config.Addr, // 地址
Password: config.Password, // 密码
_, err := db.Ping(ctx).Result() DB: config.DbName, // 数据库
if err != nil { PoolSize: 100, // 连接池大小
panic(errors.New(fmt.Sprintf("连接失败,%s", err.Error()))) })
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的值 // Set 设置一个key的值
@ -54,13 +66,13 @@ func (r *Redis) SetInterface(key string, value interface{}, expiration time.Dura
// 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.db.Set(r.ctx, key, value, r.expiration).Result() return r.db.Set(r.ctx, key, value, r.DefaultExpiration).Result()
} }
// SetInterfaceDefaultExpiration 设置一个key的值使用全局默认过期时间 // SetInterfaceDefaultExpiration 设置一个key的值使用全局默认过期时间
func (r *Redis) SetInterfaceDefaultExpiration(key string, value interface{}) (string, error) { func (r *Redis) SetInterfaceDefaultExpiration(key string, value interface{}) (string, error) {
marshal, _ := json.Marshal(value) 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的值 // Get 查询key的值
@ -90,7 +102,7 @@ func (r *Redis) SetNX(key string, value interface{}, expiration time.Duration) e
// 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.db.SetNX(r.ctx, key, value, r.expiration).Err() return r.db.SetNX(r.ctx, key, value, r.DefaultExpiration).Err()
} }
// MGet 批量查询key的值 // MGet 批量查询key的值

@ -5,22 +5,33 @@ import (
"time" "time"
) )
// RedisCacheConfig 配置
type RedisCacheConfig struct {
expiration time.Duration // 过期时间
}
// RedisCache https://github.com/go-redis/redis // RedisCache https://github.com/go-redis/redis
type RedisCache struct { type RedisCache struct {
RedisCacheConfig
db *Redis // 驱动 db *Redis // 驱动
expiration time.Duration // 默认过期时间
GetterString GttStringFunc // 不存在的操作 GetterString GttStringFunc // 不存在的操作
GetterInterface GttInterfaceFunc // 不存在的操作 GetterInterface GttInterfaceFunc // 不存在的操作
} }
// NewCache 实例化 // NewCache 实例化
func (r *Redis) NewCache(expiration time.Duration) *RedisCache { func (r *Redis) NewCache(config *RedisCacheConfig) *RedisCache {
return &RedisCache{db: r, expiration: expiration} app := &RedisCache{}
app.expiration = config.expiration
app.db = r
return app
} }
// NewCacheDefaultExpiration 实例化 // NewCacheDefaultExpiration 实例化
func (r *Redis) NewCacheDefaultExpiration() *RedisCache { func (r *Redis) NewCacheDefaultExpiration() *RedisCache {
return &RedisCache{db: r, expiration: r.expiration} app := &RedisCache{}
app.expiration = r.DefaultExpiration
app.db = r
return app
} }
// GetString 缓存操作 // GetString 缓存操作

Loading…
Cancel
Save