Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
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") )
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 ¶
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
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 Manager ¶
Manager consolidates caches and allows manipulations on them
func (*Manager) InvalidateAll ¶
func (m *Manager) InvalidateAll()
InvalidateAll invalidates all caches in Manager
func (*Manager) InvalidateTags ¶
InvalidateTags invalidates caches which have specific tags