parent
760dc44ee5
commit
1739fc2e85
@ -1,4 +1,4 @@
|
|||||||
package redis
|
package goredis
|
||||||
|
|
||||||
type Iterator struct {
|
type Iterator struct {
|
||||||
data []interface{}
|
data []interface{}
|
@ -1,4 +1,4 @@
|
|||||||
package redis
|
package goredis
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
@ -1,4 +1,4 @@
|
|||||||
package redis
|
package goredis
|
||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
@ -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
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package redis
|
package goredis
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
@ -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
|
|
||||||
}
|
|
Loading…
Reference in new issue