Documentation
¶
Overview ¶
Package fido provides a high-performance cache with optional persistence.
Index ¶
- type Cache
- func (c *Cache[K, V]) Delete(key K)
- func (c *Cache[K, V]) Fetch(key K, loader func() (V, error)) (V, error)
- func (c *Cache[K, V]) FetchTTL(key K, ttl time.Duration, loader func() (V, error)) (V, error)
- func (c *Cache[K, V]) Flush() int
- func (c *Cache[K, V]) Get(key K) (V, bool)
- func (c *Cache[K, V]) Len() int
- func (c *Cache[K, V]) Set(key K, value V)
- func (c *Cache[K, V]) SetTTL(key K, value V, ttl time.Duration)
- 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]) Fetch(ctx context.Context, key K, loader func(context.Context) (V, error)) (V, error)
- func (c *TieredCache[K, V]) FetchTTL(ctx context.Context, key K, ttl time.Duration, ...) (V, 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]) Len() int
- func (c *TieredCache[K, V]) Set(ctx context.Context, key K, value V) error
- func (c *TieredCache[K, V]) SetAsync(ctx context.Context, key K, value V) error
- func (c *TieredCache[K, V]) SetAsyncTTL(ctx context.Context, key K, value V, ttl time.Duration) error
- func (c *TieredCache[K, V]) SetTTL(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]) Delete ¶
func (c *Cache[K, V]) Delete(key K)
Delete removes a key from the cache.
func (*Cache[K, V]) Fetch ¶ added in v1.10.0
Fetch returns cached value or calls loader to compute it. Concurrent calls for the same key share one loader invocation. Computed values are stored with the default TTL.
func (*Cache[K, V]) FetchTTL ¶ added in v1.10.0
FetchTTL is like Fetch but stores computed values with an explicit TTL.
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) 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]) Fetch ¶ added in v1.10.0
func (c *TieredCache[K, V]) Fetch(ctx context.Context, key K, loader func(context.Context) (V, error)) (V, error)
Fetch returns cached value or calls loader. Concurrent calls share one loader. Computed values are stored with the default TTL.
func (*TieredCache[K, V]) FetchTTL ¶ added in v1.10.0
func (c *TieredCache[K, V]) FetchTTL(ctx context.Context, key K, ttl time.Duration, loader func(context.Context) (V, error)) (V, error)
FetchTTL is like Fetch but stores computed values with an explicit TTL.
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]) Len ¶
func (c *TieredCache[K, V]) Len() int
Len returns the memory cache size. Use Store.Len for persistence count.
func (*TieredCache[K, V]) Set ¶
func (c *TieredCache[K, V]) Set(ctx context.Context, key K, value V) error
Set stores to memory first (always), then persistence. Uses the default TTL specified at cache creation.
func (*TieredCache[K, V]) SetAsync ¶
func (c *TieredCache[K, V]) SetAsync(ctx context.Context, key K, value V) error
SetAsync stores to memory synchronously, persistence asynchronously. Uses the default TTL. Persistence errors are logged, not returned.
func (*TieredCache[K, V]) SetAsyncTTL ¶
func (c *TieredCache[K, V]) SetAsyncTTL(ctx context.Context, key K, value V, ttl time.Duration) error
SetAsyncTTL stores to memory synchronously, persistence asynchronously with explicit TTL. Persistence errors are logged, not returned.
Directories
¶
| Path | Synopsis |
|---|---|
|
pkg
|
|
|
store/cloudrun
module
|
|
|
store/compress
module
|
|
|
store/datastore
module
|
|
|
store/localfs
module
|
|
|
store/null
module
|