cache

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2022 License: MIT Imports: 8 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

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

Functions

func ValidateConnectionString

func ValidateConnectionString(typ CacheType, connStr string) error

ValidateConnectionString validates connection string for specific cache type.

Types

type Cache

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

Cache represents a cache.

func New

func New(opts ...CacheOption) *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() error

Start cache.

type CacheInstance

type CacheInstance[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
}

CacheInstance represents a cache instance.

func Create

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

Create new cache instance with specified name and options.

func Get

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

Get returns pre-configured cache instance by name.

type CacheInstanceCloser

type CacheInstanceCloser interface {
	// Close cache instance.
	Close()
}

CacheInstanceCloser represents a cache instance close method.

type CacheInstancePinger

type CacheInstancePinger interface {
	Ping(ctx context.Context) error
}

CacheInstancePinger represents a cache instance ping method.

type CacheOption

type CacheOption interface {
	// contains filtered or unexported methods
}

CacheOption is an option for the cache instance.

type CacheType

type CacheType string

CacheType represents a cache type.

const (
	// MemoryCache store data in memory.
	MemoryCache CacheType = "memory"
	// RedisCache store data in Redis database.
	RedisCache CacheType = "redis"
)

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 ErrKeyNotFound

type ErrKeyNotFound struct {
	Key string
}

func (ErrKeyNotFound) Error

func (e ErrKeyNotFound) Error() string

type ItemOption

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

ItemOption is an option for the cached item.

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 TTL

type TTL[T any] time.Duration

TTL represents time to keep item in cache.

Jump to

Keyboard shortcuts

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