cache

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HashAny

func HashAny(obj any) string

Redis keys must be strings so always use the hash

Types

type Cache

type Cache[K comparable, V any] interface {
	Get(key K) (v V, wasFound bool, err error)
	Delete(key K) error
	Set(key K, value V) error
	Purge() error
	// Get the value from cache for key K, or setItsValue and return it via func orSet
	GetOrSet(key K, orSet func() (V, error)) (val V, wasFoundInCache bool, err error)
}

func NewCache

func NewCache[K comparable, V any](backend CacheBackendAdapter[K, V], opts ...CacheOption[K, V]) Cache[K, V]

type CacheBackendAdapter

type CacheBackendAdapter[K comparable, V any] interface {
	Get(key K) (value V, wasFound bool, err error)
	Set(key K, value V) error
	Delete(key K) error
	Purge() error // Purges the cache
	// Get the value from cache for key K, or setItsValue and return it via func orSet
	GetOrSet(key K, orSet func() (V, error)) (val V, wasFoundInCache bool, err error)
}

CacheBackendAdapter manges reading/writing/deleting to and from a cache backend. E.G. Memory backend, Redis Backend, etc.

func NewInMemoryCache

func NewInMemoryCache[K comparable, V any](ttl time.Duration, opts ...CacheBackendOption[K, V]) CacheBackendAdapter[K, V]

NewInMemoryCache provisions a new InMemoryCache

func NewRedisCache

func NewRedisCache[K comparable, V any](ctx context.Context, client Redis, ttl time.Duration, opts ...CacheBackendOption[K, V]) CacheBackendAdapter[K, V]

NewRedisCache provisions a new RedisCache

type CacheBackendOption

type CacheBackendOption[K comparable, V any] func(cfg cacheBackendCfg[K, V]) cacheBackendCfg[K, V]

func IgnoreCacheSetErrors

func IgnoreCacheSetErrors[K comparable, V any]() CacheBackendOption[K, V]

IgnoreCacheSetErrors will ignore any errors that occur when setting a value in the cache and will not return an error to the client

func WithCapacity

func WithCapacity[K comparable, V any](maxObjects uint64) CacheBackendOption[K, V]

WithCapacity will ensure the cache cannot exceed the # of objects as defined here.

func WithFailThroughCache

func WithFailThroughCache[K comparable, V any](failThrough CacheBackendAdapter[K, V]) CacheBackendOption[K, V]

WithFailThroughCache will set a fail-through cache. If the primary cache does not have the value, it will attempt to get it from the fail-through cache, and update the primary cache if the value is found.

type CacheObserver

type CacheObserver[K comparable] interface {
	Hit(k K)
	Miss(k K)
	Get(k K)
	Set(k K)
	Delete(k K)
	Purge()
}

CacheObserver may be provided to track changes within the cache. These may be be used to push metrics, initiate cache purges, invalidations, etc.

type CacheOption

type CacheOption[K comparable, V any] func(cfg cacheCfg[K, V]) cacheCfg[K, V]

func WithObserver

func WithObserver[K comparable, V any](observer CacheObserver[K]) CacheOption[K, V]

type CacheSetError

type CacheSetError struct {
	Message string
}

func (CacheSetError) Error

func (cse CacheSetError) Error() string

type EncryptedCache

type EncryptedCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func NewEncryptedCache

func NewEncryptedCache[K comparable, V any](backend CacheBackendAdapter[K, []byte], salt []byte, iterations int, opts ...CacheBackendOption[K, V]) (*EncryptedCache[K, V], error)

NewEncryptedCache provides a new EncryptedCache with default configurations that provides a balance of performance and security. Selecting a # of iterations that is difficult to guess and high enough to make encrypt / decrypt durations sufficiently long to deter brute force attack is recommended. Minimum 2048 iterations is recommended

func NewEncryptedCacheWithIterations

func NewEncryptedCacheWithIterations[K comparable, V any](backend CacheBackendAdapter[K, []byte], salt []byte, iterations int, opts ...CacheBackendOption[K, V]) (*EncryptedCache[K, V], error)

NewEncryptedCacheWithIterations allows the user to specify a number of iterations which influences work factor for the cache.

func NewEncryptedCacheWithPasswordNonce

func NewEncryptedCacheWithPasswordNonce[K comparable, V any](backend CacheBackendAdapter[K, []byte], password, nonce, salt []byte, iterations int, opts ...CacheBackendOption[K, V]) (*EncryptedCache[K, V], error)

func (*EncryptedCache[K, V]) Delete

func (c *EncryptedCache[K, V]) Delete(key K) error

func (*EncryptedCache[K, V]) Get

func (c *EncryptedCache[K, V]) Get(key K) (v V, wasFound bool, err error)

func (*EncryptedCache[K, V]) GetOrSet

func (c *EncryptedCache[K, V]) GetOrSet(key K, orSet func() (V, error)) (val V, wasFoundInCache bool, err error)

func (*EncryptedCache[K, V]) Purge

func (c *EncryptedCache[K, V]) Purge() error

func (*EncryptedCache[K, V]) Set

func (c *EncryptedCache[K, V]) Set(key K, value V) error

type InMemoryCache

type InMemoryCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

InMemoryCache adapts ttlCache to integrate it with CacheBackendAdapter

func (*InMemoryCache[K, V]) Delete

func (c *InMemoryCache[K, V]) Delete(key K) error

func (*InMemoryCache[K, V]) Get

func (c *InMemoryCache[K, V]) Get(key K) (V, bool, error)

func (*InMemoryCache[K, V]) GetOrSet

func (c *InMemoryCache[K, V]) GetOrSet(key K, orSet func() (V, error)) (val V, wasFoundInCache bool, err error)

func (*InMemoryCache[K, V]) Purge

func (c *InMemoryCache[K, V]) Purge() error

func (*InMemoryCache[K, V]) Set

func (c *InMemoryCache[K, V]) Set(key K, val V) error

type Item

type Item[K comparable, V any] interface {
	Value() V
	IsExpired() bool
}

Item represents a single item in the cache, and if it is expired

type Redis

type Redis interface {
	Get(ctx context.Context, key string) *redis.StringCmd
	Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.StatusCmd
	Del(ctx context.Context, keys ...string) *redis.IntCmd
	FlushDB(ctx context.Context) *redis.StatusCmd
}

type RedisCache

type RedisCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Todo: Implement WithFailThroughCache && Capacity options. THESE ARE NOT YET IMPLEMENTED

func (*RedisCache[K, V]) Delete

func (c *RedisCache[K, V]) Delete(key K) error

func (*RedisCache[K, V]) Get

func (c *RedisCache[K, V]) Get(key K) (V, bool, error)

func (*RedisCache[K, V]) GetOrSet

func (c *RedisCache[K, V]) GetOrSet(key K, orSet func() (V, error)) (val V, wasFoundInCache bool, err error)

GetOrSet gets the value from cache for key K, or sets it value and return it via func orSet.

func (*RedisCache[K, V]) Purge

func (c *RedisCache[K, V]) Purge() error

func (*RedisCache[K, V]) Set

func (c *RedisCache[K, V]) Set(key K, val V) error

Jump to

Keyboard shortcuts

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