cachery

package module
v0.0.0-...-41babec Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2018 License: MIT Imports: 9 Imported by: 0

README

Cachery - caching framework

FOSSA Status Build Status Coverage Status Go Report Card GoDoc

Modular and feature-rich caching framework

Features include:

  • Multiple cache pools
  • Invalidation grouping with tags
  • Cluster support
  • Expire and stale behavior
  • Expvar support
  • Modularity:
    • serializers
    • cache logic modules
    • storage drivers
    • driver wrappers

Quickstart

Install/Update

go get -u github.com/DLag/cachery

Basic usage
func fetcher(key interface{}) (interface{}, error) {
    res, err := db.GetDataByKey(key)
    return res, err
}

// Add cache to the cache manager
cachery.Add(cachery.NewDefault("some_cache", cachery.Config{
    // Time after cache become stale and should be updated from fetcher in background
    Expire: time.Second * 20,
    // Time after cache become outdated and should be updated immediately
    Lifetime: time.Second * 120,
    // Serializer is reusable.
    // There is JSON serializer as well, but it is slower and has some limitations like nanoseconds in time.Time.
    Serializer: &cachery.GobSerializer{},
    // Driver is reusable for different caches
    Driver:     inmemory.Default(),
    // Fetcher is function that fetch data from the underlying storage(e.g. database)
    // could be nil if you use fetcher parameter of Get function
    Fetcher:    fetcher,
    // Expvar will be used to populate cache statistics through expvar package
    // It could be nil if you don't need it
    Expvar: nil,
    // Tags allow you invalidate all caches in Manager which have specified tags
    // could be nil
    Tags: []string{"tag1", "tag2"},
}))

// Get cache from manager
c := cachery.Get("some_cache")
var val string

c.Get("some_key", &val, nil)
// Or override fetcher function from config
c.Get("some_key", &val, fetcher)

// Invalidate all keys
c.InvalidateAll()
// Invalidate single key
c.Invalidate("some_key")

//Invalidate all caches in manager
cachery.InvalidateAll()
//Invalidate caches in manager by tag
cachery.InvalidateTags("tag1")

Examples

See examples to understand usage:

Staying up to date

To update Cachery to the latest version, use go get -u github.com/DLag/cachery.

Supported go versions

We support the three major Go versions, which are 1.8, 1.9 and 1.10 at the moment.

Contributing

Please feel free to submit issues, fork the repository and send pull requests!

When submitting an issue, we ask that you please include a complete test function that demonstrates the issue. Extra kudos for those using Cachery to write the test code that demonstrates it.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNilSerializer serializer in Config isn't set
	ErrNilSerializer = errors.New("cachery: Serializer is nil")
	// ErrNilFetcher fetcher in Get function and in Config isn't set
	ErrNilFetcher = errors.New("cachery: Fetcher is nil")
)
View Source
var (

	// Add cache to the internal cache Manager
	Add = caches.Add
	// Get cache from the internal Manager by its name or returns nil if could not find it
	Get = caches.Get
	// InvalidateTags invalidates caches of internal Manager which have specific tags
	InvalidateTags = caches.InvalidateTags
	// InvalidateAll invalidates all caches of internal Manager
	InvalidateAll = caches.InvalidateAll
)

Functions

func Key

func Key(key interface{}) string

Key returns representation of value which satisfies consistent key requirements

Types

type Cache

type Cache interface {
	// Get loads data to dst from cache or from fetcher function
	Get(key interface{}, dst interface{}, fetcher Fetcher) error
	// Name returns name of the cache
	Name() string
	// Invalidate specific key
	Invalidate(key interface{}) error
	// InvalidateTags invalidates cache if finds necessary tags
	InvalidateTags(tags ...string)
	// InvalidateAll invalidates all data from this cache
	InvalidateAll()
}

Cache describes cache object

type Config

