This commit is contained in:
coward 2023-04-23 19:39:10 +08:00
parent 8dd264de76
commit 1996f736b8
2 changed files with 119 additions and 117 deletions

233
cache.go
View File

@ -2,6 +2,7 @@ package rds_cache_go
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
"time" "time"
@ -60,9 +61,9 @@ type Cache interface {
// Exist 判断某个缓存是否存在 // Exist 判断某个缓存是否存在
Exist(key string) bool Exist(key string) bool
// Get 获取某个缓存的值 // Get 获取某个缓存的值
Get(key string) interface{} Get(key string) (interface{}, error)
// Delete 删除指定缓存 // Delete 删除指定缓存
Delete(keys ...string) int64 Delete(keys ...string) (int64, error)
// HPut hash put // HPut hash put
HPut(key string, value ...interface{}) error HPut(key string, value ...interface{}) error
// HMPut hash put 兼容redis v3 // HMPut hash put 兼容redis v3
@ -70,25 +71,25 @@ type Cache interface {
// HKeyExist 判断hash表中的key是否存在 // HKeyExist 判断hash表中的key是否存在
HKeyExist(key, field string) bool HKeyExist(key, field string) bool
// HGet 获取hash表中指定field的值 // HGet 获取hash表中指定field的值
HGet(key, field string) interface{} HGet(key, field string) (interface{}, error)
// HGetAll 获取hash表中的全部值 // HGetAll 获取hash表中的全部值
HGetAll(key string) map[string]string HGetAll(key string) (map[string]string, error)
// HGetKeyAll 获取hash表中的全部key // HGetKeyAll 获取hash表中的全部key
HGetKeyAll(key string) []string HGetKeyAll(key string) ([]string, error)
// HIncrBy 为哈希表 key 中的指定字段的整数值加上增量 increment 。 // HIncrBy 为哈希表 key 中的指定字段的整数值加上增量 increment 。
HIncrBy(key, field string, incr int64) int64 HIncrBy(key, field string, incr int64) (int64, error)
// HFloatIncrBy 为哈希表 key 中的指定字段的浮点数值加上增量 increment 。 // HFloatIncrBy 为哈希表 key 中的指定字段的浮点数值加上增量 increment 。
HFloatIncrBy(key, field string, incr float64) float64 HFloatIncrBy(key, field string, incr float64) (float64, error)
// HGetValAll 获取hash表中全部的value值 // HGetValAll 获取hash表中全部的value值
HGetValAll(key string) []string HGetValAll(key string) ([]string, error)
// HDelete 删除hash表中一个或多个字段 // HDelete 删除hash表中一个或多个字段
HDelete(key string, fields ...string) int64 HDelete(key string, fields ...string) (int64, error)
} }
// @method NewCache // @method NewCache
// @description: 初始化一个cache对象 // @description: 初始化一个cache对象
// @param opts ...Option // @param opts ...Option
// @return Cache // @return Cache
func NewCache(opts ...Option) Cache { func NewCache(opts ...Option) Cache {
cha := new(config) cha := new(config)
@ -124,13 +125,13 @@ type cache struct {
ctx context.Context ctx context.Context
} }
// @method Put // @method Put
// @description: 放入缓存 // @description: 放入缓存
// @receiver c // @receiver c
// @param key string key // @param key string key
// @param value interface{} 值 // @param value interface{} 值
// @param ttl time.Duration 过期时间 // @param ttl time.Duration 过期时间
// @return error // @return error
func (c *cache) Put(key string, value interface{}, ttl time.Duration) error { func (c *cache) Put(key string, value interface{}, ttl time.Duration) error {
_, err := c.client.Set(c.ctx, key, value, TTL(ttl)).Result() _, err := c.client.Set(c.ctx, key, value, TTL(ttl)).Result()
@ -141,11 +142,11 @@ func (c *cache) Put(key string, value interface{}, ttl time.Duration) error {
return nil return nil
} }
// @method Exist // @method Exist
// @description: 判断指定缓存是否存在 // @description: 判断指定缓存是否存在
// @receiver c // @receiver c
// @param key string 缓存key // @param key string 缓存key
// @return bool // @return bool
func (c *cache) Exist(key string) bool { func (c *cache) Exist(key string) bool {
result, err := c.client.Exists(c.ctx, key).Result() result, err := c.client.Exists(c.ctx, key).Result()
if err != nil { if err != nil {
@ -159,65 +160,65 @@ func (c *cache) Exist(key string) bool {
return true return true
} }
// @method Get // @method Get
// @description: 获取指定缓存 // @description: 获取指定缓存
// @receiver c // @receiver c
// @param key string 缓存key // @param key string 缓存key
// @return interface{} // @return interface{}
func (c *cache) Get(key string) interface{} { func (c *cache) Get(key string) (interface{}, error) {
result, err := c.client.Get(c.ctx, key).Result() result, err := c.client.Get(c.ctx, key).Result()
if err != nil { if err != nil {
panic("get cache by " + key + " failed: " + err.Error()) return nil, errors.New("NOT FOUND " + key)
} }
return result return result, nil
} }
// @method Delete // @method Delete
// @description: 删除指定缓存 // @description: 删除指定缓存
// @receiver c // @receiver c
// @param keys ...string // @param keys ...string
// @return int // @return int
func (c *cache) Delete(keys ...string) int64 { func (c *cache) Delete(keys ...string) (int64, error) {
result, err := c.client.Del(c.ctx, keys...).Result() result, err := c.client.Del(c.ctx, keys...).Result()
if err != nil { if err != nil {
panic("delete cache failed: " + err.Error()) return 0, errors.New("DELETE CACHE FAILED " + err.Error())
} }
return result return result, nil
} }
// @method HPut // @method HPut
// @description: hash put // @description: hash put
// @receiver c // @receiver c
// @param key string // @param key string
// @param value ...interface{} // @param value ...interface{}
// @return error // @return error
func (c *cache) HPut(key string, value ...interface{}) error { func (c *cache) HPut(key string, value ...interface{}) error {
_, err := c.client.HSet(c.ctx, key, value...).Result() _, err := c.client.HSet(c.ctx, key, value...).Result()
return err return err
} }
// @method HMPut // @method HMPut
// @description: hash put 用来兼容redis v3 // @description: hash put 用来兼容redis v3
// @receiver c // @receiver c
// @param key string // @param key string
// @param value interface{} // @param value interface{}
// @return error // @return error
func (c *cache) HMPut(key string, value ...interface{}) error { func (c *cache) HMPut(key string, value ...interface{}) error {
_, err := c.client.HMSet(c.ctx, key, value).Result() _, err := c.client.HMSet(c.ctx, key, value).Result()
return err return err
} }
// @method HKeyExist // @method HKeyExist
// @description: 判断hash表中的key是否存在 // @description: 判断hash表中的key是否存在
// @receiver c // @receiver c
// @param key string // @param key string
// @return bool // @return bool
func (c *cache) HKeyExist(key, field string) bool { func (c *cache) HKeyExist(key, field string) bool {
result, err := c.client.HExists(c.ctx, key, field).Result() result, err := c.client.HExists(c.ctx, key, field).Result()
@ -229,109 +230,109 @@ func (c *cache) HKeyExist(key, field string) bool {
} }
// @method HGet // @method HGet
// @description: 获取hash表中指定keyfield的值 // @description: 获取hash表中指定keyfield的值
// @receiver c // @receiver c
// @param key string // @param key string
// @param field string // @param field string
// @return interface{} // @return interface{}
func (c *cache) HGet(key, field string) interface{} { func (c *cache) HGet(key, field string) (interface{}, error) {
result, err := c.client.HGet(c.ctx, key, field).Result() result, err := c.client.HGet(c.ctx, key, field).Result()
if err != nil { if err != nil {
panic("get key : " + key + " from hash filed: " + field + " failed: " + err.Error()) return nil, errors.New("GET HASH CACHE FAILED " + err.Error())
} }
return result return result, nil
} }
// @method HGetAll // @method HGetAll
// @description: 获取hash表中的全部数据 // @description: 获取hash表中的全部数据
// @receiver c // @receiver c
// @param key string // @param key string
// @return interface{} // @return interface{}
func (c *cache) HGetAll(key string) map[string]string { func (c *cache) HGetAll(key string) (map[string]string, error) {
result, err := c.client.HGetAll(c.ctx, key).Result() result, err := c.client.HGetAll(c.ctx, key).Result()
if err != nil { if err != nil {
panic("get hash by key: " + key + " failed: " + err.Error()) return nil, errors.New("GET HASH CACHE ALL FAILED " + err.Error())
} }
return result return result, nil
} }
// @method HGetKeyAll // @method HGetKeyAll
// @description: 获取hash表中的全部key // @description: 获取hash表中的全部key
// @receiver c // @receiver c
// @param key string // @param key string
func (c *cache) HGetKeyAll(key string) []string { func (c *cache) HGetKeyAll(key string) ([]string, error) {
result, err := c.client.HKeys(c.ctx, key).Result() result, err := c.client.HKeys(c.ctx, key).Result()
if err != nil { if err != nil {
panic("get hash all key failed :" + err.Error() + " by cache key :" + key) return nil, errors.New("get hash all key failed :" + err.Error() + " by cache key :" + key)
} }
return result return result, nil
} }
// @method HIncrBy // @method HIncrBy
// @description: 为哈希表 key 中的指定字段的整数值加上增量 increment // @description: 为哈希表 key 中的指定字段的整数值加上增量 increment
// @receiver c // @receiver c
// @param key string // @param key string
// @param field string // @param field string
// @param incr int64 // @param incr int64
func (c *cache) HIncrBy(key, field string, incr int64) int64 { func (c *cache) HIncrBy(key, field string, incr int64) (int64, error) {
result, err := c.client.HIncrBy(c.ctx, key, field, incr).Result() result, err := c.client.HIncrBy(c.ctx, key, field, incr).Result()
if err != nil { if err != nil {
panic("hash incr by failed: " + err.Error()) return 0, errors.New("hash incr by failed: " + err.Error())
} }
return result return result, nil
} }
// @method HFloatIncrBy // @method HFloatIncrBy
// @description: 为哈希表 key 中的指定字段的浮点数值加上增量 increment // @description: 为哈希表 key 中的指定字段的浮点数值加上增量 increment
// @receiver c // @receiver c
// @param key string // @param key string
// @param field string // @param field string
// @param incr float64 // @param incr float64
// @return float64 // @return float64
func (c *cache) HFloatIncrBy(key, field string, incr float64) float64 { func (c *cache) HFloatIncrBy(key, field string, incr float64) (float64, error) {
result, err := c.client.HIncrByFloat(c.ctx, key, field, incr).Result() result, err := c.client.HIncrByFloat(c.ctx, key, field, incr).Result()
if err != nil { if err != nil {
panic("hash incr float by failed: " + err.Error()) return 0, errors.New("hash incr float by failed: " + err.Error())
} }
return result return result, nil
} }
// @method HGetValAll // @method HGetValAll
// @description: 获取hash表中全部的value 值 // @description: 获取hash表中全部的value 值
// @receiver c // @receiver c
// @param key string // @param key string
func (c *cache) HGetValAll(key string) []string { func (c *cache) HGetValAll(key string) ([]string, error) {
result, err := c.client.HVals(c.ctx, key).Result() result, err := c.client.HVals(c.ctx, key).Result()
if err != nil { if err != nil {
panic("get hash all value failed: " + err.Error()) return nil, errors.New("get hash all value failed: " + err.Error())
} }
return result return result, nil
} }
// @method HDelete // @method HDelete
// @description: 删除hash表中一个或多个字段 // @description: 删除hash表中一个或多个字段
// @receiver c // @receiver c
// @param key string // @param key string
// @param fields ...string // @param fields ...string
func (c *cache) HDelete(key string, fields ...string) int64 { func (c *cache) HDelete(key string, fields ...string) (int64, error) {
result, err := c.client.HDel(c.ctx, key, fields...).Result() result, err := c.client.HDel(c.ctx, key, fields...).Result()
if err != nil { if err != nil {
panic("delete hash filed failed: " + err.Error()) return 0, errors.New("delete hash filed failed: " + err.Error())
} }
return result return result, nil
} }

3
go.mod
View File

@ -2,8 +2,9 @@ module github.com/cowardmrx/rds_cache_go
go 1.17 go 1.17
require github.com/go-redis/redis/v8 v8.11.4
require ( require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/go-redis/redis/v8 v8.11.4 // indirect
) )