update redis

master
李光春 2 years ago
parent ad44d7efec
commit 6de1c147b8

10
.gitignore vendored

@ -3,12 +3,4 @@
.svn
.idea
.vscode
*.log
git.sh
gittag.sh
gittagdel.sh
gomod@latest.sh
/gitmod.sh
tests/
/tests
/tests/
*.log

@ -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

@ -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, // 操作类

@ -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
}

@ -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
}

@ -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
}

@ -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
}

@ -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()

Loading…
Cancel
Save