- update redis
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
e5a9651aec
commit
0de8f4e2ca
@ -0,0 +1,26 @@
|
||||
package dorm
|
||||
|
||||
type Iterator struct {
|
||||
data []interface{}
|
||||
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
|
||||
}
|
||||
return i.index < len(i.data)
|
||||
}
|
||||
|
||||
// Next 循环下一个
|
||||
func (i *Iterator) Next() (Ret interface{}) {
|
||||
Ret = i.data[i.index]
|
||||
i.index = i.index + 1
|
||||
return
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package dorm
|
||||
|
||||
import "time"
|
||||
|
||||
type empty struct{}
|
||||
|
||||
const (
|
||||
AttrExpr = "expr" // 过期时间
|
||||
AttrNx = "nx" // 设置Nx
|
||||
)
|
||||
|
||||
type OperationAttr struct {
|
||||
Name string
|
||||
Value interface{}
|
||||
}
|
||||
|
||||
type OperationAttrs []*OperationAttr
|
||||
|
||||
func (a OperationAttrs) Find(name string) interface{} {
|
||||
for _, attr := range a {
|
||||
if attr.Name == name {
|
||||
return attr.Value
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// WithExpire 过期时间
|
||||
func WithExpire(t time.Duration) *OperationAttr {
|
||||
return &OperationAttr{Name: AttrExpr, Value: t}
|
||||
}
|
||||
|
||||
func WithNX() *OperationAttr {
|
||||
return &OperationAttr{Name: AttrNx, Value: empty{}}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package dorm
|
||||
|
||||
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 // 序列化方式
|
||||
}
|
||||
|
||||
// NewSimpleCache 构造函数
|
||||
func (c *RedisClient) NewSimpleCache(operation *StringOperation, expire time.Duration, serializer string) *SimpleCache {
|
||||
return &SimpleCache{
|
||||
Operation: operation, // 操作类
|
||||
Expire: expire, // 过去时间
|
||||
Serializer: serializer, // 缓存不存在的操作 DB
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
type DBGttInterfaceFunc func() interface{}
|
||||
|
||||
// SimpleInterfaceCache 缓存
|
||||
type SimpleInterfaceCache struct {
|
||||
Operation *SimpleOperation // 操作类
|
||||
Expire time.Duration // 过期时间
|
||||
DBGetter DBGttInterfaceFunc // 缓存不存在的操作 DB
|
||||
}
|
||||
|
||||
// NewSimpleInterfaceCache 构造函数
|
||||
func (c *RedisClient) NewSimpleInterfaceCache(operation *SimpleOperation, 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 interface{}) {
|
||||
f := func() interface{} {
|
||||
return c.DBGetter()
|
||||
}
|
||||
ret = c.Operation.Get(key).UnwrapOrElse(f)
|
||||
c.SetCache(key, ret)
|
||||
log.Println(ret)
|
||||
return ret
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
type DBGttJsonFunc func() interface{}
|
||||
|
||||
// SimpleJsonCache 缓存
|
||||
type SimpleJsonCache struct {
|
||||
Operation *StringOperation // 操作类
|
||||
Expire time.Duration // 过期时间
|
||||
DBGetter DBGttJsonFunc // 缓存不存在的操作 DB
|
||||
}
|
||||
|
||||
// NewSimpleJsonCache 构造函数
|
||||
func (c *RedisClient) 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,35 @@
|
||||
package dorm
|
||||
|
||||
type SimpleResult struct {
|
||||
Result interface{}
|
||||
Err error
|
||||
}
|
||||
|
||||
// NewSimpleResult 构造函数
|
||||
func NewSimpleResult(result interface{}, err error) *SimpleResult {
|
||||
return &SimpleResult{Result: result, Err: err}
|
||||
}
|
||||
|
||||
// Unwrap 空值情况下返回错误
|
||||
func (r *SimpleResult) Unwrap() interface{} {
|
||||
if r.Err != nil {
|
||||
panic(r.Err)
|
||||
}
|
||||
return r.Result
|
||||
}
|
||||
|
||||
// UnwrapOr 空值情况下设置返回默认值
|
||||
func (r *SimpleResult) UnwrapOr(defaults interface{}) interface{} {
|
||||
if r.Err != nil {
|
||||
return defaults
|
||||
}
|
||||
return r.Result
|
||||
}
|
||||
|
||||
// UnwrapOrElse 空值情况下设置返回其他
|
||||
func (r *SimpleResult) UnwrapOrElse(f func() interface{}) interface{} {
|
||||
if r.Err != nil {
|
||||
return f()
|
||||
}
|
||||
return r.Result
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type DBGttStringFunc func() string
|
||||
|
||||
// SimpleStringCache 缓存
|
||||
type SimpleStringCache struct {
|
||||
Operation *StringOperation // 操作类
|
||||
Expire time.Duration // 过期时间
|
||||
DBGetter DBGttStringFunc // 缓存不存在的操作 DB
|
||||
}
|
||||
|
||||
// NewSimpleStringCache 构造函数
|
||||
func (c *RedisClient) 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
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package dorm
|
||||
|
||||
type SliceResult struct {
|
||||
Result []interface{}
|
||||
Err error
|
||||
}
|
||||
|
||||
// NewSliceResult 构造函数
|
||||
func NewSliceResult(result []interface{}, err error) *SliceResult {
|
||||
return &SliceResult{Result: result, Err: err}
|
||||
}
|
||||
|
||||
// Unwrap 空值情况下返回错误
|
||||
func (r *SliceResult) Unwrap() []interface{} {
|
||||
if r.Err != nil {
|
||||
panic(r.Err)
|
||||
}
|
||||
return r.Result
|
||||
}
|
||||
|
||||
// UnwrapOr 空值情况下设置返回默认值
|
||||
func (r *SliceResult) UnwrapOr(defaults []interface{}) []interface{} {
|
||||
if r.Err != nil {
|
||||
return defaults
|
||||
}
|
||||
return r.Result
|
||||
}
|
||||
|
||||
func (r *SliceResult) Iter() *Iterator {
|
||||
return NewIterator(r.Result)
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package dorm
|
||||
|
||||
type StringResult struct {
|
||||
Result string // 结果
|
||||
Err error // 错误
|
||||
}
|
||||
|
||||
// NewStringResult 构造函数
|
||||
func NewStringResult(result string, err error) *StringResult {
|
||||
return &StringResult{
|
||||
Result: result,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
|
||||
// Unwrap 空值情况下返回错误
|
||||
func (r *StringResult) Unwrap() string {
|
||||
if r.Err != nil {
|
||||
panic(r.Err)
|
||||
}
|
||||
return r.Result
|
||||
}
|
||||
|
||||
// UnwrapOr 空值情况下设置返回默认值
|
||||
func (r *StringResult) UnwrapOr(defaults string) string {
|
||||
if r.Err != nil {
|
||||
return defaults
|
||||
}
|
||||
return r.Result
|
||||
}
|
||||
|
||||
// UnwrapOrElse 空值情况下设置返回其他
|
||||
func (r *StringResult) UnwrapOrElse(f func() string) string {
|
||||
if r.Err != nil {
|
||||
return f()
|
||||
}
|
||||
return r.Result
|
||||
}
|
Loading…
Reference in new issue