Documentation ¶
Overview ¶
Package cache implements an caches for objects.
Index ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type Cacher ¶
type Cacher interface { // Closer closes the cache, cleaning up any stale entries. A closed cache is // no longer valid and attempts to call methods should return an error or // (less preferred) panic. io.Closer // Fetch retrieves the named item from the cache. If the item does not exist, // it calls FetchFunc to create the item. If FetchFunc returns an error, the // error is bubbled up the stack and no value is cached. If FetchFunc // succeeds, the value is cached for the provided TTL. Fetch(context.Context, string, interface{}, time.Duration, FetchFunc) error // Read gets an item from the cache and reads it into the provided interface. // If it does not exist, it returns ErrNotFound. Read(context.Context, string, interface{}) error // Write adds an item to the cache, overwriting if it already exists, caching // for TTL. It returns any errors that occur on writing. Write(context.Context, string, interface{}, time.Duration) error // Delete removes an item from the cache, returning any errors that occur. Delete(context.Context, string) error }
Cacher is an interface that defines caching.
func NewInMemory ¶
func NewInMemory(i *InMemoryConfig) (Cacher, error)
NewInMemory creates a new in-memory cache.
func NewRedis ¶
func NewRedis(i *RedisConfig) (Cacher, error)
NewRedis creates a new in-memory cache.
type CacherType ¶
type CacherType string
CacherType represents a type of cacher.
const ( TypeNoop CacherType = "NOOP" TypeInMemory CacherType = "IN_MEMORY" TypeRedis CacherType = "REDIS" )
type Config ¶
type Config struct { Type CacherType `env:"TYPE, default=IN_MEMORY"` // Redis options RedisAddress string `env:"REDIS_ADDRESS"` RedisUsername string `env:"REDIS_USERNAME"` RedisPassword string `env:"REDIS_PASSWORD"` }
Config represents configuration for a cacher.
type FetchFunc ¶
type FetchFunc func() (interface{}, error)
FetchFunc is a function used to Fetch in a cacher.
type InMemoryConfig ¶
type KeyFunc ¶
KeyFunc is a function that mutates the provided cache key before storing it in Redis. This can be used to hash or HMAC values to prevent their plaintext from appearing in Redis. A good example might be an API key lookup that you HMAC before passing along.
The KeyFunc can also be used to add a prefix or namespace to keys in multi-tenant systems.
func HashKeyFunc ¶
HashKeyFunc returns a KeyFunc that hashes or HMACs the provided key before passing it to the cacher for storage.
func MultiKeyFunc ¶
MultiKeyFunc returns a KeyFunc that calls the provided KeyFuncs in order, with the previous value passed to the next.
func PrefixKeyFunc ¶
PrefixKeyFunc returns a KeyFunc that prefixes the key with the given constant before passing it to the cacher for storage.