Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ExpirableCache ¶ added in v0.2.0
type ExpirableCache struct { CacheStat // contains filtered or unexported fields }
ExpirableCache implements LoadingCache with TTL.
func NewExpirableCache ¶ added in v0.2.0
func NewExpirableCache(opts ...Option) (*ExpirableCache, error)
NewExpirableCache makes expirable LoadingCache implementation, 1000 max keys by default and 5s TTL
func (*ExpirableCache) Get ¶ added in v0.2.0
Get gets value by key or load with fn if not found in cache
func (*ExpirableCache) Invalidate ¶ added in v0.2.0
func (c *ExpirableCache) Invalidate(fn func(key string) bool)
Invalidate removes keys with passed predicate fn, i.e. fn(key) should be true to get evicted
func (*ExpirableCache) Peek ¶ added in v0.2.0
func (c *ExpirableCache) Peek(key string) (Value, bool)
Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.
func (*ExpirableCache) Purge ¶ added in v0.2.0
func (c *ExpirableCache) Purge()
Purge clears the cache completely.
func (*ExpirableCache) Stat ¶ added in v0.2.0
func (c *ExpirableCache) Stat() CacheStat
Stat returns cache statistics
type LoadingCache ¶
type LoadingCache interface { Get(key string, fn func() (Value, error)) (val Value, err error) // load or get from cache Peek(key string) (Value, bool) // get from cache by key Invalidate(fn func(key string) bool) // invalidate items for func(key) == true Purge() // clear cache Stat() CacheStat // cache stats }
LoadingCache defines guava-like cache with Get method returning cached value ao retriving it if not in cache
type LruCache ¶ added in v0.2.0
type LruCache struct { CacheStat // contains filtered or unexported fields }
LruCache wraps lru.LruCache with laoding cache Get and size limits
Example ¶
LruCache illustrates the use of LRU loading cache
// load page function loadURL := func(url string) (string, error) { resp, err := http.Get(url) if err != nil { return "", err } resp.Body.Close() b, err := ioutil.ReadAll(resp.Body) if err != nil { return "", err } return string(b), nil } // fixed size LRU cache, 100 items, up to 10k in total size cache, err := NewLruCache(MaxKeys(100), MaxCacheSize(10*1024)) if err != nil { log.Fatalf("can't make lru cache, %v", err) } // url not in cache, load data url := "https://radio-t.com/online/" cache.Get(url, func() (val Value, err error) { return loadURL(url) }) // url not in cache, load data url = "https://radio-t.com/info/" cache.Get(url, func() (val Value, err error) { return loadURL(url) }) // url cached, skip load and get from the cache url = "https://radio-t.com/online/" cache.Get(url, func() (val Value, err error) { return loadURL(url) })
Output:
func NewLruCache ¶ added in v0.2.0
NewLruCache makes LRU LoadingCache implementation, 1000 max keys by default
func (*LruCache) Invalidate ¶ added in v0.2.0
Invalidate removes keys with passed predicate fn, i.e. fn(key) should be true to get evicted
func (*LruCache) Peek ¶ added in v0.2.0
Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.
type Nop ¶
type Nop struct{}
Nop is do-nothing implementation of LoadingCache
func (*Nop) Invalidate ¶
Invalidate does nothing for nop cache
type Option ¶
type Option func(o *options) error
Option func type
func MaxCacheSize ¶
MaxCacheSize functional option defines the total size of cached data. By default it is 0, which means unlimited.
func MaxKeySize ¶
MaxKeySize functional option defines the largest key's size allowed to be used in cache By default it is 0, which means unlimited.
func MaxKeys ¶
MaxKeys functional option defines how many keys to keep. By default it is 0, which means unlimited.
func MaxValSize ¶
MaxValSize functional option defines the largest value's size allowed to be cached By default it is 0, which means unlimited.