Documentation ¶
Index ¶
- Variables
- func FileTtlCache(key string, f func() (b []byte, ttl time.Duration, err error)) (b []byte, err error)
- func MustFileChangeCache(key string, pathList []string, f func())
- func MustKvdbGet(s string, obj interface{}) bool
- func MustKvdbGetBytes(s string) (b []byte)
- func MustKvdbSet(s string, obj interface{})
- func MustKvdbSetBytes(s string, b []byte)
- func MustMd5FileChangeCache(key string, pathList []string, f func())
- type AsyncTtlCache
- type MemoryTtlCacheV2
- type TtlCache
Constants ¶
This section is empty.
Variables ¶
var CacheMiss = errors.New("cache miss")
var DoNotNeedCache = errors.New("do not need cache")
Functions ¶
func FileTtlCache ¶
func FileTtlCache(key string, f func() (b []byte, ttl time.Duration, err error)) (b []byte, err error)
文件ttl缓存实现,每次都会读取文件,由于没有泛型,此处需要调用者自行解决序列化问题. 2.存储在文件里面,以便重启之后可以使用. 3.当缓存存在的时候,并且ttl在时间范围内,使用ttl,当缓存不存在的时候,使用回调拉取数据.当缓存过期的时候,使用回调拉取数据. 4.每一次使用都会读取文件 5.当某一次缓存拉取出现错误的时候,直接返回错误给调用者
func MustFileChangeCache ¶
这个函数bug太多,以废弃,请使用 MustMd5FileChangeCache 根据文件变化,对f这个请求进行缓存 key表示这件事情的缓存key pathList表示需要监控的目录 文件列表里面如果有文件不存在,会运行代码 已知bug,小于1秒的修改不能被检测到. @deprecated
func MustKvdbSet ¶
func MustKvdbSet(s string, obj interface{})
func MustKvdbSetBytes ¶
func MustMd5FileChangeCache ¶
key 表示一组缓存,pathList 即这组缓存相关的文件 比较容易用错的情况: key := 1 pathList := []string{"/a.txt","/b.txt"} a.txt 或者 b.txt 中任意有文件发生变化,这个 key 对应的缓存都会被更新 如果要对单个文件进行缓存控制,那么应该一个文件一个 key
Types ¶
type AsyncTtlCache ¶
type AsyncTtlCache struct {
// contains filtered or unexported fields
}
会在缓存超时的时候,异步更新缓存,并且返回前一个数据.
func NewAsyncCache ¶
func NewAsyncCache() *AsyncTtlCache
func (*AsyncTtlCache) DoWithTtl ¶
func (s *AsyncTtlCache) DoWithTtl(key string, f func() (value interface{}, ttl uint32, canSave bool)) (value interface{}, ttl uint32)
如果缓存不存在,会同步查询 如果缓存过期,会异步查询,以便下次请求的时候有这个数据 如果你把needcache设置为false,表示这个请求本次不进行缓存(应该是什么地方出现错误了,具体错误请自行处理) 1.如果缓存不存在,会同步查询 2.如果缓存过去,会异步查询,并返回旧的数据 3.如果不要求保存数据,不会把数据保存到缓存中.
- 如果缓存不存在,返回f.value f.ttl
type MemoryTtlCacheV2 ¶
type MemoryTtlCacheV2 struct {
// contains filtered or unexported fields
}
请调用 NewTtlCache() 初始化
type TtlCache ¶
type TtlCache struct {
// contains filtered or unexported fields
}
请调用 NewTtlCache() 初始化
func NewTtlCache ¶
func NewTtlCache() *TtlCache