tieredcache

package module
v0.0.0-...-fc101b8 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2020 License: Apache-2.0 Imports: 13 Imported by: 0

README

Tiered Cache for Go

GoDoc Build Status codecov Go Report Card

tieredcache composes a local in-memory cache (BigCache) with Clustered Redis (redigo/redisc). Local cache is checked first. If a result is not found, the redis cluster is checked. Cache sets are performed on both local and remote cache.

API documentation and examples can be found in the GoDoc

License

Apache 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	GetBytes(ctx context.Context, key string) ([]byte, error)
	Get(ctx context.Context, key string, target interface{}) error
	SetBytes(ctx context.Context, key string, value []byte) error
	Set(ctx context.Context, key string, value interface{}) error
	Delete(ctx context.Context, key string) error
	Purge(ctx context.Context) error
}

Cache defines the interface for interacting with caching utilities. All derived caches must implement this interface

type CacheEncoder

type CacheEncoder interface {
	// value must be a pointer
	Encode(value interface{}) ([]byte, error)
	// target must be a pointer
	Decode(cachedValue []byte, target interface{}) error
}

CacheEncoder defines an interface for encoding and decoding values stored in cache

type CacheMetrics

type CacheMetrics interface {
	Hit()
	Miss()
	Set()
	SetCollision()
	DeleteHit()
	DeleteMiss()
	PurgeHit()
	PurgeMiss()
}

CacheMetrics defines an interface for recording cache metrics

type GobCacheEncoder

type GobCacheEncoder struct{}

GobCacheEncoder uses encoding/gob to encode values for caching

func (*GobCacheEncoder) Decode

func (gb *GobCacheEncoder) Decode(cachedValue []byte, target interface{}) error

Decode decodes the cached value using the gob encoding and sets the result in target. target must be a pointer

func (*GobCacheEncoder) Encode

func (gb *GobCacheEncoder) Encode(value interface{}) ([]byte, error)

Encode encodes the provided value using gob. value must be a pointer.

type LocalCache

type LocalCache struct {
	Cache          *bigcache.BigCache
	Encoder        CacheEncoder
	Metrics        CacheMetrics
	TracingEnabled bool
}

LocalCache defines a remote-caching approach in which keys are stored remotely in a separate process.

func (LocalCache) Delete

func (lc LocalCache) Delete(ctx context.Context, key string) error

Delete removes the value from local cache

func (LocalCache) Get

func (lc LocalCache) Get(ctx context.Context, key string, target interface{}) error

Get retrieves the value from cache, decodes it, and sets the result in target. target must be a pointer.

func (LocalCache) GetBytes

func (lc LocalCache) GetBytes(ctx context.Context, key string) ([]byte, error)

GetBytes gets the requested bytes from local cache

func (LocalCache) Purge

func (lc LocalCache) Purge(ctx context.Context) error

Purge wipes out all items in local cache

func (LocalCache) Set

func (lc LocalCache) Set(ctx context.Context, key string, value interface{}) error

Set encodes the provided value and sets it in the local cache

func (LocalCache) SetBytes

func (lc LocalCache) SetBytes(ctx context.Context, key string, value []byte) error

SetBytes sets the provided bytes in the local cache on the provided key

type LocalCacheConfig

type LocalCacheConfig struct {
	Eviction       time.Duration
	TTL            time.Duration
	Shards         uint // Must be power of 2
	TracingEnabled bool
}

LocalCacheConfig is the necessary configuration for instantiating a LocalCache struct

func (LocalCacheConfig) NewCache

func (lcc LocalCacheConfig) NewCache(
	encoder CacheEncoder,
	metrics CacheMetrics,
) (LocalCache, error)

NewCache constructs and returns a LocalCache given configuration

func (*LocalCacheConfig) RegisterFlags

func (lcc *LocalCacheConfig) RegisterFlags(flags *pflag.FlagSet)

RegisterFlags registers LocalCache pflags

type MockCache

type MockCache struct {
	Cache   map[string][]byte
	Encoder CacheEncoder
	Metrics CacheMetrics
}

MockCache mocks the Cache implementation for use in test caches

func NewMockCache

func NewMockCache(encoder CacheEncoder) *MockCache

NewMockCache constructs a new cache for testing

func (*MockCache) Delete

func (mc *MockCache) Delete(ctx context.Context, key string) error

Delete is a mock Delete implementation for cache

func (*MockCache) Get

func (mc *MockCache) Get(ctx context.Context, key string, target interface{}) error

Get is a mock GetBytes implementation for cache

func (*MockCache) GetBytes

func (mc *MockCache) GetBytes(ctx context.Context, key string) ([]byte, error)

GetBytes is a mock GetBytes implementation for cache

func (*MockCache) Purge

func (mc *MockCache) Purge(ctx context.Context) error

Purge is a mock Purge implementation for cache

