store

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cacher

type Cacher[T any] interface {
	Get(ctx context.Context, key string) (value T, exists bool, err error)
	Set(ctx context.Context, key string, value T) error
}

Cacher is a generic interface for caching, allowing storage and retrieval of values with methods to handle cache entries.

func NewLRUCache

func NewLRUCache[T any](size int) Cacher[T]

NewLRUCache creates a new instance of a generic LRU cache with the specified size and returns it as a Cacher interface.

func NewLRUExpirableCache

func NewLRUExpirableCache[T any](size int, ttl time.Duration) Cacher[T]

NewLRUExpirableCache creates a new LRU cache with a specified size and time-to-live (TTL) for each entry.

func NewNatsCache

func NewNatsCache[T any](kv jetstream.KeyValue, prefix string) Cacher[T]

NewNatsCache creates a new instance of a NATS-based cache with the specified key-value store and key prefix.

func NewRedisCache

func NewRedisCache[T any](db *redis.Client, prefix string, ttl time.Duration) Cacher[T]

NewRedisCache creates a new Redis-based generic cache with a specified prefix and time-to-live duration.

func NewSingleCache

func NewSingleCache[T any](ttl time.Duration) Cacher[T]

NewSingleCache creates a single-entry cache with the specified TTL, returning a generic Cacher interface instance.

type RefreshFunc

type RefreshFunc[T any] func(ctx context.Context) (T, error)

RefreshFunc defines a function type for refreshing or computing a value in a cache, returning the value and an error.

type StaleValue

type StaleValue[T any] struct {
	Value     T
	CreatedAt time.Time
}

StaleValue represents a value associated with a timestamp indicating when it was created.

type StaleWhileRevalidateCache

type StaleWhileRevalidateCache[T any] interface {
	Cacher[StaleValue[T]]
	TryAcquireRefreshLock(ctx context.Context, key string, randValue string, ttl time.Duration) (bool, error)
	ReleaseRefreshLock(ctx context.Context, key string, randValue string) error
}

StaleWhileRevalidateCache is a generic interface for a cache implementing stale-while-revalidate pattern. The cache is capable of storing and retrieving stale values while allowing background refresh of data. It embeds Cacher for basic caching operations and provides methods for managing refresh locks.

func NewStaleWhileRevalidateExpiringLRUCache

func NewStaleWhileRevalidateExpiringLRUCache[T any](size int, ttl time.Duration) StaleWhileRevalidateCache[T]

NewStaleWhileRevalidateExpiringLRUCache creates a new LRU-based cache with support for stale-while-revalidate and expirable items. The cache allows a maximum of `size` items and applies a time-to-live duration defined by `ttl` for stored data.

func NewStaleWhileRevalidateLRUCache

func NewStaleWhileRevalidateLRUCache[T any](size int) StaleWhileRevalidateCache[T]

NewStaleWhileRevalidateLRUCache creates a new LRU-based StaleWhileRevalidateCache with a specified size.

func NewStaleWhileRevalidateNatsCache

func NewStaleWhileRevalidateNatsCache[T any](kv jetstream.KeyValue, prefix string) StaleWhileRevalidateCache[T]

NewStaleWhileRevalidateNatsCache creates a new StaleWhileRevalidateCache instance backed by NATS JetStream KeyValue store. T is the type of data to be cached. kv specifies the KeyValue store to use for storing cached values. prefix defines the key prefix to use within the KeyValue store.

func NewStaleWhileRevalidateRedisCache

func NewStaleWhileRevalidateRedisCache[T any](db *redis.Client, prefix string, ttl time.Duration) StaleWhileRevalidateCache[T]

NewStaleWhileRevalidateRedisCache creates a Redis-backed stale-while-revalidate cache with the specified prefix and TTL.

func NewStaleWhileRevalidateSingleCache

func NewStaleWhileRevalidateSingleCache[T any](ttl time.Duration) StaleWhileRevalidateCache[T]

NewStaleWhileRevalidateSingleCache creates a single-entry cache with a stale-while-revalidate pattern and a specified TTL.

Jump to

Keyboard shortcuts

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