Documentation
¶
Overview ¶
Package bdcache provides a high-performance cache with S3-FIFO eviction and optional persistence.
Index ¶
- type Cache
- func (c *Cache[K, V]) Cleanup() int
- func (c *Cache[K, V]) Close() error
- func (c *Cache[K, V]) Delete(ctx context.Context, key K)
- func (c *Cache[K, V]) Get(ctx context.Context, key K) (V, bool, error)
- func (c *Cache[K, V]) Len() int
- func (c *Cache[K, V]) Set(ctx context.Context, key K, value V, ttl time.Duration) error
- func (c *Cache[K, V]) SetAsync(ctx context.Context, key K, value V, ttl time.Duration) error
- type Entry
- type Option
- type Options
- type PersistenceLayer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[K comparable, V any] struct { // contains filtered or unexported fields }
Cache is a generic cache with memory and optional persistence layers.
func (*Cache[K, V]) Cleanup ¶
Cleanup removes expired entries from the cache. Returns the number of entries removed.
func (*Cache[K, V]) Get ¶
Get retrieves a value from the cache. It first checks the memory cache, then falls back to persistence if available.
func (*Cache[K, V]) Set ¶
Set stores a value in the cache with an optional TTL. A zero TTL means no expiration (or uses DefaultTTL if configured). The value is ALWAYS stored in memory, even if persistence fails. Returns an error if the key violates persistence constraints or if persistence fails. Even when an error is returned, the value is cached in memory.
func (*Cache[K, V]) SetAsync ¶ added in v0.6.0
SetAsync adds or updates a value in the cache with optional TTL, handling persistence asynchronously. Key validation and in-memory caching happen synchronously. Persistence errors are logged but not returned. Returns an error only for validation failures (e.g., invalid key format).
type Option ¶
type Option func(*Options)
Option is a functional option for configuring a Cache.
func WithCleanup ¶ added in v0.6.0
WithCleanup enables background cleanup of expired entries at startup. maxAge should be set to your maximum TTL value - entries older than this are deleted. This is a safety net for expired data and works alongside native Datastore TTL policies. If native TTL is properly configured, this cleanup will be fast (no-op).
func WithDefaultTTL ¶
WithDefaultTTL sets the default TTL for cache items.
func WithMemorySize ¶
WithMemorySize sets the maximum number of items in the memory cache.
func WithPersistence ¶ added in v0.6.0
func WithPersistence[K comparable, V any](p PersistenceLayer[K, V]) Option
WithPersistence sets the persistence layer for the cache. Pass a PersistenceLayer implementation from packages like:
- github.com/codeGROOVE-dev/bdcache/persist/localfs
- github.com/codeGROOVE-dev/bdcache/persist/datastore
Example:
p, _ := localfs.New[string, int]("myapp")
cache, _ := bdcache.New[string, int](ctx, bdcache.WithPersistence(p))
func WithWarmup ¶
WithWarmup enables cache warmup by loading the N most recently updated entries from persistence on startup. By default, warmup is disabled (0). Set to a positive number to load that many entries.
type Options ¶
type Options struct {
Persister any
MemorySize int
DefaultTTL time.Duration
WarmupLimit int
CleanupMaxAge time.Duration
CleanupEnabled bool
}
Options configures a Cache instance.
type PersistenceLayer ¶
type PersistenceLayer[K comparable, V any] interface { // ValidateKey checks if a key is valid for this persistence layer. // Returns an error if the key violates constraints. ValidateKey(key K) error // Load retrieves a value from persistent storage. // Returns the value, expiry time, whether it was found, and any error. Load(ctx context.Context, key K) (V, time.Time, bool, error) // Store saves a value to persistent storage with an expiry time. Store(ctx context.Context, key K, value V, expiry time.Time) error // Delete removes a value from persistent storage. Delete(ctx context.Context, key K) error // LoadRecent returns channels for streaming the most recently updated entries from persistent storage. // Used for warming up the cache on startup. Returns up to 'limit' most recently updated entries. // If limit is 0, returns all entries. // The entry channel should be closed when all entries have been sent. // If an error occurs, send it on the error channel. LoadRecent(ctx context.Context, limit int) (<-chan Entry[K, V], <-chan error) // Cleanup removes expired entries from persistent storage. // maxAge specifies how old entries must be before deletion. // Returns the number of entries deleted and any error. Cleanup(ctx context.Context, maxAge time.Duration) (int, error) // Location returns the storage location/identifier for a given key. // For file-based persistence, this returns the file path. // For database persistence, this returns the database key/ID. // Useful for testing and debugging to verify where items are stored. Location(key K) string // Close releases any resources held by the persistence layer. Close() error }
PersistenceLayer defines the interface for cache persistence backends.