Documentation
¶
Index ¶
- type Cacher
- func NewLRUCache[T any](size int) Cacher[T]
- func NewLRUExpirableCache[T any](size int, ttl time.Duration) Cacher[T]
- func NewNatsCache[T any](kv jetstream.KeyValue, prefix string) Cacher[T]
- func NewRedisCache[T any](db *redis.Client, prefix string, ttl time.Duration) Cacher[T]
- func NewSingleCache[T any](ttl time.Duration) Cacher[T]
- type RefreshFunc
- type StaleValue
- type StaleWhileRevalidateCache
- func NewStaleWhileRevalidateExpiringLRUCache[T any](size int, ttl time.Duration) StaleWhileRevalidateCache[T]
- func NewStaleWhileRevalidateLRUCache[T any](size int) StaleWhileRevalidateCache[T]
- func NewStaleWhileRevalidateNatsCache[T any](kv jetstream.KeyValue, prefix string) StaleWhileRevalidateCache[T]
- func NewStaleWhileRevalidateRedisCache[T any](db *redis.Client, prefix string, ttl time.Duration) StaleWhileRevalidateCache[T]
- func NewStaleWhileRevalidateSingleCache[T any](ttl time.Duration) StaleWhileRevalidateCache[T]
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 ¶
NewLRUCache creates a new instance of a generic LRU cache with the specified size and returns it as a Cacher interface.
func NewLRUExpirableCache ¶
NewLRUExpirableCache creates a new LRU cache with a specified size and time-to-live (TTL) for each entry.
func NewNatsCache ¶
NewNatsCache creates a new instance of a NATS-based cache with the specified key-value store and key prefix.
func NewRedisCache ¶
NewRedisCache creates a new Redis-based generic cache with a specified prefix and time-to-live duration.
type RefreshFunc ¶
RefreshFunc defines a function type for refreshing or computing a value in a cache, returning the value and an error.
type StaleValue ¶
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.