Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[K comparable, V any] interface { Fetcher[K, V] // Embeds Fetch and Close methods Invalidate(ctx context.Context, key K) error }
Cache read/write/invalidate interface
type Fetcher ¶
type Fetcher[K comparable, V any] interface { // Fetch retrieves a value by its key. The implementation is responsible // for its own retrieval logic, which may include falling back to another // Fetcher. Fetch(ctx context.Context, key K) (V, error) io.Closer }
Fetcher defines the public, read-only contract for any component that can retrieve data by a key. This is the primary interface that consuming services should depend on.
type Firestore ¶
type Firestore[K comparable, V any] struct { // contains filtered or unexported fields }
Firestore is a generic data fetcher for a specific Firestore collection. It implements the Fetcher interface and acts as a "source of truth" that a caching Fetcher can use as a fallback.
func NewFirestore ¶
func NewFirestore[K comparable, V any]( _ context.Context, cfg *FirestoreConfig, client *firestore.Client, logger zerolog.Logger, ) (*Firestore[K, V], error)
NewFirestore creates a new generic Firestore Fetcher.
type FirestoreConfig ¶
FirestoreConfig holds configuration for the Firestore client.
type InMemoryCache ¶
type InMemoryCache[K comparable, V any] struct { // contains filtered or unexported fields }
InMemoryCache is a generic, thread-safe, in-memory cache. It implements the Fetcher interface and can be configured with a fallback Fetcher to use on a cache miss.
func NewInMemoryCache ¶
func NewInMemoryCache[K comparable, V any](fallback Fetcher[K, V]) *InMemoryCache[K, V]
NewInMemoryCache creates a new in-memory cache. It can optionally be provided with a fallback Fetcher, which will be used to populate the cache on a miss.
func (*InMemoryCache[K, V]) Close ¶
func (c *InMemoryCache[K, V]) Close() error
Close is a no-op for the in-memory cache but satisfies the Fetcher interface.
func (*InMemoryCache[K, V]) Fetch ¶
func (c *InMemoryCache[K, V]) Fetch(ctx context.Context, key K) (V, error)
Fetch retrieves an item. It first checks its internal in-memory map. If the key is not found (a cache miss), and a fallback Fetcher is configured, it will attempt to fetch the data from the fallback. If the fallback fetch is successful, it writes the result to its own cache for future requests.
func (*InMemoryCache[K, V]) Invalidate ¶
func (c *InMemoryCache[K, V]) Invalidate(_ context.Context, key K) error
type InMemoryLRUCache ¶
type InMemoryLRUCache[K comparable, V any] struct { // contains filtered or unexported fields }
InMemoryLRUCache is a generic, thread-safe, in-memory cache with a fixed size and a Least Recently Used (LRU) eviction policy. It implements the Fetcher interface and can be configured with a fallback Fetcher to use on a cache miss.
func NewInMemoryLRUCache ¶
func NewInMemoryLRUCache[K comparable, V any](maxSize int, fallback Fetcher[K, V]) (*InMemoryLRUCache[K, V], error)
NewInMemoryLRUCache creates a new size-limited, in-memory LRU cache. - maxSize: The maximum number of items to store in the cache. Must be > 0. - fallback: An optional Fetcher to use to populate the cache on a miss.
func (*InMemoryLRUCache[K, V]) Close ¶
func (c *InMemoryLRUCache[K, V]) Close() error
Close is a no-op for the in-memory cache but satisfies the Fetcher interface.
func (*InMemoryLRUCache[K, V]) Fetch ¶
func (c *InMemoryLRUCache[K, V]) Fetch(ctx context.Context, key K) (V, error)
Fetch retrieves an item. It first checks its internal in-memory map. If the key is found (a cache hit), it moves the item to the front of the recency list. If the key is not found (a cache miss), and a fallback is configured, it fetches the data from the fallback, adds it to the cache, and potentially evicts the least recently used item if the cache is full.
func (*InMemoryLRUCache[K, V]) Invalidate ¶
func (c *InMemoryLRUCache[K, V]) Invalidate(_ context.Context, key K) error
type RedisCache ¶
type RedisCache[K comparable, V any] struct { // contains filtered or unexported fields }
RedisCache is a generic cache implementation using Redis. It implements the Fetcher interface and can be configured with a fallback Fetcher to use on a cache miss.
func NewRedisCache ¶
func NewRedisCache[K comparable, V any]( ctx context.Context, cfg *RedisConfig, logger zerolog.Logger, fallback Fetcher[K, V], ) (*RedisCache[K, V], error)
NewRedisCache creates and connects a new generic RedisCache. It pings the Redis server to ensure connectivity before returning. It can optionally be provided with a fallback Fetcher to use on a cache miss.
func (*RedisCache[K, V]) Close ¶
func (c *RedisCache[K, V]) Close() error
Close closes the Redis client connection.
func (*RedisCache[K, V]) Fetch ¶
func (c *RedisCache[K, V]) Fetch(ctx context.Context, key K) (V, error)
Fetch retrieves an item by key. It first checks Redis. On a cache miss, if a fallback is configured, it fetches from the fallback, writes the result back to Redis in the background, and returns the value.
func (*RedisCache[K, V]) Invalidate ¶
func (c *RedisCache[K, V]) Invalidate(ctx context.Context, key K) error