kmgCache

package
v0.0.0-...-05317bf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2015 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CacheMiss = errors.New("cache miss")
View Source
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

func MustFileChangeCache(key string, pathList []string, f func())

这个函数bug太多,以废弃,请使用 MustMd5FileChangeCache 根据文件变化,对f这个请求进行缓存 key表示这件事情的缓存key pathList表示需要监控的目录 文件列表里面如果有文件不存在,会运行代码 已知bug,小于1秒的修改不能被检测到. @deprecated

func MustKvdbGet

func MustKvdbGet(s string, obj interface{}) bool

返回是否找到了数据

func MustKvdbGetBytes

func MustKvdbGetBytes(s string) (b []byte)

如果没有数据会返回nil

func MustKvdbSet

func MustKvdbSet(s string, obj interface{})

func MustKvdbSetBytes

func MustKvdbSetBytes(s string, b []byte)

func MustMd5FileChangeCache

func MustMd5FileChangeCache(key string, pathList []string, f func())

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.如果不要求保存数据,不会把数据保存到缓存中.

  1. 如果缓存不存在,返回f.value f.ttl

func (*AsyncTtlCache) GcThread

func (s *AsyncTtlCache) GcThread()

要有个进程在一边进行gc,避免内存泄漏

func (*AsyncTtlCache) Len

func (s *AsyncTtlCache) Len() int

里面数据的个数

type MemoryTtlCacheV2

type MemoryTtlCacheV2 struct {
	// contains filtered or unexported fields
}

请调用 NewTtlCache() 初始化

func NewMemoryTtlCacheV2

func NewMemoryTtlCacheV2() *MemoryTtlCacheV2

TODO 写一个close,以便可以关闭缓存

func (*MemoryTtlCacheV2) Do

func (s *MemoryTtlCacheV2) Do(key string, f func() (value interface{}, ttl time.Duration, err error)) (value interface{}, err error)

Ttl 内存缓存实现第二版 1.使用key表示不同的项 2.f 返回err 表示本次无法获取到信息,该err会被返回给调用者,并且此时调用者的value是nil 3.使用singleGroup避免大量请求同时访问某个一个key

func (*MemoryTtlCacheV2) GcThread

func (s *MemoryTtlCacheV2) GcThread()

要有个进程在一边进行gc,避免内存泄漏

type TtlCache

type TtlCache struct {
	// contains filtered or unexported fields
}

请调用 NewTtlCache() 初始化

func NewTtlCache

func NewTtlCache() *TtlCache

func (*TtlCache) DoWithTtl

func (s *TtlCache) DoWithTtl(key string, f func() (value interface{}, ttl uint32, err error)) (value interface{}, ttl uint32, err error)

如果f 返回的 err不是空,则不会把数据保存在缓存里面,但是会返回另外2项.

func (*TtlCache) GcThread

func (s *TtlCache) GcThread()

要有个进程在一边进行gc,避免内存泄漏

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL