cachier

package module
v0.0.0-...-6dae6c1 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2019 License: MIT Imports: 6 Imported by: 0

README

Build Status GoDoc

Cachier

Cachier is a Go library that provides an interface for dealing with cache. There is a CacheEngine interface which requires you to implement common cache methods (like Get, Set, Delete, etc). When implemented, you wrap this CacheEngine into the Cache struct. This struct has some methods implemented like GetOrCompute method (shortcut for fetching a hit or computing/writing a miss).

There are also three implementations included:

  • LRUCache: a wrapper of hashicorp/golang-lru which fulfills the CacheEngine interface

  • RedisCache: CacheEngine based on redis

  • CacheWithSubcache: Implementation of combination of primary cache with fast L1 subcache. E.g. primary Redis cache and fast (and small) LRU subcache. But any other implementations of CacheEngine can be used.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound = errors.New("Key not found")
)

Errors

Functions

This section is empty.

Types

type Cache

type Cache struct {
	CacheEngine
	// contains filtered or unexported fields
}

Cache is an implementation of a cache (key-value store). It needs to be provided with cache engine.

func MakeCache

func MakeCache(engine CacheEngine) *Cache

MakeCache creates cache with provided engine

func (*Cache) DeleteWithPrefix

func (c *Cache) DeleteWithPrefix(prefix string) error

DeleteWithPrefix removes all keys that start with given prefix

func (*Cache) GetOrCompute

func (c *Cache) GetOrCompute(key string, evaluator func() (interface{}, error)) (interface{}, error)

GetOrCompute tries to get value from cache. If not found, it computes the value using provided evaluator function and stores it into cache.

type CacheEngine

type CacheEngine interface {
	Get(key string) (interface{}, error)
	Peek(key string) (interface{}, error)
	Set(key string, value interface{}) error
	Delete(key string) error
	Keys() ([]string, error)
	Purge() error
}

CacheEngine is an interface for cache engine (e.g. in-memory cache or Redis Cache)

type CacheWithSubcache

type CacheWithSubcache struct {
	Cache    *Cache
	Subcache *Cache
}

CacheWithSubcache is a Cache with L1 subcache.

func (*CacheWithSubcache) Delete

func (cs *CacheWithSubcache) Delete(key string) error

Delete removes a key from cache

func (*CacheWithSubcache) Get

func (cs *CacheWithSubcache) Get(key string) (interface{}, error)

Get gets a cached value by key

func (*CacheWithSubcache) Keys

func (cs *CacheWithSubcache) Keys() ([]string, error)

Keys returns a slice of all keys in the cache

func (*CacheWithSubcache) Peek

func (cs *CacheWithSubcache) Peek(key string) (interface{}, error)

Peek gets a cached key value without side-effects (i.e. without adding to L1 cache)

func (*CacheWithSubcache) Purge

func (cs *CacheWithSubcache) Purge() error

Purge removes all the records from the cache

func (*CacheWithSubcache) Set

func (cs *CacheWithSubcache) Set(key string, value interface{}) error

Set stores a key-value pair into cache

type DummyLogger

type DummyLogger struct{}

DummyLogger is implementation of Logger that does not log anything

func (DummyLogger) Error

func (d DummyLogger) Error(...interface{})

Error does nothing

func (DummyLogger) Print

func (d DummyLogger) Print(...interface{})

Print does nothing

func (DummyLogger) Warn

func (d DummyLogger) Warn(...interface{})

Warn does nothing

type LRUCache

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

LRUCache is a wrapper of hashicorp's golang-lru cache which implements cachier.Cache interface

func NewLRUCache

func NewLRUCache(size int) (*LRUCache, error)

NewLRUCache is a constructor that creates LRU cache of given size

func (*LRUCache) Delete

func (lc *LRUCache) Delete(key string) error

Delete removes a key from cache

func (*LRUCache) Get

func (lc *LRUCache) Get(key string) (interface{}, error)

Get gets a value by given key

func (*LRUCache) Keys

func (lc *LRUCache) Keys() ([]string, error)

Keys returns all the keys in cache

func (*LRUCache) Peek

func (lc *LRUCache) Peek(key string) (interface{}, error)

Peek gets a value by given key and does not change it's "lruness"

func (*LRUCache) Purge

func (lc *LRUCache) Purge() error

Purge removes all records from the cache

func (*LRUCache) Set

func (lc *LRUCache) Set(key string, value interface{}) error

Set stores given key-value pair into cache

type Logger

type Logger interface {
	Error(...interface{})
	Warn(...interface{})
	Print(...interface{})
}

Logger is interface for logging

type RedisCache

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

RedisCache implements cachier.CacheTTL interface using redis storage

func NewRedisCache

func NewRedisCache(
	redisClient *redis.Client,
	keyPrefix string,
	marshal func(value interface{}) ([]byte, error),
	unmarshal func(b []byte, value *interface{}) error,
	ttl time.Duration,
) *RedisCache

NewRedisCache is a constructor that creates a RedisCache

func NewRedisCacheWithLogger

func NewRedisCacheWithLogger(
	redisClient *redis.Client,
	keyPrefix string,
	marshal func(value interface{}) ([]byte, error),
	unmarshal func(b []byte, value *interface{}) error,
	ttl time.Duration,
	logger Logger,
) *RedisCache

NewRedisCacheWithLogger is a constructor that creates a RedisCache

func (*RedisCache) Delete

func (rc *RedisCache) Delete(key string) error

Delete removes a key from cache

func (*RedisCache) Get

func (rc *RedisCache) Get(key string) (interface{}, error)

Get gets a cached value by key

func (*RedisCache) Keys

func (rc *RedisCache) Keys() ([]string, error)

Keys returns all the keys in the cache

func (*RedisCache) Peek

func (rc *RedisCache) Peek(key string) (interface{}, error)

Peek gets a cached value by key without any sideeffects (identical as Get in this implementation)

func (*RedisCache) Purge

func (rc *RedisCache) Purge() error

Purge removes all the records from the cache

func (*RedisCache) Set

func (rc *RedisCache) Set(key string, value interface{}) error

Set stores a key-value pair into cache

Jump to

Keyboard shortcuts

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