diff --git a/.gitignore b/.gitignore index 2a06a711..b9f5d61c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,12 +3,4 @@ .svn .idea .vscode -*.log -git.sh -gittag.sh -gittagdel.sh -gomod@latest.sh -/gitmod.sh -tests/ -/tests -/tests/ +*.log \ No newline at end of file diff --git a/utils/goredis/Iterator.go b/utils/goredis/Iterator.go index 1bbe8f8b..6c4957b1 100644 --- a/utils/goredis/Iterator.go +++ b/utils/goredis/Iterator.go @@ -5,9 +5,12 @@ type Iterator struct { index int } +// NewIterator 构造函数 func NewIterator(data []interface{}) *Iterator { return &Iterator{data: data} } + +// HasNext 是否有下一个 func (i *Iterator) HasNext() bool { if i.data == nil || len(i.data) == 0 { return false @@ -15,6 +18,7 @@ func (i *Iterator) HasNext() bool { return i.index < len(i.data) } +// Next 循环下一个 func (i *Iterator) Next() (Ret interface{}) { Ret = i.data[i.index] i.index = i.index + 1 diff --git a/utils/goredis/simple_cache.go b/utils/goredis/simple_cache.go index 52996426..abcbd0a8 100644 --- a/utils/goredis/simple_cache.go +++ b/utils/goredis/simple_cache.go @@ -23,6 +23,7 @@ type SimpleCache struct { Serializer string // 序列化方式 } +// NewSimpleCache 构造函数 func (app *App) NewSimpleCache(operation *StringOperation, expire time.Duration, serializer string) *SimpleCache { return &SimpleCache{ Operation: operation, // 操作类 diff --git a/utils/goredis/simple_interface_cache.go b/utils/goredis/simple_interface_cache.go new file mode 100644 index 00000000..246cc25f --- /dev/null +++ b/utils/goredis/simple_interface_cache.go @@ -0,0 +1,43 @@ +package goredis + +import ( + "encoding/json" + "time" +) + +type DBGttInterfaceFunc func() interface{} + +// SimpleInterfaceCache 缓存 +type SimpleInterfaceCache struct { + Operation *StringOperation // 操作类 + Expire time.Duration // 过期时间 + DBGetter DBGttInterfaceFunc // 缓存不存在的操作 DB +} + +// NewSimpleInterfaceCache 构造函数 +func (app *App) NewSimpleInterfaceCache(operation *StringOperation, expire time.Duration) *SimpleInterfaceCache { + return &SimpleInterfaceCache{ + Operation: operation, // 操作类 + Expire: expire, // 过期时间 + } +} + +// SetCache 设置缓存 +func (c *SimpleInterfaceCache) SetCache(key string, value interface{}) { + c.Operation.Set(key, value, WithExpire(c.Expire)).Unwrap() +} + +// GetCache 获取缓存 +func (c *SimpleInterfaceCache) GetCache(key string) (ret string) { + f := func() string { + obj := c.DBGetter() + b, err := json.Marshal(obj) + if err != nil { + return "" + } + return string(b) + } + ret = c.Operation.Get(key).UnwrapOrElse(f) + c.SetCache(key, ret) + return +} diff --git a/utils/goredis/simple_json_cache.go b/utils/goredis/simple_json_cache.go new file mode 100644 index 00000000..a1809259 --- /dev/null +++ b/utils/goredis/simple_json_cache.go @@ -0,0 +1,43 @@ +package goredis + +import ( + "encoding/json" + "time" +) + +type DBGttJsonFunc func() interface{} + +// SimpleJsonCache 缓存 +type SimpleJsonCache struct { + Operation *StringOperation // 操作类 + Expire time.Duration // 过期时间 + DBGetter DBGttJsonFunc // 缓存不存在的操作 DB +} + +// NewSimpleJsonCache 构造函数 +func (app *App) NewSimpleJsonCache(operation *StringOperation, expire time.Duration) *SimpleJsonCache { + return &SimpleJsonCache{ + Operation: operation, // 操作类 + Expire: expire, // 过期时间 + } +} + +// SetCache 设置缓存 +func (c *SimpleJsonCache) SetCache(key string, value interface{}) { + c.Operation.Set(key, value, WithExpire(c.Expire)).Unwrap() +} + +// GetCache 获取缓存 +func (c *SimpleJsonCache) GetCache(key string) (ret interface{}) { + f := func() string { + obj := c.DBGetter() + b, err := json.Marshal(obj) + if err != nil { + return "" + } + return string(b) + } + ret = c.Operation.Get(key).UnwrapOrElse(f) + c.SetCache(key, ret) + return +} diff --git a/utils/goredis/simple_sring_cache.go b/utils/goredis/simple_sring_cache.go new file mode 100644 index 00000000..9d49c860 --- /dev/null +++ b/utils/goredis/simple_sring_cache.go @@ -0,0 +1,37 @@ +package goredis + +import ( + "time" +) + +type DBGttStringFunc func() string + +// SimpleStringCache 缓存 +type SimpleStringCache struct { + Operation *StringOperation // 操作类 + Expire time.Duration // 过期时间 + DBGetter DBGttStringFunc // 缓存不存在的操作 DB +} + +// NewSimpleStringCache 构造函数 +func (app *App) NewSimpleStringCache(operation *StringOperation, expire time.Duration) *SimpleStringCache { + return &SimpleStringCache{ + Operation: operation, // 操作类 + Expire: expire, // 过期时间 + } +} + +// SetCache 设置缓存 +func (c *SimpleStringCache) SetCache(key string, value string) { + c.Operation.Set(key, value, WithExpire(c.Expire)).Unwrap() +} + +// GetCache 获取缓存 +func (c *SimpleStringCache) GetCache(key string) (ret string) { + f := func() string { + return c.DBGetter() + } + ret = c.Operation.Get(key).UnwrapOrElse(f) + c.SetCache(key, ret) + return +} diff --git a/utils/goredis/slice_result.go b/utils/goredis/slice_result.go index 2bdb9311..d04737ef 100644 --- a/utils/goredis/slice_result.go +++ b/utils/goredis/slice_result.go @@ -1,12 +1,11 @@ package goredis -import "log" - type SliceResult struct { Result []interface{} Err error } +// NewSliceResult 构造函数 func NewSliceResult(result []interface{}, err error) *SliceResult { return &SliceResult{Result: result, Err: err} } @@ -14,7 +13,7 @@ func NewSliceResult(result []interface{}, err error) *SliceResult { // Unwrap 空值情况下返回错误 func (r *SliceResult) Unwrap() []interface{} { if r.Err != nil { - log.Fatal(r.Err) + panic(r.Err) } return r.Result } diff --git a/utils/goredis/string_result.go b/utils/goredis/string_result.go index 90a7f1cc..d9c2c81f 100644 --- a/utils/goredis/string_result.go +++ b/utils/goredis/string_result.go @@ -1,20 +1,22 @@ package goredis -import "log" - type StringResult struct { - Result string - Err error + Result string // 结果 + Err error // 错误 } +// NewStringResult 构造函数 func NewStringResult(result string, err error) *StringResult { - return &StringResult{Result: result, Err: err} + return &StringResult{ + Result: result, + Err: err, + } } // Unwrap 空值情况下返回错误 func (r *StringResult) Unwrap() string { if r.Err != nil { - log.Fatal(r.Err) + panic(r.Err) } return r.Result } @@ -27,6 +29,7 @@ func (r *StringResult) UnwrapOr(defaults string) string { return r.Result } +// UnwrapOrElse 空值情况下设置返回其他 func (r *StringResult) UnwrapOrElse(f func() string) string { if r.Err != nil { return f()