cache

package
v0.0.0-...-0b0e567 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultCache is the default cache.
	DefaultCache Cache = NewCache()
	// DefaultExpiration is the default duration for items stored in
	// the cache to expire.
	DefaultExpiration time.Duration = 0

	// ErrItemExpired is returned in Cache.Get when the item found in the cache
	// has expired.
	ErrItemExpired error = errors.New("item has expired")
	// ErrKeyNotFound is returned in Cache.Get and Cache.Delete when the
	// provided key could not be found in cache.
	ErrKeyNotFound error = errors.New("key not found in cache")
)

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// String returns the name of the implementation.
	String() string

	// Get gets a cached value by key.
	Get(ctx context.Context, key string) (any, time.Time, error)
	// Put stores a key-value pair into cache.
	Put(ctx context.Context, key string, val any, d time.Duration) error
	// Delete removes a key from cache.
	Delete(ctx context.Context, key string) error

	// MultiGet gets multiple cached values by key.
	MultiGet(ctx context.Context, keys []string) (map[string]any, error)
	// MultiPut stores multiple key-value pairs into cache.
	MultiPut(ctx context.Context, items map[string]Item, d time.Duration) error
	// MultiDelete removes multiple keys from cache.
	MultiDelete(ctx context.Context, keys []string) error

	// Incr increments a key by delta.
	Incr(ctx context.Context, key string, delta int64) (int64, error)
	// Decr decrements a key by delta.
	Decr(ctx context.Context, key string, delta int64) (int64, error)
	// CompareAndSwap compares the old value with the value stored in the cache.
	// If they match, the new value is stored in the cache and true is returned.
	// If they don't match, the old value is returned and false is returned.
	CompareAndSwap(ctx context.Context, key string, old, new any) (bool, error)

	// Lock attempts to acquire a lock with the provided key and ttl.
	Lock(ctx context.Context, key string, ttl time.Duration) (bool, error)
	// Unlock releases the lock with the provided key.
	Unlock(ctx context.Context, key string) (bool, error)
	// Renew attempts to renew the lock with the provided key and ttl.
	Renew(ctx context.Context, key string, ttl time.Duration) (bool, error)
}

Cache is the interface that wraps the cache.

func NewCache

func NewCache(opts ...Option) Cache

NewCache returns a new cache.

type Item

type Item struct {
	Value      any
	Expiration int64
}

Item represents an item stored in the cache.

func (*Item) Expired

func (i *Item) Expired() bool

Expired returns true if the item has expired.

type Option

type Option func(o *Options)

Option manipulates the Options passed.

func Expiration

func Expiration(d time.Duration) Option

Expiration sets the duration for items stored in the cache to expire.

func Items

func Items(i map[string]Item) Option

Items initializes the cache with preconfigured items.

func WithAddress

func WithAddress(addr string) Option

WithAddress sets the cache service address or connection information.

func WithContext

func WithContext(c context.Context) Option

WithContext sets the cache context, for any extra configuration.

func WithLogger

func WithLogger(l logger.Logger) Option

WithLogger sets underline logger.

type Options

type Options struct {
	// Context should contain all implementation specific options, using context.WithValue.
	Context context.Context
	// Logger is the be used logger
	Logger logger.Logger
	Items  map[string]Item
	// Address represents the address or other connection information of the cache service.
	Address    string
	Expiration time.Duration
}

Options represents the options for the cache.

func NewOptions

func NewOptions(opts ...Option) Options

NewOptions returns a new options struct.

Source Files

  • cache.go
  • memory.go
  • options.go

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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