Documentation
ΒΆ
Overview ΒΆ
Package bdcache provides a high-performance cache with S3-FIFO eviction and optional persistence.
Index ΒΆ
- type Entry
- type MemoryCache
- func (*MemoryCache[K, V]) Close()
- func (c *MemoryCache[K, V]) Delete(key K)
- func (c *MemoryCache[K, V]) Flush() int
- func (c *MemoryCache[K, V]) Get(key K) (V, bool)
- func (c *MemoryCache[K, V]) GetOrSet(key K, loader func() V, ttl ...time.Duration) V
- func (c *MemoryCache[K, V]) Len() int
- func (c *MemoryCache[K, V]) Set(key K, value V, ttl ...time.Duration)
- func (c *MemoryCache[K, V]) SetIfAbsent(key K, value V, ttl ...time.Duration) (V, bool)
- type Option
- type PersistenceLayer
- type PersistentCache
- func (c *PersistentCache[K, V]) Close() error
- func (c *PersistentCache[K, V]) Delete(ctx context.Context, key K) error
- func (c *PersistentCache[K, V]) Flush(ctx context.Context) (int, error)
- func (c *PersistentCache[K, V]) Get(ctx context.Context, key K) (V, bool, error)
- func (c *PersistentCache[K, V]) GetOrSet(ctx context.Context, key K, loader func(context.Context) (V, error), ...) (V, error)
- func (c *PersistentCache[K, V]) Len() int
- func (c *PersistentCache[K, V]) Set(ctx context.Context, key K, value V, ttl ...time.Duration) error
- func (c *PersistentCache[K, V]) SetAsync(ctx context.Context, key K, value V, ttl ...time.Duration) (<-chan error, error)
Constants ΒΆ
This section is empty.
Variables ΒΆ
This section is empty.
Functions ΒΆ
This section is empty.
Types ΒΆ
type MemoryCache ΒΆ added in v0.9.7
type MemoryCache[K comparable, V any] struct { // contains filtered or unexported fields }
MemoryCache is a fast in-memory cache without persistence. All operations are context-free and never return errors.
func Memory ΒΆ added in v0.9.7
func Memory[K comparable, V any](opts ...Option) *MemoryCache[K, V]
Memory creates a new memory-only cache.
Example:
cache := bdcache.Memory[string, User](
bdcache.WithSize(10000),
bdcache.WithTTL(time.Hour),
)
defer cache.Close()
cache.Set("user:123", user) // uses default TTL
cache.Set("user:123", user, time.Hour) // explicit TTL
user, ok := cache.Get("user:123")
func (*MemoryCache[K, V]) Close ΒΆ added in v0.9.7
func (*MemoryCache[K, V]) Close()
Close releases resources held by the cache. For MemoryCache this is a no-op, but provided for API consistency.
func (*MemoryCache[K, V]) Delete ΒΆ added in v0.9.7
func (c *MemoryCache[K, V]) Delete(key K)
Delete removes a value from the cache.
func (*MemoryCache[K, V]) Flush ΒΆ added in v0.9.7
func (c *MemoryCache[K, V]) Flush() int
Flush removes all entries from the cache. Returns the number of entries removed.
func (*MemoryCache[K, V]) Get ΒΆ added in v0.9.7
func (c *MemoryCache[K, V]) Get(key K) (V, bool)
Get retrieves a value from the cache. Returns the value and true if found, or the zero value and false if not found.
func (*MemoryCache[K, V]) GetOrSet ΒΆ added in v0.9.7
func (c *MemoryCache[K, V]) GetOrSet(key K, loader func() V, ttl ...time.Duration) V
GetOrSet retrieves a value from the cache, or computes and stores it if not found. The loader function is only called if the key is not in the cache. If no TTL is provided, the default TTL is used. This is optimized to perform a single shard lookup and lock acquisition.
func (*MemoryCache[K, V]) Len ΒΆ added in v0.9.7
func (c *MemoryCache[K, V]) Len() int
Len returns the number of items in the cache.
func (*MemoryCache[K, V]) Set ΒΆ added in v0.9.7
func (c *MemoryCache[K, V]) Set(key K, value V, ttl ...time.Duration)
Set stores a value in the cache. If no TTL is provided, the default TTL is used. If no default TTL is configured, the item never expires.
func (*MemoryCache[K, V]) SetIfAbsent ΒΆ added in v0.9.8
func (c *MemoryCache[K, V]) SetIfAbsent(key K, value V, ttl ...time.Duration) (V, bool)
SetIfAbsent stores a value only if the key is not already in the cache. Returns the existing value and true if found, or the new value and false if inserted. This is optimized to perform a single shard lookup and lock acquisition.
type Option ΒΆ
type Option func(*config)
Option configures a MemoryCache or PersistentCache.
func WithTTL ΒΆ added in v0.9.7
WithTTL sets the default TTL for cache items. Items without an explicit TTL will use this value.
func WithWarmup ΒΆ
WithWarmup enables cache warmup by loading the N most recently updated entries from persistence on startup. Only applies to PersistentCache. By default, warmup is disabled (0). Set to a positive number to load that many entries.
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 // Flush removes all entries from persistent storage. // Returns the number of entries removed and any error. Flush(ctx context.Context) (int, error) // Len returns the number of entries in persistent storage. Len(ctx context.Context) (int, error) // Close releases any resources held by the persistence layer. Close() error }
PersistenceLayer defines the interface for cache persistence backends.
type PersistentCache ΒΆ added in v0.9.7
type PersistentCache[K comparable, V any] struct { // Store provides direct access to the persistence layer. // Use this for persistence-specific operations: // cache.Store.Len(ctx) // cache.Store.Flush(ctx) // cache.Store.Cleanup(ctx, maxAge) Store PersistenceLayer[K, V] // contains filtered or unexported fields }
PersistentCache is a cache backed by both memory and persistent storage. Core operations require context for I/O, while memory operations like Len() do not.
func Persistent ΒΆ added in v0.9.7
func Persistent[K comparable, V any](ctx context.Context, p PersistenceLayer[K, V], opts ...Option) (*PersistentCache[K, V], error)
Persistent creates a cache with persistence backing.
Example:
store, _ := localfs.New[string, User]("myapp", "")
cache, err := bdcache.Persistent[string, User](ctx, store,
bdcache.WithSize(10000),
bdcache.WithTTL(time.Hour),
bdcache.WithWarmup(1000),
)
if err != nil {
return err
}
defer cache.Close()
cache.Set(ctx, "user:123", user) // uses default TTL
cache.Set(ctx, "user:123", user, time.Hour) // explicit TTL
user, ok, err := cache.Get(ctx, "user:123")
storeCount, _ := cache.Store.Len(ctx)
func (*PersistentCache[K, V]) Close ΒΆ added in v0.9.7
func (c *PersistentCache[K, V]) Close() error
Close releases resources held by the cache.
func (*PersistentCache[K, V]) Delete ΒΆ added in v0.9.7
func (c *PersistentCache[K, V]) Delete(ctx context.Context, key K) error
Delete removes a value from the cache. The value is always removed from memory. Returns an error if persistence deletion fails.
func (*PersistentCache[K, V]) Flush ΒΆ added in v0.9.7
func (c *PersistentCache[K, V]) Flush(ctx context.Context) (int, error)
Flush removes all entries from the cache, including persistent storage. Returns the total number of entries removed from memory and persistence.
func (*PersistentCache[K, V]) Get ΒΆ added in v0.9.7
func (c *PersistentCache[K, V]) Get(ctx context.Context, key K) (V, bool, error)
Get retrieves a value from the cache. It first checks the memory cache, then falls back to persistence.
func (*PersistentCache[K, V]) GetOrSet ΒΆ added in v0.9.7
func (c *PersistentCache[K, V]) GetOrSet(ctx context.Context, key K, loader func(context.Context) (V, error), ttl ...time.Duration) (V, error)
GetOrSet retrieves a value from the cache, or computes and stores it if not found. The loader function is only called if the key is not in the cache. If no TTL is provided, the default TTL is used. If the loader returns an error, it is propagated.
func (*PersistentCache[K, V]) Len ΒΆ added in v0.9.7
func (c *PersistentCache[K, V]) Len() int
Len returns the number of items in the memory cache. For persistence item count, use cache.Store.Len(ctx).
func (*PersistentCache[K, V]) Set ΒΆ added in v0.9.7
func (c *PersistentCache[K, V]) Set(ctx context.Context, key K, value V, ttl ...time.Duration) error
Set stores a value in the cache. If no TTL is provided, the default TTL is used. The value is ALWAYS stored in memory, even if persistence fails. Returns an error if the key violates persistence constraints or if persistence fails.
func (*PersistentCache[K, V]) SetAsync ΒΆ added in v0.9.7
func (c *PersistentCache[K, V]) SetAsync(ctx context.Context, key K, value V, ttl ...time.Duration) (<-chan error, error)
SetAsync stores a value in the cache, handling persistence asynchronously. If no TTL is provided, the default TTL is used. Key validation and in-memory caching happen synchronously. Returns a channel that will receive any persistence error (or be closed on success). Returns an error only for validation failures (e.g., invalid key format).