v0.17.1 Latest Latest

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

Go to latest
Published: Jun 19, 2024 License: MIT Imports: 12 Imported by: 3




View Source
const (
	InstrumentationStart  = "cache-start"
	InstrumentationClose  = "cache-close"
	InstrumentationPing   = "cache-ping"
	InstrumentationGet    = "cache-get"
	InstrumentationLoader = "cache-loader"
	InstrumentationSet    = "cache-set"
	InstrumentationDelete = "cache-delete"


View Source
var ErrCacheClosed = errors.New("cache closed")


func InstrDelete added in v0.15.0

func InstrDelete(op string, args ...any) (string, bool)

InstrDelete returns cache key if the operation is cache delete event.

func InstrGet added in v0.15.0

func InstrGet(op string, args ...any) (string, bool)

InstrGet returns cache key if the operation is cache get event.

func InstrLoader added in v0.15.0

func InstrLoader(op string, args ...any) (string, bool)

InstrLoader returns cache key if the operation is cache loader event.

func InstrSet added in v0.15.0

func InstrSet(op string, args ...any) (string, bool)

InstrSet returns cache key if the operation is cache set event.

func ParseRedisClusterURL added in v0.11.0

func ParseRedisClusterURL(v string) (*redis.ClusterOptions, error)

func ParseRedisURL added in v0.11.0

func ParseRedisURL(v string) (*redis.Options, error)

func ValidateConnectionString

func ValidateConnectionString(typ Type, connStr string) error

ValidateConnectionString validates connection string for specific cache type.


type Cache

type Cache struct {
	// contains filtered or unexported fields

Cache represents a cache.

func New

func New(opts ...Option) *Cache

New creates a new cache with specified type.

func (*Cache) Close

func (c *Cache) Close()

Close cache and all its instances.

func (*Cache) Ping

func (c *Cache) Ping(ctx context.Context) error

Ping cache and all its instances.

func (*Cache) Start

func (c *Cache) Start(ctx context.Context) error

Start cache.

type ConnectionPassword

type ConnectionPassword string

ConnectionString is a connection password for the cache instance.

type ConnectionString

type ConnectionString string

ConnectionString is a connection string for the cache instance.

type DefaultTTL

type DefaultTTL time.Duration

DefaultTTL is an default TTL for items in cache instance.

type Instance added in v0.15.0

type Instance[T any] interface {
	// Get value from cache. If value is not found, it will return default value.
	Get(ctx context.Context, key string, opts ...ItemOption[T]) (T, error)
	// Pop returns value from tha cache and deletes it. If value is not found, it will return ErrKeyNotFound error.
	Pop(ctx context.Context, key string) (T, error)
	// Set value in cache.
	Set(ctx context.Context, key string, value T, opts ...ItemOption[T]) error
	// Delete value from cache.
	Delete(ctx context.Context, key string) error

Instance of a cache.

func Create

func Create[T any](cache *Cache, name string, opts ...Option) (Instance[T], error)

Create new cache instance with specified name and options.

func Get

func Get[T any](cache *Cache, name string) (Instance[T], error)

Get returns pre-configured cache instance by name.

type InstanceCloser added in v0.15.0

type InstanceCloser interface {
	// Close cache instance.

InstanceCloser represents a cache instance close method.

type InstancePinger added in v0.15.0

type InstancePinger interface {
	Ping(ctx context.Context) error

InstancePinger represents a cache instance ping method.

type Instrumenter added in v0.7.0

type Instrumenter instrumenter.Instrumenter

Instrumenter is a function that instruments cache operations.

type ItemOption

type ItemOption[T any] interface {
	// contains filtered or unexported methods

ItemOption is an option for the cached item.

type KeyNotFoundError added in v0.15.0

type KeyNotFoundError struct {
	Key string

func (KeyNotFoundError) Error added in v0.15.0

func (e KeyNotFoundError) Error() string

type KeyPrefix

type KeyPrefix string

KeyPrefix is a prefix for the cache keys.

type Loader

type Loader func(ctx context.Context, key string) (any, error)

Loader is a function that loads data when cache key is missing.

WARNING: it's not guaranteed that the function will be called only once.

type Option added in v0.15.0

type Option interface {
	// contains filtered or unexported methods

Option for the cache instance.

type TTL

type TTL[T any] time.Duration

TTL represents time to keep item in cache.

type Type added in v0.15.0

type Type string

Type of a cache.

const (
	// MemoryCache store data in memory.
	MemoryCache Type = "memory"
	// RedisCache store data in Redis database.
	RedisCache Type = "redis"
	// RedisClusterCache store data in Redis database cluster.
	RedisClusterCache Type = "redis-cluster"

Jump to

Keyboard shortcuts

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