mirror of
https://github.com/cowardmrx/rds_cache_go.git
synced 2025-02-23 04:10:13 +08:00
✨新增hash系列缓存方法
This commit is contained in:
parent
a0caf16ba9
commit
8dd264de76
167
cache.go
167
cache.go
@ -63,6 +63,26 @@ type Cache interface {
|
|||||||
Get(key string) interface{}
|
Get(key string) interface{}
|
||||||
// Delete 删除指定缓存
|
// Delete 删除指定缓存
|
||||||
Delete(keys ...string) int64
|
Delete(keys ...string) int64
|
||||||
|
// HPut hash put
|
||||||
|
HPut(key string, value ...interface{}) error
|
||||||
|
// HMPut hash put 兼容redis v3
|
||||||
|
HMPut(key string, value ...interface{}) error
|
||||||
|
// HKeyExist 判断hash表中的key是否存在
|
||||||
|
HKeyExist(key, field string) bool
|
||||||
|
// HGet 获取hash表中指定field的值
|
||||||
|
HGet(key, field string) interface{}
|
||||||
|
// HGetAll 获取hash表中的全部值
|
||||||
|
HGetAll(key string) map[string]string
|
||||||
|
// HGetKeyAll 获取hash表中的全部key
|
||||||
|
HGetKeyAll(key string) []string
|
||||||
|
// HIncrBy 为哈希表 key 中的指定字段的整数值加上增量 increment 。
|
||||||
|
HIncrBy(key, field string, incr int64) int64
|
||||||
|
// HFloatIncrBy 为哈希表 key 中的指定字段的浮点数值加上增量 increment 。
|
||||||
|
HFloatIncrBy(key, field string, incr float64) float64
|
||||||
|
// HGetValAll 获取hash表中全部的value值
|
||||||
|
HGetValAll(key string) []string
|
||||||
|
// HDelete 删除hash表中一个或多个字段
|
||||||
|
HDelete(key string, fields ...string) int64
|
||||||
}
|
}
|
||||||
|
|
||||||
// @method NewCache
|
// @method NewCache
|
||||||
@ -168,3 +188,150 @@ func (c *cache) Delete(keys ...string) int64 {
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @method HPut
|
||||||
|
// @description: hash put
|
||||||
|
// @receiver c
|
||||||
|
// @param key string
|
||||||
|
// @param value ...interface{}
|
||||||
|
// @return error
|
||||||
|
func (c *cache) HPut(key string, value ...interface{}) error {
|
||||||
|
_, err := c.client.HSet(c.ctx, key, value...).Result()
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// @method HMPut
|
||||||
|
// @description: hash put 用来兼容redis v3
|
||||||
|
// @receiver c
|
||||||
|
// @param key string
|
||||||
|
// @param value interface{}
|
||||||
|
// @return error
|
||||||
|
func (c *cache) HMPut(key string, value ...interface{}) error {
|
||||||
|
_, err := c.client.HMSet(c.ctx, key, value).Result()
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// @method HKeyExist
|
||||||
|
// @description: 判断hash表中的key是否存在
|
||||||
|
// @receiver c
|
||||||
|
// @param key string
|
||||||
|
// @return bool
|
||||||
|
func (c *cache) HKeyExist(key, field string) bool {
|
||||||
|
result, err := c.client.HExists(c.ctx, key, field).Result()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// @method HGet
|
||||||
|
// @description: 获取hash表中指定key,field的值
|
||||||
|
// @receiver c
|
||||||
|
// @param key string
|
||||||
|
// @param field string
|
||||||
|
// @return interface{}
|
||||||
|
func (c *cache) HGet(key, field string) interface{} {
|
||||||
|
result, err := c.client.HGet(c.ctx, key, field).Result()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic("get key : " + key + " from hash filed: " + field + " failed: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// @method HGetAll
|
||||||
|
// @description: 获取hash表中的全部数据
|
||||||
|
// @receiver c
|
||||||
|
// @param key string
|
||||||
|
// @return interface{}
|
||||||
|
func (c *cache) HGetAll(key string) map[string]string {
|
||||||
|
result, err := c.client.HGetAll(c.ctx, key).Result()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic("get hash by key: " + key + " failed: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// @method HGetKeyAll
|
||||||
|
// @description: 获取hash表中的全部key
|
||||||
|
// @receiver c
|
||||||
|
// @param key string
|
||||||
|
func (c *cache) HGetKeyAll(key string) []string {
|
||||||
|
result, err := c.client.HKeys(c.ctx, key).Result()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic("get hash all key failed :" + err.Error() + " by cache key :" + key)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// @method HIncrBy
|
||||||
|
// @description: 为哈希表 key 中的指定字段的整数值加上增量 increment
|
||||||
|
// @receiver c
|
||||||
|
// @param key string
|
||||||
|
// @param field string
|
||||||
|
// @param incr int64
|
||||||
|
func (c *cache) HIncrBy(key, field string, incr int64) int64 {
|
||||||
|
result, err := c.client.HIncrBy(c.ctx, key, field, incr).Result()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic("hash incr by failed: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// @method HFloatIncrBy
|
||||||
|
// @description: 为哈希表 key 中的指定字段的浮点数值加上增量 increment
|
||||||
|
// @receiver c
|
||||||
|
// @param key string
|
||||||
|
// @param field string
|
||||||
|
// @param incr float64
|
||||||
|
// @return float64
|
||||||
|
func (c *cache) HFloatIncrBy(key, field string, incr float64) float64 {
|
||||||
|
result, err := c.client.HIncrByFloat(c.ctx, key, field, incr).Result()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic("hash incr float by failed: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// @method HGetValAll
|
||||||
|
// @description: 获取hash表中全部的value 值
|
||||||
|
// @receiver c
|
||||||
|
// @param key string
|
||||||
|
func (c *cache) HGetValAll(key string) []string {
|
||||||
|
result, err := c.client.HVals(c.ctx, key).Result()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic("get hash all value failed: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// @method HDelete
|
||||||
|
// @description: 删除hash表中一个或多个字段
|
||||||
|
// @receiver c
|
||||||
|
// @param key string
|
||||||
|
// @param fields ...string
|
||||||
|
func (c *cache) HDelete(key string, fields ...string) int64 {
|
||||||
|
result, err := c.client.HDel(c.ctx, key, fields...).Result()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic("delete hash filed failed: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
@ -49,3 +49,79 @@ func TestCache_RdsClient(t *testing.T) {
|
|||||||
|
|
||||||
cachess.Put("this key", "this aaaaa", 1*time.Minute)
|
cachess.Put("this key", "this aaaaa", 1*time.Minute)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCache_HPut(t *testing.T) {
|
||||||
|
|
||||||
|
//value1 := map[string]interface{}{
|
||||||
|
// "data": "shabi",
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//jsonValue, _ := json.Marshal(value1)
|
||||||
|
|
||||||
|
if err := caches.HPut("h_key", "hvalue_1", 1, "key_2", 3.14); err != nil {
|
||||||
|
t.Logf("hash put failed: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Log("hash put success")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCache_HMPut(t *testing.T) {
|
||||||
|
|
||||||
|
if err := caches.HMPut("h_m_put", "hm_key_1", "hm_value_1", "hm_key_2", "hm_value_2"); err != nil {
|
||||||
|
t.Logf("hm put failed: %v", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Log("hm put success")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCache_HKeyExist(t *testing.T) {
|
||||||
|
if ok := caches.HKeyExist("h_m_put", "hm_key_1"); !ok {
|
||||||
|
t.Log("key is not exist")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Log("key is exist")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCache_HGet(t *testing.T) {
|
||||||
|
result := caches.HGet("h_key", "hvalue_1")
|
||||||
|
|
||||||
|
t.Logf("result is :%v", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCache_HGetAll(t *testing.T) {
|
||||||
|
result := caches.HGetAll("h_keys")
|
||||||
|
|
||||||
|
t.Logf("result is : %v", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCache_HGetKeyAll(t *testing.T) {
|
||||||
|
result := caches.HGetKeyAll("h_key")
|
||||||
|
t.Logf("all key is : %v", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCache_HIncrBy(t *testing.T) {
|
||||||
|
result := caches.HIncrBy("h_key", "hvalue_1", 3)
|
||||||
|
|
||||||
|
t.Logf("result is : %v", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCache_HFloatIncrBy(t *testing.T) {
|
||||||
|
result := caches.HFloatIncrBy("h_key", "key_2", 3.14)
|
||||||
|
|
||||||
|
t.Logf("result is : %v", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCache_HGetValAll(t *testing.T) {
|
||||||
|
result := caches.HGetValAll("h_key")
|
||||||
|
|
||||||
|
t.Logf("result is : %v", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCache_HDelete(t *testing.T) {
|
||||||
|
result := caches.HDelete("h_key", "key_2")
|
||||||
|
|
||||||
|
t.Logf("result is : %v", result)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user