Documentation
¶
Overview ¶
Package multicache provides a high-performance cache with optional persistence.
Index ¶
- type Cache
- func (*Cache[K, V]) Close()
- func (c *Cache[K, V]) Delete(key K)
- func (c *Cache[K, V]) Flush() int
- func (c *Cache[K, V]) Get(key K) (V, bool)
- func (c *Cache[K, V]) GetSet(key K, loader func() (V, error), ttl ...time.Duration) (V, error)
- func (c *Cache[K, V]) Len() int
- func (c *Cache[K, V]) Set(key K, value V, ttl ...time.Duration)
- func (c *Cache[K, V]) SetIfAbsent(key K, value V, ttl ...time.Duration) (V, bool)
- type Option
- type Store
- type TieredCache
- func (c *TieredCache[K, V]) Close() error
- func (c *TieredCache[K, V]) Delete(ctx context.Context, key K) error
- func (c *TieredCache[K, V]) Flush(ctx context.Context) (int, error)
- func (c *TieredCache[K, V]) Get(ctx context.Context, key K) (V, bool, error)
- func (c *TieredCache[K, V]) GetSet(ctx context.Context, key K, loader func(context.Context) (V, error), ...) (V, error)
- func (c *TieredCache[K, V]) Len() int
- func (c *TieredCache[K, V]) Set(ctx context.Context, key K, value V, ttl ...time.Duration) error
- func (c *TieredCache[K, V]) SetAsync(ctx context.Context, key K, value V, ttl ...time.Duration) error
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 an in-memory cache. All operations are synchronous and infallible.
func New ¶
func New[K comparable, V any](opts ...Option) *Cache[K, V]
New creates an in-memory cache.
func (*Cache[K, V]) Close ¶
func (*Cache[K, V]) Close()
Close is a no-op for Cache (provided for API symmetry with TieredCache).
func (*Cache[K, V]) Delete ¶
func (c *Cache[K, V]) Delete(key K)
Delete removes a key from the cache.
func (*Cache[K, V]) GetSet ¶
GetSet returns cached value or calls loader to compute it. Concurrent calls for the same key share one loader invocation.
type Store ¶
type Store[K comparable, V any] interface { ValidateKey(key K) error Get(ctx context.Context, key K) (V, time.Time, bool, error) Set(ctx context.Context, key K, value V, expiry time.Time) error Delete(ctx context.Context, key K) error Cleanup(ctx context.Context, maxAge time.Duration) (int, error) Location(key K) string Flush(ctx context.Context) (int, error) Len(ctx context.Context) (int, error) Close() error }
Store is the persistence backend interface.
type TieredCache ¶
type TieredCache[K comparable, V any] struct { Store Store[K, V] // direct access to persistence layer // contains filtered or unexported fields }
TieredCache combines an in-memory cache with persistent storage.
func NewTiered ¶
func NewTiered[K comparable, V any](store Store[K, V], opts ...Option) (*TieredCache[K, V], error)
NewTiered creates a cache backed by the given store.
func (*TieredCache[K, V]) Close ¶
func (c *TieredCache[K, V]) Close() error
Close releases store resources.
func (*TieredCache[K, V]) Delete ¶
func (c *TieredCache[K, V]) Delete(ctx context.Context, key K) error
Delete removes from memory and persistence.
func (*TieredCache[K, V]) Flush ¶
func (c *TieredCache[K, V]) Flush(ctx context.Context) (int, error)
Flush clears memory and persistence. Returns total entries removed.
func (*TieredCache[K, V]) Get ¶
func (c *TieredCache[K, V]) Get(ctx context.Context, key K) (V, bool, error)
Get checks memory, then persistence. Found values are cached in memory.
func (*TieredCache[K, V]) GetSet ¶
func (c *TieredCache[K, V]) GetSet(ctx context.Context, key K, loader func(context.Context) (V, error), ttl ...time.Duration) (V, error)
GetSet returns cached value or calls loader. Concurrent calls share one loader.
func (*TieredCache[K, V]) Len ¶
func (c *TieredCache[K, V]) Len() int
Len returns the memory cache size. Use Store.Len for persistence count.