From 8dd264de765d6615edf0cb5e5c8f07f401cf1a6a Mon Sep 17 00:00:00 2001 From: coward Date: Wed, 5 Jan 2022 11:15:25 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=E6=96=B0=E5=A2=9Ehash=E7=B3=BB?= =?UTF-8?q?=E5=88=97=E7=BC=93=E5=AD=98=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache.go | 167 ++++++++++++++++++++++++++++++++++++++++++++++++++ cache_test.go | 76 +++++++++++++++++++++++ 2 files changed, 243 insertions(+) diff --git a/cache.go b/cache.go index 561bb0e..304a5d8 100644 --- a/cache.go +++ b/cache.go @@ -63,6 +63,26 @@ type Cache interface { Get(key string) interface{} // Delete 删除指定缓存 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 @@ -168,3 +188,150 @@ func (c *cache) Delete(keys ...string) int64 { 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 +} diff --git a/cache_test.go b/cache_test.go index eafe242..d52e5dc 100644 --- a/cache_test.go +++ b/cache_test.go @@ -49,3 +49,79 @@ func TestCache_RdsClient(t *testing.T) { 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) +}