cache

package module
v0.0.0-...-68bbfba Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2019 License: ISC Imports: 12 Imported by: 1

README

CacheInterface

Build Status

An experimental interface to use different caches interchangeably via URIs. The goal for this library is to simplify using a caching layer for use with different services, enabling easy swapping of backends without the hassle of configuration.

To achieve this, we use unique "uris" for each driver, giving us a single string to configure the settings, much like database servers and other software.

redis://server:port?db=0&password=test
memcache://server1:11211,server2:11211
memory://
lru://?size=128

Cache Types

LRU

Implemented using github.com/hashicorp/golang-lru, this driver provides an in-memory lru cache implementation. Items in this cache will be evicted on an as-needed basis, with the most frequently used items staying in memory longer.

Memory

Implemented using github.com/patrickmn/go-cache, this driver provides an in-memory object cache. This driver specifically supports non-serialized storing of objects when using Get(key, dstPointer)

Memcache

Implemented using github.com/bradfitz/gomemcache, this driver provides a Memcache connector for the Memcached protocol and compatible servers.

Redis

Implemented using github.com/hoisie/redis, this driver provides a Redis connector for the ever popular Redis key-value store.

Serialization

When the driver requires it, values are almost always serialized into []byte. The library can automatically handle this as long as the structures are supported by msgpack. Future plans may include other serialization methods or customized serialization.

The exception to the above is for raw []byte values and strings (stored as a []byte), as they can be easily converted.

API

The CacheInterface interface has all methods. All drivers will implement this on a best-effort basis.

type CacheInterface interface {
	Has(key string) bool
	Get(key string, dst ...interface{}) ([]byte, error)
	Set(key string, val interface{}, ttl time.Duration) (err error)
	Del(key string) error
}

In addition to this, cache drivers can be used without the cacheinterface.New method by using the New<Driver>Cache method and providing the necessary parameters.

Code & Testing

As many test methods as we can add have been implemented, with the exception being those the CI service (Drone) cannot support (Memcache, Redis) due to limitations using extra services.

All code should have tests to verify there isn't anything broken with a new commit.

Documentation

Index

Constants

View Source
const (
	Memcache = "memcache"
	Redis    = "redis"
	Memory   = "memory"
	Lru      = "lru"
)

Variables

View Source
var (
	ErrInvalidDriver = errors.New("invalid driver")
)
View Source
var (
	ErrMemoryCacheNotExists = errors.New("item does not exist")
)

Functions

This section is empty.

Types

type CacheInterface

type CacheInterface interface {
	Has(key string) bool
	Get(key string, dst ...interface{}) ([]byte, error)
	Set(key string, val interface{}, ttl time.Duration) (err error)
	Del(key string) error
}

func New

func New(uri string) (CacheInterface, error)

func NewLruCache

func NewLruCache(size int) (CacheInterface, error)

func NewMemcacheCache

func NewMemcacheCache(s MemcacheSettings) (CacheInterface, error)

func NewMemoryCache

func NewMemoryCache(cleanupTime time.Duration) (CacheInterface, error)

func NewRedisCache

func NewRedisCache(c RedisSettings) (CacheInterface, error)

type LruCache

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

func (*LruCache) Del

func (mc *LruCache) Del(key string) error

func (*LruCache) Get

func (mc *LruCache) Get(key string, dst ...interface{}) ([]byte, error)

func (*LruCache) Has

func (mc *LruCache) Has(key string) bool

func (*LruCache) Set

func (mc *LruCache) Set(key string, val interface{}, ttl time.Duration) error

type MemcacheCache

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

func (*MemcacheCache) Del

func (mc *MemcacheCache) Del(key string) error

func (*MemcacheCache) Get

func (mc *MemcacheCache) Get(key string, dst ...interface{}) ([]byte, error)

func (*MemcacheCache) Has

func (mc *MemcacheCache) Has(key string) bool

func (*MemcacheCache) Set

func (mc *MemcacheCache) Set(key string, val interface{}, ttl time.Duration) error

type MemcacheSettings

type MemcacheSettings struct {
	Servers []string
}

type MemoryCache

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

func (*MemoryCache) Del

func (mc *MemoryCache) Del(key string) error

func (*MemoryCache) Get

func (mc *MemoryCache) Get(key string, dst ...interface{}) ([]byte, error)

func (*MemoryCache) Has

func (mc *MemoryCache) Has(key string) bool

func (*MemoryCache) Set

func (mc *MemoryCache) Set(key string, val interface{}, ttl time.Duration) error

type RedisCache

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

func (*RedisCache) Del

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

func (*RedisCache) Get

func (rc *RedisCache) Get(key string, dst ...interface{}) ([]byte, error)

func (*RedisCache) Has

func (rc *RedisCache) Has(key string) bool

func (*RedisCache) Set

func (rc *RedisCache) Set(key string, val interface{}, ttl time.Duration) error

type RedisSettings

type RedisSettings struct {
	Address  string
	DB       int
	Password string
}

Jump to

Keyboard shortcuts

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