func (*MockCache) Set

func (mc *MockCache) Set(ctx context.Context, key string, value interface{}) error

Set is a mock Set implementation for cache

func (*MockCache) SetBytes

func (mc *MockCache) SetBytes(ctx context.Context, key string, value []byte) error

SetBytes is a mock SetBytes implementation for cache

type MockCacheEncoder

type MockCacheEncoder struct{}

MockCacheEncoder is a fake encoder for use in tests

func (MockCacheEncoder) Decode

func (mce MockCacheEncoder) Decode(cachedValue []byte, target interface{}) error

Decode mock returns no error

func (MockCacheEncoder) Encode

func (mce MockCacheEncoder) Encode(value interface{}) ([]byte, error)

Encode mock simply returns the value it was given

type MockCacheMetrics

type MockCacheMetrics struct {
	mock.Mock
}

MockCacheMetrics provides a mock cache metrics implementation

func (*MockCacheMetrics) DeleteHit

func (mcc *MockCacheMetrics) DeleteHit()

DeleteHit is a mock metrics DeleteHit implementation

func (*MockCacheMetrics) DeleteMiss

func (mcc *MockCacheMetrics) DeleteMiss()

DeleteMiss is a mock metrics DeleteMiss implementation

func (*MockCacheMetrics) Hit

func (mcc *MockCacheMetrics) Hit()

Hit is a mock metrics Hit implementation

func (*MockCacheMetrics) Miss

func (mcc *MockCacheMetrics) Miss()

Miss is a mock metrics Miss implementation

func (*MockCacheMetrics) PurgeHit

func (mcc *MockCacheMetrics) PurgeHit()

PurgeHit is a mock metrics PurgeHit implementation

func (*MockCacheMetrics) PurgeMiss

func (mcc *MockCacheMetrics) PurgeMiss()

PurgeMiss is a mock metrics PurgeMiss implementation

func (*MockCacheMetrics) Set

func (mcc *MockCacheMetrics) Set()

Set is a mock metrics Set implementation

func (*MockCacheMetrics) SetCollision

func (mcc *MockCacheMetrics) SetCollision()

SetCollision is a mock metrics SetCollision implementation

type MockTieredCacheCreator

type MockTieredCacheCreator struct {
	mock.Mock
}

MockTieredCacheCreator provides a mock tiered cache config implementation

func (*MockTieredCacheCreator) NewCache

func (m *MockTieredCacheCreator) NewCache(
	encoder CacheEncoder,
	metrics CacheMetrics,
	localMetrics CacheMetrics,
	remoteMetrics CacheMetrics,
) (Cache, error)

NewCache returns a mocked tiered cache

type MockedCacheEncoder

type MockedCacheEncoder struct {
	mock.Mock
}

MockCacheEncoder mocks the cache encoder for use in tests

func (*MockedCacheEncoder) Decode

func (mce *MockedCacheEncoder) Decode(cachedValue []byte, target interface{}) error

Decode mocks the cache decode implementation

func (*MockedCacheEncoder) Encode

func (mce *MockedCacheEncoder) Encode(value interface{}) ([]byte, error)

Encode mocks the cache encode implementation

type PrometheusCacheMetrics

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

PrometheusCacheMetrics surfaces cache metrics for usage with Prometheus

func NewPrometheusCacheMetrics

func NewPrometheusCacheMetrics(client, cacheName string) *PrometheusCacheMetrics

NewPrometheusCacheMetrics creates and returns a Prometheus cache metrics recorder

func (*PrometheusCacheMetrics) DeleteHit

func (pcm *PrometheusCacheMetrics) DeleteHit()

DeleteHit defines a deletion hit from cache

func (*PrometheusCacheMetrics) DeleteMiss

func (pcm *PrometheusCacheMetrics) DeleteMiss()

DeleteMiss defines a deletion miss from cache

func (*PrometheusCacheMetrics) Hit

func (pcm *PrometheusCacheMetrics) Hit()

Hit defines a cache hit

func (*PrometheusCacheMetrics) Miss

func (pcm *PrometheusCacheMetrics) Miss()

Miss defines a cache miss

func (*PrometheusCacheMetrics) PurgeHit

func (pcm *PrometheusCacheMetrics) PurgeHit()

PurgeHit defines a purge hit of cache

func (*PrometheusCacheMetrics) PurgeMiss

func (pcm *PrometheusCacheMetrics) PurgeMiss()

PurgeMiss defines a purge miss of cache

func (*PrometheusCacheMetrics) Set

func (pcm *PrometheusCacheMetrics) Set()

Set defines a cache set

func (*PrometheusCacheMetrics) SetCollision

func (pcm *PrometheusCacheMetrics) SetCollision()

SetCollision defines a cache set collision

type RemoteCache

