cache

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2019 License: MIT Imports: 4 Imported by: 0

README

Cache wrapper for web applications.

The primary goal is to simplify caching of responses.

Adds guava-style loading cache and support of scopes for partial flushes. Provides in-memory NewMemoryCache on top of hashicorp/golang-lru and defines basic interface for other implementations.

In addition to Get and Flush methods, memory cache also support limits for a single value size, number of keys and total memory utilization. PostFlushFn adds ability to call a function on flush completion.

Install and update

go get -u github.com/go-pkgz/rest/cache

Technical details

  • Cache keeps data in a simple key:value format.
  • Key is a type, created with Key(site_id) where site_id represents independent bucket in the cache. For simple cases can be set to an empty string.
  • Particular key set by Key.ID(string)
  • Key may contain optional scopes (list of string). They not affect retrieval and used for partial (scoped) invalidation only.
  • Cache is safe for concurrent use.
  • Value is []byte.
  • Get method returns from the cache if the key already in. Overwise executes passed function and saves results.
  • Special fake implementation cache.Nop satisfies LoadingCache interface and can be used to disable any caching

Usage

    // create in-memory cache with max keys=50, total (max) size=2000 and max cached size of a record = 200
    lc, err := cache.NewMemoryCache(cache.MaxKeys(50), cacheMaxCacheSize(2000), cache.MaxValSize(200)) 
    if err != nil {
        panic(err)
    }
    ...

    // load cached value for key1. Call func if not cached yet or evicted
    res, err = lc.Get(cache.NewKey("site1").ID("key1").Scopes("scope1"), func() ([]byte, error) {
		return []byte("1234567890"), nil
    }) 
    
    lc.Flush(cache.Flusher("site1").Scopes("scope1")) // invalidate cache for scope1

Documentation

Overview

Package cache implements a wrapper on top of hashicorp/golang-lru with guava-style loader func. In addition for Get (i.e. load from cache if found and put to cache if absent) it adds a Key struct with record ID, site ID and invalidation scopes. Scopes used to evict matching records. Additional limits added for total cache size, mac number of keys and max size of the value. If exceeded it won't put to cache but will call loader func. Usually the cache involved on []byte response level, i.e. post-marshaling right before response's send.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FlusherRequest

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

FlusherRequest used as input for cache.Flush

func Flusher

func Flusher(siteID string) FlusherRequest

Flusher makes new FlusherRequest with empty scopes

func (FlusherRequest) Scopes

func (f FlusherRequest) Scopes(scopes ...string) FlusherRequest

Scopes adds scopes to FlusherRequest

type Key

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

Key for cache

func NewKey

func NewKey(site string) Key

NewKey makes keys for site

func ParseKey

func ParseKey(fullKey string) (Key, error)

ParseKey gets compound key created by Key func and split it to the actual key and scopes

func (Key) ID

func (k Key) ID(id string) Key

ID sets key id

func (Key) Merge

func (k Key) Merge() string

Merge makes full string key from primary key and scopes

func (Key) Scopes

func (k Key) Scopes(scopes ...string) Key

Scopes of the key

type LoadingCache

type LoadingCache interface {
	Get(key Key, fn func() ([]byte, error)) (data []byte, err error) // load from cache if found or put to cache and return
	Flush(req FlusherRequest)                                        // evict matched records
}

LoadingCache defines interface for caching

func NewMemoryCache

func NewMemoryCache(options ...Option) (LoadingCache, error)

NewMemoryCache makes memoryCache implementation

type Nop

type Nop struct{}

Nop does nothing for caching, passing fn call only

func (*Nop) Flush

func (n *Nop) Flush(req FlusherRequest)

Flush does nothing for NoopCache

func (*Nop) Get

func (n *Nop) Get(key Key, fn func() ([]byte, error)) (data []byte, err error)

Get calls fn, no actual caching

type Option

type Option func(lc cacheWithOpts) error

Option func type

func MaxCacheSize

func MaxCacheSize(max int64) Option

MaxCacheSize functional option defines the total size of cached data. By default it is 0, which means unlimited.

func MaxKeys

func MaxKeys(max int) Option

MaxKeys functional option defines how many keys to keep. By default it is 0, which means unlimited.

func MaxValSize

func MaxValSize(max int) Option

MaxValSize functional option defines the largest value's size allowed to be cached By default it is 0, which means unlimited.

func PostFlushFn

func PostFlushFn(postFlushFn func()) Option

PostFlushFn functional option defines how callback function called after each Flush.

Jump to

Keyboard shortcuts

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