Documentation ¶
Overview ¶
Package lcw adds a thin layer on top of lru cache and go-cache providing more limits and common interface. The primary method to get (and set) data to/from the cache is LoadingCache.Get retruning stored data for a given key or call provided func to retrive and store, similar to Guava loading cache. Limits allow max values for key size, number of keys, value size and total size of values in the cache. CacheStat gives general stats on cache performance. 3 flavours of cache provided - NoP (do-nothing cache), ExpirableCache (TTL based), and LruCache
Index ¶
- type CacheStat
- type ExpirableCache
- func (c *ExpirableCache) Delete(key string)
- func (c *ExpirableCache) Get(key string, fn func() (Value, error)) (data Value, err error)
- func (c *ExpirableCache) Invalidate(fn func(key string) bool)
- func (c *ExpirableCache) Peek(key string) (Value, bool)
- func (c *ExpirableCache) Purge()
- func (c *ExpirableCache) Stat() CacheStat
- type LoadingCache
- type LruCache
- type Nop
- type Option
- type Sizer
- type Value
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) Delete ¶ added in v0.3.0
func (c *ExpirableCache) Delete(key string)
Delete cache item by key
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 Delete(key string) // delete by key Purge() // clear cache Stat() CacheStat // cache stats }
LoadingCache defines guava-like cache with Get method returning cached value ao retrieving 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/" val, err := cache.Get(url, func() (val Value, err error) { return loadURL(url) }) if err != nil { log.Fatalf("can't load url %s, %v", url, err) } log.Print(val.(string)) // url not in cache, load data url = "https://radio-t.com/info/" val, err = cache.Get(url, func() (val Value, err error) { return loadURL(url) }) if err != nil { log.Fatalf("can't load url %s, %v", url, err) } log.Print(val.(string)) // url cached, skip load and get from the cache url = "https://radio-t.com/online/" val, err = cache.Get(url, func() (val Value, err error) { return loadURL(url) }) if err != nil { log.Fatalf("can't load url %s, %v", url, err) } log.Print(val.(string))
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.