cache

package
v0.26.1 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultTTL = 30 * time.Minute

DefaultTTL specifies default TTL

View Source
var ErrNotFound = errors.New("not found")

ErrNotFound defines not found error

View Source
var KeepTTL = time.Duration(-1)

KeepTTL specifies to keep value

View Source
var NowFunc = time.Now

NowFunc allows to override default time

Functions

func GetOrSet added in v0.24.557

func GetOrSet(ctx context.Context, p Provider, key string, value any, getter func() (any, error)) error

GetOrSet gets value from cache, or sets it using getter

func IsNotFoundError

func IsNotFoundError(err error) bool

IsNotFoundError returns true, if error is NotFound

Types

type Config

type Config struct {
	// Provider specifies the cache provider: redis|memory
	Provider string       `json:"provider" yaml:"provider"`
	Redis    *RedisConfig `json:"redis" yaml:"redis"`
}

Config specifies configuration of the cache.

type Provider

type Provider interface {
	// Set data
	Set(ctx context.Context, key string, v any, ttl time.Duration) error
	// Get data
	Get(ctx context.Context, key string, v any) error
	// Delete data
	Delete(ctx context.Context, key string) error
	// CleanExpired data
	CleanExpired(ctx context.Context)
	// Close closes the client, releasing any open resources.
	// It is rare to Close a Client, as the Client is meant to be long-lived and shared between many goroutines.
	Close() error
	// Keys returns list of keys.
	// This method should be used mostly for testing, as in prod many keys maybe returned
	Keys(ctx context.Context, pattern string) ([]string, error)

	// IsLocal returns true, if cache is local
	IsLocal() bool

	// Publish publishes message to channel
	Publish(ctx context.Context, channel, message string) error
	// Subscribe subscribes to channel
	Subscribe(ctx context.Context, channel string) Subscription
}

Provider defines cache interface

func NewMemoryProvider

func NewMemoryProvider(prefix string) Provider

NewMemoryProvider returns memory cache

func NewProxyProvider

func NewProxyProvider(prefix string, prov Provider) Provider

NewProxyProvider returns proxy provider

func NewRedisProvider

func NewRedisProvider(cfg RedisConfig, prefix string) (Provider, error)

NewRedisProvider returns Redis cache

type RedisConfig

type RedisConfig struct {
	Server string        `json:"server,omitempty" yaml:"server,omitempty"`
	TTL    time.Duration `json:"ttl,omitempty" yaml:"ttl,omitempty"`
	// ClientTLS describes the TLS certs used to connect to the cluster
	ClientTLS *gserver.TLSInfo `json:"client_tls,omitempty" yaml:"client_tls,omitempty"`
	User      string           `json:"user,omitempty" yaml:"user,omitempty"`
	Password  string           `json:"password,omitempty" yaml:"password,omitempty"`
}

RedisConfig specifies configuration of the redis.

type Subscription

type Subscription interface {
	// Close the subscription
	Close() error
	// ReceiveMessage returns message,
	// or error if subscription is closed
	ReceiveMessage(ctx context.Context) (string, error)
}

Subscription defines subscription interface

Jump to

Keyboard shortcuts

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