From 1739fc2e85b193d9d65866114d2371bd7125cf9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Sat, 25 Dec 2021 09:52:46 +0800 Subject: [PATCH] update redis --- utils/{redis => goredis}/Iterator.go | 2 +- utils/{redis => goredis}/client.go | 2 +- utils/{redis => goredis}/operation_attr.go | 2 +- utils/goredis/simple_cache.go | 56 ++++++++++ utils/{redis => goredis}/slice_result.go | 6 +- utils/{redis => goredis}/string_operation.go | 2 +- utils/{redis => goredis}/string_result.go | 6 +- utils/redis/simple_cache.go | 102 ------------------- 8 files changed, 68 insertions(+), 110 deletions(-) rename utils/{redis => goredis}/Iterator.go (95%) rename utils/{redis => goredis}/client.go (99%) rename utils/{redis => goredis}/operation_attr.go (97%) create mode 100644 utils/goredis/simple_cache.go rename utils/{redis => goredis}/slice_result.go (92%) rename utils/{redis => goredis}/string_operation.go (97%) rename utils/{redis => goredis}/string_result.go (92%) delete mode 100644 utils/redis/simple_cache.go diff --git a/utils/redis/Iterator.go b/utils/goredis/Iterator.go similarity index 95% rename from utils/redis/Iterator.go rename to utils/goredis/Iterator.go index 3a89dbf5..1bbe8f8b 100644 --- a/utils/redis/Iterator.go +++ b/utils/goredis/Iterator.go @@ -1,4 +1,4 @@ -package redis +package goredis type Iterator struct { data []interface{} diff --git a/utils/redis/client.go b/utils/goredis/client.go similarity index 99% rename from utils/redis/client.go rename to utils/goredis/client.go index fda3fe66..d3b9e2b8 100644 --- a/utils/redis/client.go +++ b/utils/goredis/client.go @@ -1,4 +1,4 @@ -package redis +package goredis import ( "context" diff --git a/utils/redis/operation_attr.go b/utils/goredis/operation_attr.go similarity index 97% rename from utils/redis/operation_attr.go rename to utils/goredis/operation_attr.go index 6d5d165e..35009343 100644 --- a/utils/redis/operation_attr.go +++ b/utils/goredis/operation_attr.go @@ -1,4 +1,4 @@ -package redis +package goredis import "time" diff --git a/utils/goredis/simple_cache.go b/utils/goredis/simple_cache.go new file mode 100644 index 00000000..6a979a3a --- /dev/null +++ b/utils/goredis/simple_cache.go @@ -0,0 +1,56 @@ +package goredis + +import ( + "encoding/json" + "time" +) + +const ( + SerializerJson = "json" + SerializerString = "string" +) + +type JsonGttFunc func() interface{} + +type DBGttFunc func() string + +// SimpleCache 缓存 +type SimpleCache struct { + Operation *StringOperation // 操作类 + Expire time.Duration // 过去时间 + DBGetter DBGttFunc // 缓存不存在的操作 DB + JsonGetter JsonGttFunc // 缓存不存在的操作 JSON + Serializer string // 序列化方式 +} + +func NewSimpleCache(operation *StringOperation, expire time.Duration, serializer string) *SimpleCache { + return &SimpleCache{Operation: operation, Expire: expire, Serializer: serializer} +} + +// SetCache 设置缓存 +func (c *SimpleCache) SetCache(key string, value interface{}) { + c.Operation.Set(key, value, WithExpire(c.Expire)).Unwrap() +} + +// GetCache 获取缓存 +func (c *SimpleCache) GetCache(key string) (ret interface{}) { + if c.Serializer == SerializerJson { + f := func() string { + obj := c.JsonGetter() + b, err := json.Marshal(obj) + if err != nil { + return "" + } + return string(b) + } + ret = c.Operation.Get(key).UnwrapOrElse(f) + c.SetCache(key, ret) + } else if c.Serializer == SerializerString { + f := func() string { + return c.DBGetter() + } + ret = c.Operation.Get(key).UnwrapOrElse(f) + c.SetCache(key, ret) + } + return +} diff --git a/utils/redis/slice_result.go b/utils/goredis/slice_result.go similarity index 92% rename from utils/redis/slice_result.go rename to utils/goredis/slice_result.go index b237088b..2bdb9311 100644 --- a/utils/redis/slice_result.go +++ b/utils/goredis/slice_result.go @@ -1,4 +1,6 @@ -package redis +package goredis + +import "log" type SliceResult struct { Result []interface{} @@ -12,7 +14,7 @@ func NewSliceResult(result []interface{}, err error) *SliceResult { // Unwrap 空值情况下返回错误 func (r *SliceResult) Unwrap() []interface{} { if r.Err != nil { - panic(r.Err) + log.Fatal(r.Err) } return r.Result } diff --git a/utils/redis/string_operation.go b/utils/goredis/string_operation.go similarity index 97% rename from utils/redis/string_operation.go rename to utils/goredis/string_operation.go index b174ce75..0165d57a 100644 --- a/utils/redis/string_operation.go +++ b/utils/goredis/string_operation.go @@ -1,4 +1,4 @@ -package redis +package goredis import ( "context" diff --git a/utils/redis/string_result.go b/utils/goredis/string_result.go similarity index 92% rename from utils/redis/string_result.go rename to utils/goredis/string_result.go index 96190751..90a7f1cc 100644 --- a/utils/redis/string_result.go +++ b/utils/goredis/string_result.go @@ -1,4 +1,6 @@ -package redis +package goredis + +import "log" type StringResult struct { Result string @@ -12,7 +14,7 @@ func NewStringResult(result string, err error) *StringResult { // Unwrap 空值情况下返回错误 func (r *StringResult) Unwrap() string { if r.Err != nil { - panic(r.Err) + log.Fatal(r.Err) } return r.Result } diff --git a/utils/redis/simple_cache.go b/utils/redis/simple_cache.go deleted file mode 100644 index 8be2fd7b..00000000 --- a/utils/redis/simple_cache.go +++ /dev/null @@ -1,102 +0,0 @@ -package redis - -import ( - "encoding/json" - "github.com/bitly/go-simplejson" - "time" -) - -const ( - SerializerJson = "json" - SerializerSimpleJson = "simplejson" - SerializerString = "string" -) - -type JsonGttFunc func() interface{} - -type SimpleJsonGttFunc func() *simplejson.Json - -type DBGttFunc func() string - -// SimpleCache 缓存 -type SimpleCache struct { - Operation *StringOperation // 操作类 - Expire time.Duration // 过去时间 - DBGetter DBGttFunc // 缓存不存在的操作 DB - JsonGetter JsonGttFunc // 缓存不存在的操作 JSON - SimpleJsonGetter SimpleJsonGttFunc // 缓存不存在的操作 SimpleJson - Serializer string // 序列化方式 -} - -func NewSimpleCache(operation *StringOperation, expire time.Duration, serializer string) *SimpleCache { - return &SimpleCache{Operation: operation, Expire: expire, Serializer: serializer} -} - -// SetCache 设置缓存 -func (c *SimpleCache) SetCache(key string, value interface{}) { - c.Operation.Set(key, value, WithExpire(c.Expire)).Unwrap() -} - -// GetCache 获取缓存 -func (c *SimpleCache) GetCache(key string) (ret interface{}) { - if c.Serializer == SerializerJson { - f := func() string { - obj := c.JsonGetter() - b, err := json.Marshal(obj) - if err != nil { - return "" - } - return string(b) - } - ret = c.Operation.Get(key).UnwrapOrElse(f) - c.SetCache(key, ret) - } else if c.Serializer == SerializerString { - f := func() string { - return c.DBGetter() - } - ret = c.Operation.Get(key).UnwrapOrElse(f) - c.SetCache(key, ret) - } else if c.Serializer == SerializerSimpleJson { - f := func() string { - obj := c.SimpleJsonGetter() - encode, err := obj.Encode() - if err != nil { - return "" - } - return string(encode) - } - ret = c.Operation.Get(key).UnwrapOrElse(f) - c.SetCache(key, ret) - } - return -} - -// GetCacheSimpleJson 获取缓存配合SimpleJson插件 -func (c *SimpleCache) GetCacheSimpleJson(key string) (js *simplejson.Json) { - if c.Serializer == SerializerJson { - f := func() string { - obj := c.JsonGetter() - b, err := json.Marshal(obj) - if err != nil { - return "" - } - return string(b) - } - ret := c.Operation.Get(key).UnwrapOrElse(f) - c.SetCache(key, ret) - js, _ = simplejson.NewJson([]byte(ret)) - } else if c.Serializer == SerializerSimpleJson { - f := func() string { - obj := c.SimpleJsonGetter() - encode, err := obj.Encode() - if err != nil { - return "" - } - return string(encode) - } - ret := c.Operation.Get(key).UnwrapOrElse(f) - c.SetCache(key, ret) - js, _ = simplejson.NewJson([]byte(ret)) - } - return -}