type RemoteCache struct {
	Encoder        CacheEncoder
	Metrics        CacheMetrics
	TracingEnabled bool
	// contains filtered or unexported fields
}

RemoteCache defines a remote-caching approach in which keys are stored remotely in a separate process.

func (RemoteCache) Close

func (rc RemoteCache) Close()

Close cleans up cache and removes any open connections

func (RemoteCache) Delete

func (rc RemoteCache) Delete(ctx context.Context, key string) error

Delete removes the value from remote cache. Because Redis doesnt support Fuzzy matches for delete, this function first gets all matching keys, and then proceeds to pipeline deletion of those keys

func (RemoteCache) Get

func (rc RemoteCache) Get(ctx context.Context, key string, target interface{}) error

Get retrieves the value from cache, decodes it, and sets the result in target. target must be a pointer.

func (RemoteCache) GetBytes

func (rc RemoteCache) GetBytes(ctx context.Context, key string) ([]byte, error)

GetBytes gets the requested bytes from remote cache

func (RemoteCache) Purge

func (rc RemoteCache) Purge(ctx context.Context) error

Purge wipes out all items under control of this cache in Redis

func (RemoteCache) Set

func (rc RemoteCache) Set(ctx context.Context, key string, value interface{}) error

Set encodes the provided value and sets it in the remote cache

func (RemoteCache) SetBytes

func (rc RemoteCache) SetBytes(ctx context.Context, key string, value []byte) error

SetBytes sets the provided bytes in the remote cache on the provided key

type RemoteCacheConfig

type RemoteCacheConfig struct {
	URLs           []string
	AuthToken      string
	Timeout        time.Duration
	TracingEnabled bool
}

RemoteCacheConfig is the necessary configuration for instantiating a RemoteCache struct

func (RemoteCacheConfig) NewCache

func (rcc RemoteCacheConfig) NewCache(
	encoder CacheEncoder,
	metrics CacheMetrics,
) (RemoteCache, error)

NewCache constructs and returns a RemoteCache given configuration

func (*RemoteCacheConfig) RegisterFlags

func (rcc *RemoteCacheConfig) RegisterFlags(flags *pflag.FlagSet)

RegisterFlags registers RemoteCache pflags

type TieredCache

type TieredCache struct {
	Remote         Cache
	Local          Cache
	Metrics        CacheMetrics
	TracingEnabled bool
}

TieredCache defines a combined local and remote-caching approach in which keys are stored remotely in a separate process as well as cached locally. Local cache is preferred.

func (TieredCache) Close

func (tc TieredCache) Close()

Close cleans up cache and removes any open connections

func (TieredCache) Delete

func (tc TieredCache) Delete(ctx context.Context, key string) error

Delete removes the value from local cache and remote cache

func (TieredCache) Get

func (tc TieredCache) Get(ctx context.Context, key string, target interface{}) error

Get retrieves the value from the tiered cache, cache, decodes it, and sets the result in target. Local cache first, then remote. target must be a pointer.

func (TieredCache) GetBytes

func (tc TieredCache) GetBytes(ctx context.Context, key string) ([]byte, error)

GetBytes gets the requested bytes from from tiered cache. Local first, then remote.

func (TieredCache) Purge

func (tc TieredCache) Purge(ctx context.Context) error

Purge wipes out all items locally, and all items under control of this cache in Redis

func (TieredCache) Set

func (tc TieredCache) Set(ctx context.Context, key string, value interface{}) error

Set encodes the provided value and sets it in the local and remote cache

func (TieredCache) SetBytes

func (tc TieredCache) SetBytes(ctx context.Context, key string, value []byte) error

SetBytes sets the provided bytes in the local and remote caches on the provided key

type TieredCacheConfig

type TieredCacheConfig struct {
	RemoteConfig   RemoteCacheConfig
	LocalConfig    LocalCacheConfig
	Encoder        CacheEncoder
	TracingEnabled bool
}

TieredCacheConfig is the necessary configuration for instantiating a TieredCache struct

func (TieredCacheConfig) NewCache

func (tcc TieredCacheConfig) NewCache(
	encoder CacheEncoder,
	metrics CacheMetrics,
	localMetrics CacheMetrics,
	remoteMetrics CacheMetrics,
) (Cache, error)

NewCache constructs and returns a TieredCache given configuration

func (*TieredCacheConfig) RegisterFlags

func (tcc *TieredCacheConfig) RegisterFlags(flags *pflag.FlagSet)

RegisterFlags registers TieredCache pflags

type TieredCacheCreator

type TieredCacheCreator interface {
	NewCache(
		encoder CacheEncoder,
		metrics CacheMetrics,
		localMetrics CacheMetrics,
		remoteMetrics CacheMetrics,
	) (Cache, error)
}

TieredCacheCreator defines an interface to create and return a Tiered Cache

Jump to

Keyboard shortcuts

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