type Config struct {
	// Expire when data in cache becomes stale but still usable and needs to be updated from fetcher
	Expire time.Duration
	// Lifetime when data in cache becomes outdated and needs to be updated from fetcher before use
	Lifetime time.Duration
	// Tags of the cache
	Tags []string
	// Serializer for objects
	Serializer Serializer
	// Driver cache storage driver (e.g. Redis, Memcached, Memory)
	Driver Driver
	// Fetcher optional instance of Fetcher function, could be nil if fetcher parameter of Get function is used
	Fetcher Fetcher
	// Driver cache storage driver (e.g. Redis, Memcached, Memory)
	Expvar *expvar.Map
}

Config describes configuration of cache

type DefaultCache

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

DefaultCache default implementation of caching logic

func NewDefault

func NewDefault(name string, config Config) *DefaultCache

NewDefault creates an instance of DefaultCache

func (*DefaultCache) Get

func (c *DefaultCache) Get(key interface{}, obj interface{}, fetcher Fetcher) error

Get loads data to dst from cache or from fetcher function

func (*DefaultCache) Invalidate

func (c *DefaultCache) Invalidate(key interface{}) error

Invalidate specific key

func (*DefaultCache) InvalidateAll

func (c *DefaultCache) InvalidateAll()

InvalidateAll invalidates all data from this cache

func (*DefaultCache) InvalidateTags

func (c *DefaultCache) InvalidateTags(tags ...string)

InvalidateTags invalidates cache if finds necessary tags

func (*DefaultCache) Name

func (c *DefaultCache) Name() string

Name returns name of the cache

type Driver

type Driver interface {
	// Get loads key from the cache store if it is not outdated
	Get(cacheName string, key interface{}) (val []byte, ttl time.Duration, err error)
	// Set saves key to the cache store
	Set(cacheName string, key interface{}, val []byte, ttl time.Duration) (err error)
	// Invalidate removes the key from the cache store
	Invalidate(cacheName string, key interface{}) error
	// InvalidateAll removes all keys from the cache store
	InvalidateAll(cacheName string)
}

Driver describes storage driver interface

type Fetcher

type Fetcher func(key interface{}) (interface{}, error)

Fetcher is a function which returns data from origin data store

type GobSerializer

type GobSerializer struct{}

GobSerializer implements Serializer with Gob serialization

func (GobSerializer) Deserialize

func (GobSerializer) Deserialize(src []byte, obj interface{}) error

Deserialize deserializes []byte to object

func (GobSerializer) Serialize

func (GobSerializer) Serialize(obj interface{}) ([]byte, error)

Serialize serializes object to []byte

type JSONSerializer

type JSONSerializer struct{}

JSONSerializer implements Serializer with JSON serialization

func (JSONSerializer) Deserialize

func (JSONSerializer) Deserialize(src []byte, obj interface{}) error

Deserialize deserializes []byte to object

func (JSONSerializer) Serialize

func (JSONSerializer) Serialize(obj interface{}) ([]byte, error)

Serialize serializes object to []byte

type Keyer

type Keyer interface {
	Key() string
}

Keyer provides idempotent key

type Manager

type Manager struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Manager consolidates caches and allows manipulations on them

func (*Manager) Add

func (m *Manager) Add(cache ...Cache) *Manager

Add cache to Manager

func (*Manager) Get

func (m *Manager) Get(name string) Cache

Get cache from Manager by its name or returns nil if could not find it

func (*Manager) InvalidateAll

func (m *Manager) InvalidateAll()

InvalidateAll invalidates all caches in Manager

func (*Manager) InvalidateTags

func (m *Manager) InvalidateTags(tags ...string)

InvalidateTags invalidates caches which have specific tags

type Serializer

type Serializer interface {
	// Serialize serializes object to []byte
	Serialize(obj interface{}) ([]byte, error)
	// Deserialize deserializes []byte to object
	Deserialize(src []byte, obj interface{}) error
}

Serializer describes serializer interface

Directories

Path Synopsis
drivers
examples
simple command
webserver command
wrappers

Jump to

Keyboard shortcuts

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