Documentation
¶
Overview ¶
Package memory provides a process-local byte cache with synchronous writes.
Index ¶
- Constants
- type Config
- type Store
- func (s *Store) Clear(ctx context.Context) error
- func (s *Store) Close(context.Context) error
- func (s *Store) Delete(ctx context.Context, key string) error
- func (s *Store) Exists(ctx context.Context, key string) (bool, error)
- func (s *Store) Get(ctx context.Context, key string) ([]byte, bool, error)
- func (s *Store) GetInto(ctx context.Context, key string, dst []byte) ([]byte, bool, error)
- func (s *Store) GetMulti(ctx context.Context, keys []string) (map[string][]byte, error)
- func (s *Store) PruneExpired(ctx context.Context, limit int) (int, error)
- func (s *Store) Remember(ctx context.Context, key string, ttl time.Duration, ...) (any, error)
- func (s *Store) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (s *Store) SetMulti(ctx context.Context, items map[string][]byte, ttl time.Duration) error
- func (s *Store) Usage() Usage
- type Usage
Constants ¶
const DefaultMaxBytes int64 = 64 << 20
DefaultMaxBytes bounds retained key/value bytes, not total process memory.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
Config limits retained entries and key/value bytes. MaxBytes defaults to DefaultMaxBytes when zero. Capacity and MaxEntryBytes must be positive. Map metadata, allocator overhead and caller-owned Get results are additional.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store supports concurrent use. Its zero value is not usable; use New. It rejects writes with ErrFull instead of evicting live entries. Constructors start no goroutines. Expiration is lazy; PruneExpired allows explicit cleanup.
func New ¶
New creates a cache with a 64 MiB key/value budget. Use NewWithConfig to override that budget. Each entry includes its key in maxEntryBytes.
func NewWithConfig ¶
NewWithConfig constructs an empty cache without preallocating its byte budget.
func (*Store) Clear ¶
Clear releases retained entries and keeps the store usable. It is atomic relative to memory-store writes; a later write may immediately refill it.
func (*Store) Close ¶
Close releases all retained storage and is idempotent. It does not cancel in-flight reads; stop users before closing if their completion must be joined.
func (*Store) GetInto ¶
GetInto appends a hit to dst, reusing its capacity when possible. The returned slice belongs to the caller; the cache never retains dst. On a miss or error, dst is returned unchanged. Pass dst[:0] to reuse a buffer between reads. This optional memory-specific API avoids allocating a result on every hit.
func (*Store) PruneExpired ¶
PruneExpired removes at most limit expired entries (1–10000). It may inspect all entries under the write lock; schedule modest batches outside hot paths. Cancellation can return both a positive removal count and an error. No live entry is evicted. The caller owns any scheduling and shutdown of maintenance.
func (*Store) Remember ¶
func (s *Store) Remember(ctx context.Context, key string, ttl time.Duration, load func(context.Context) (any, error)) (any, error)
Remember returns a cached JSON value or loads it on a miss. See cache.Remember for validation, error, expiry and concurrent-loading semantics.