Skip to content

Commit

Permalink
#### Version 1.3.8
Browse files Browse the repository at this point in the history
* 完善redisutil,增加对hset、set的操作支持
* 感谢 @chacha923 的pr
* 2017-11-23 00:00
  • Loading branch information
devfeel committed Nov 23, 2017
1 parent d458fd8 commit 044512d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 61 deletions.
4 changes: 2 additions & 2 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ func NewRuntimeCache() Cache {

//new redis cache
//must set serverIp like "redis://:[email protected]:6379/0"
func NewRedisCache(serverIp string) Cache {
return redis.NewRedisCache(serverIp)
func NewRedisCache(serverUrl string) Cache {
return redis.NewRedisCache(serverUrl)
}
22 changes: 11 additions & 11 deletions cache/redis/cache_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ var (
// RedisCache is redis cache adapter.
// it contains serverIp for redis conn.
type RedisCache struct {
serverIp string //connection string, like "10.0.1.11:6379"
serverUrl string //connection string, like "redis://:password@10.0.1.11:6379/0"
}

// NewRedisCache returns a new *RedisCache.
func NewRedisCache(serverIp string) *RedisCache {
cache := RedisCache{serverIp: serverIp}
func NewRedisCache(serverUrl string) *RedisCache {
cache := RedisCache{serverUrl: serverUrl}
return &cache
}

// Exists check item exist in redis cache.
func (ca *RedisCache) Exists(key string) (bool, error) {
redisClient := redisutil.GetRedisClient(ca.serverIp)
redisClient := redisutil.GetRedisClient(ca.serverUrl)
exists, err := redisClient.Exists(key)
return exists, err
}

// Incr increase int64 counter in redis cache.
func (ca *RedisCache) Incr(key string) (int64, error) {
redisClient := redisutil.GetRedisClient(ca.serverIp)
redisClient := redisutil.GetRedisClient(ca.serverUrl)
val, err := redisClient.INCR(key)
if err != nil {
return 0, err
Expand All @@ -40,7 +40,7 @@ func (ca *RedisCache) Incr(key string) (int64, error) {

// Decr decrease counter in redis cache.
func (ca *RedisCache) Decr(key string) (int64, error) {
redisClient := redisutil.GetRedisClient(ca.serverIp)
redisClient := redisutil.GetRedisClient(ca.serverUrl)
val, err := redisClient.DECR(key)
if err != nil {
return 0, err
Expand All @@ -51,15 +51,15 @@ func (ca *RedisCache) Decr(key string) (int64, error) {
// Get cache from redis cache.
// if non-existed or expired, return nil.
func (ca *RedisCache) Get(key string) (interface{}, error) {
redisClient := redisutil.GetRedisClient(ca.serverIp)
redisClient := redisutil.GetRedisClient(ca.serverUrl)
reply, err := redisClient.GetObj(key)
return reply, err
}

// returns value string format by given key
// if non-existed or expired, return "".
func (ca *RedisCache) GetString(key string) (string, error) {
redisClient := redisutil.GetRedisClient(ca.serverIp)
redisClient := redisutil.GetRedisClient(ca.serverUrl)
reply, err := redisClient.Get(key)
return reply, err
}
Expand Down Expand Up @@ -99,23 +99,23 @@ func (ca *RedisCache) GetInt64(key string) (int64, error) {
// Set cache to redis.
// ttl is second, if ttl is 0, it will be forever.
func (ca *RedisCache) Set(key string, value interface{}, ttl int64) error {
redisClient := redisutil.GetRedisClient(ca.serverIp)
redisClient := redisutil.GetRedisClient(ca.serverUrl)
_, err := redisClient.SetWithExpire(key, value, ttl)
return err
}

// Delete item in redis cacha.
// if not exists, we think it's success
func (ca *RedisCache) Delete(key string) error {
redisClient := redisutil.GetRedisClient(ca.serverIp)
redisClient := redisutil.GetRedisClient(ca.serverUrl)
_, err := redisClient.Del(key)
return err
}

// ClearAll will delete all item in redis cache.
// never error
func (ca *RedisCache) ClearAll() error {
redisClient := redisutil.GetRedisClient(ca.serverIp)
redisClient := redisutil.GetRedisClient(ca.serverUrl)
redisClient.FlushDB()
return nil
}
106 changes: 58 additions & 48 deletions framework/redis/redisutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func newPool(redisURL string) *redis.Pool {
}
}

//GetRedisClient 获取指定Address的RedisClient
// GetRedisClient 获取指定Address的RedisClient
func GetRedisClient(address string) *RedisClient {
var redis *RedisClient
var mok bool
Expand All @@ -58,7 +58,7 @@ func GetRedisClient(address string) *RedisClient {
return redis
}

//GetObj 获取指定key的内容, interface{}
// GetObj 获取指定key的内容, interface{}
func (rc *RedisClient) GetObj(key string) (interface{}, error) {
// 从连接池里面获得一个连接
conn := rc.pool.Get()
Expand All @@ -68,13 +68,13 @@ func (rc *RedisClient) GetObj(key string) (interface{}, error) {
return reply, errDo
}

//Get 获取指定key的内容, string
// Get 获取指定key的内容, string
func (rc *RedisClient) Get(key string) (string, error) {
val, err := redis.String(rc.GetObj(key))
return val, err
}

//Exists 检查指定key是否存在
// Exists 检查指定key是否存在
func (rc *RedisClient) Exists(key string) (bool, error) {
// 从连接池里面获得一个连接
conn := rc.pool.Get()
Expand All @@ -85,7 +85,7 @@ func (rc *RedisClient) Exists(key string) (bool, error) {
return reply, errDo
}

//Del 删除指定key
// Del 删除指定key
func (rc *RedisClient) Del(key string) (int64, error) {
// 从连接池里面获得一个连接
conn := rc.pool.Get()
Expand All @@ -99,7 +99,7 @@ func (rc *RedisClient) Del(key string) (int64, error) {
return val, err
}

//INCR 对存储在指定key的数值执行原子的加1操作
// INCR 对存储在指定key的数值执行原子的加1操作
func (rc *RedisClient) INCR(key string) (int, error) {
conn := rc.pool.Get()
defer conn.Close()
Expand All @@ -111,7 +111,7 @@ func (rc *RedisClient) INCR(key string) (int, error) {
return val, err
}

//DECR 对存储在指定key的数值执行原子的减1操作
// DECR 对存储在指定key的数值执行原子的减1操作
func (rc *RedisClient) DECR(key string) (int, error) {
conn := rc.pool.Get()
defer conn.Close()
Expand All @@ -124,7 +124,7 @@ func (rc *RedisClient) DECR(key string) (int, error) {
}


//Append 如果 key 已经存在并且是一个字符串, APPEND 命令将 value 追加到 key 原来的值的末尾。
// Append 如果 key 已经存在并且是一个字符串, APPEND 命令将 value 追加到 key 原来的值的末尾。
// 如果 key 不存在, APPEND 就简单地将给定 key 设为 value ,就像执行 SET key value 一样。
func (rc *RedisClient) Append(key string, val interface{}) (interface{}, error) {
conn := rc.pool.Get()
Expand All @@ -137,23 +137,32 @@ func (rc *RedisClient) Append(key string, val interface{}) (interface{}, error)
return val, err
}

//Set 设置指定Key/Value
// Set 设置指定Key/Value
func (rc *RedisClient) Set(key string, val interface{}) (interface{}, error) {
conn := rc.pool.Get()
defer conn.Close()
val, err := redis.String(conn.Do("SET", key, val))
return val, err
}

//SetWithExpire 设置指定key的内容

// Expire 设置指定key的过期时间
func (rc *RedisClient) Expire(key string, timeOutSeconds int64) (int64, error) {
conn := rc.pool.Get()
defer conn.Close()
val, err := redis.Int64(conn.Do("EXPIRE", key, timeOutSeconds))
return val, err
}

// SetWithExpire 设置指定key的内容
func (rc *RedisClient) SetWithExpire(key string, val interface{}, timeOutSeconds int64) (interface{}, error) {
conn := rc.pool.Get()
defer conn.Close()
val, err := redis.String(conn.Do("SET", key, val, "EX", timeOutSeconds))
return val, err
}

//SetNX 将 key 的值设为 value ,当且仅当 key 不存在。
// SetNX 将 key 的值设为 value ,当且仅当 key 不存在。
// 若给定的 key 已经存在,则 SETNX 不做任何动作。 成功返回1, 失败返回0
func (rc *RedisClient) SetNX(key, value string) (interface{}, error){
conn := rc.pool.Get()
Expand All @@ -164,7 +173,7 @@ func (rc *RedisClient) SetNX(key, value string) (interface{}, error){
}


//HGet 获取指定hashset的内容
// HGet 获取指定hashset的内容
func (rc *RedisClient) HGet(hashID string, field string) (string, error) {
conn := rc.pool.Get()
defer conn.Close()
Expand All @@ -176,23 +185,23 @@ func (rc *RedisClient) HGet(hashID string, field string) (string, error) {
return val, err
}

//HGetAll 获取指定hashset的所有内容
// HGetAll 获取指定hashset的所有内容
func (rc *RedisClient) HGetAll(hashID string) (map[string]string, error) {
conn := rc.pool.Get()
defer conn.Close()
reply, err := redis.StringMap(conn.Do("HGetAll", hashID))
return reply, err
}

//HSet 设置指定hashset的内容
// HSet 设置指定hashset的内容
func (rc *RedisClient) HSet(hashID string, field string, val string) error {
conn := rc.pool.Get()
defer conn.Close()
_, err := conn.Do("HSET", hashID, field, val)
return err
}

//HSetNX 设置指定hashset的内容, 如果field不存在, 该操作无效
// HSetNX 设置指定hashset的内容, 如果field不存在, 该操作无效
func (rc *RedisClient) HSetNX(key, field, value string) (interface{}, error) {
conn := rc.pool.Get()
defer conn.Close()
Expand All @@ -201,7 +210,7 @@ func (rc *RedisClient) HSetNX(key, field, value string) (interface{}, error) {
return val, err
}

//HLen 返回哈希表 key 中域的数量, 当 key 不存在时,返回0
// HLen 返回哈希表 key 中域的数量, 当 key 不存在时,返回0
func (rc *RedisClient) HLen(key string) (int64, error) {
conn := rc.pool.Get()
defer conn.Close()
Expand All @@ -210,7 +219,7 @@ func (rc *RedisClient) HLen(key string) (int64, error) {
return val, err
}

//HDel 设置指定hashset的内容, 如果field不存在, 该操作无效, 返回0
// HDel 设置指定hashset的内容, 如果field不存在, 该操作无效, 返回0
func (rc *RedisClient) HDel(args ...interface{}) (int64, error) {
conn := rc.pool.Get()
defer conn.Close()
Expand All @@ -219,7 +228,7 @@ func (rc *RedisClient) HDel(args ...interface{}) (int64, error) {
return val, err
}

//HVals 返回哈希表 key 中所有域的值, 当 key 不存在时,返回空
// HVals 返回哈希表 key 中所有域的值, 当 key 不存在时,返回空
func (rc *RedisClient) HVals(key string) (interface{}, error) {
conn := rc.pool.Get()
defer conn.Close()
Expand All @@ -229,8 +238,7 @@ func (rc *RedisClient) HVals(key string) (interface{}, error) {
}



//BRPop 删除,并获得该列表中的最后一个元素,或阻塞,直到有一个可用
// BRPop 删除,并获得该列表中的最后一个元素,或阻塞,直到有一个可用
func (rc *RedisClient) BRPop(key string) (string, error) {
conn := rc.pool.Get()
defer conn.Close()
Expand All @@ -254,33 +262,8 @@ func (rc *RedisClient) LPush(key string, val string) (int64, error) {
}
}


//Expire 设置指定key的过期时间
func (rc *RedisClient) Expire(key string, timeOutSeconds int64) (int64, error) {
conn := rc.pool.Get()
defer conn.Close()
val, err := redis.Int64(conn.Do("EXPIRE", key, timeOutSeconds))
return val, err
}

//FlushDB 删除当前数据库里面的所有数据
//这个命令永远不会出现失败
func (rc *RedisClient) FlushDB() {
conn := rc.pool.Get()
defer conn.Close()
conn.Do("FLUSHALL")
}


//返回一个从连接池获取的redis连接, 需要手动释放redis连接
func (rc *RedisClient) ConnGet() redis.Conn{
conn := rc.pool.Get()

return conn
}



// SAdd 将一个或多个 member 元素加入到集合 key 当中,已经存在于集合的 member 元素将被忽略。
// 假如 key 不存在,则创建一个只包含 member 元素作成员的集合。
func (rc *RedisClient) SAdd(args ...interface{}) (int64, error){
conn := rc.pool.Get()
defer conn.Close()
Expand All @@ -289,6 +272,10 @@ func (rc *RedisClient) SAdd(args ...interface{}) (int64, error){
return val, err
}

// SCard 返回集合 key 的基数(集合中元素的数量)。
// 返回值:
// 集合的基数。
// 当 key 不存在时,返回 0
func (rc *RedisClient) SCard(key string) (int64, error) {
conn := rc.pool.Get()
defer conn.Close()
Expand All @@ -297,6 +284,8 @@ func (rc *RedisClient) SCard(key string) (int64, error) {
return val, err
}

// SPop 移除并返回集合中的一个随机元素。
// 如果只想获取一个随机元素,但不想该元素从集合中被移除的话,可以使用 SRANDMEMBER 命令。
func (rc *RedisClient) SPop(key string) (string, error) {
conn := rc.pool.Get()
defer conn.Close()
Expand All @@ -305,6 +294,8 @@ func (rc *RedisClient) SPop(key string) (string, error) {
return val, err
}

// SRandMember 如果命令执行时,只提供了 key 参数,那么返回集合中的一个随机元素。
// 该操作和 SPOP 相似,但 SPOP 将随机元素从集合中移除并返回,而 SRANDMEMBER 则仅仅返回随机元素,而不对集合进行任何改动。
func (rc *RedisClient) SRandMember(args ...interface{}) (string, error) {
conn := rc.pool.Get()
defer conn.Close()
Expand All @@ -313,6 +304,9 @@ func (rc *RedisClient) SRandMember(args ...interface{}) (string, error) {
return val, err
}

// SRem 移除集合 key 中的一个或多个 member 元素,不存在的 member 元素会被忽略。
// 当 key 不是集合类型,返回一个错误。
// 在 Redis 2.4 版本以前, SREM 只接受单个 member 值。
func (rc *RedisClient) SRem(args ...interface{}) (string, error) {
conn := rc.pool.Get()
defer conn.Close()
Expand All @@ -321,10 +315,26 @@ func (rc *RedisClient) SRem(args ...interface{}) (string, error) {
return val, err
}

// DBSize 返回当前数据库的 key 的数量
func (rc *RedisClient) DBSize()(int64, error){
conn := rc.pool.Get()
defer conn.Close()

val, err := redis.Int64(conn.Do("DBSIZE"))
return val, err
}
}

// FlushDB 删除当前数据库里面的所有数据
// 这个命令永远不会出现失败
func (rc *RedisClient) FlushDB() {
conn := rc.pool.Get()
defer conn.Close()
conn.Do("FLUSHALL")
}


// GetConn 返回一个从连接池获取的redis连接,
// 需要手动释放redis连接
func (rc *RedisClient) GetConn() redis.Conn{
return rc.pool.Get()
}
5 changes: 5 additions & 0 deletions version.MD
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## dotweb版本记录:

#### Version 1.3.8
* 完善redisutil,增加对hset、set的操作支持
* 感谢 @chacha923 的pr
* 2017-11-23 00:00

#### Version 1.3.7
* 新增Classic初始化App方式,调整New初始化内容
* 调整日志启用开关默认为false,可以通过SetEnabledLog(true)或者SetDevelopmentMode()启用
Expand Down

0 comments on commit 044512d

Please sign in to comment.