Versions in this module Expand all Collapse all v1 v1.0.1 May 8, 2026 v1.0.0 May 8, 2026 Changes in this version + var ErrClosed = errors.New("memcache: cache is closed") + var ErrComputeReentrant = errors.New("memcache: compute callback re-entered cache") + var ErrInvalidTTL = errors.New("memcache: ttl must be non-negative") + var ErrKeyTooLarge = errors.New("memcache: key exceeds size limit") + var ErrLoaderRateLimited = errors.New("memcache: loader rate limit exceeded") + var ErrLoaderTimeout = errors.New("memcache: loader timeout exceeded") + var ErrLoaderTooManyInFlight = errors.New("memcache: too many in-flight loads") + var ErrNoLoader = errors.New("memcache: no loader configured") + var ErrNotFound = errors.New("memcache: not found") + var ErrSnapshotCorrupt = errors.New("memcache: snapshot corrupt") + var ErrSnapshotIncompatible = errors.New("memcache: snapshot version not supported") + var ErrTooManyTags = errors.New("memcache: too many tags for entry") + var ErrUnbounded = errors.New("memcache: must specify WithMaxEntries or WithMaxBytes") + var ErrValueTooLarge = errors.New("memcache: value exceeds weight limit") + func Decrement[K comparable, V Number](c *Cache[K, V], key K) (V, error) + func DeleteCacheable[V CacheKeyer](c *Cache[string, V], proto V) bool + func GetCacheable[V CacheKeyer](c *Cache[string, V], proto V) (V, bool) + func IncrementBy[K comparable, V Number](c *Cache[K, V], key K, delta V) (V, error) + func Increment[K comparable, V Number](c *Cache[K, V], key K) (V, error) + func SetCacheable[V CacheKeyer](c *Cache[string, V], v V, opts ...SetOption) error + type AdmissionPolicy interface + Admit func(key K) bool + Observe func(key K) + Reset func() + type AdmitAlways struct + func (AdmitAlways[K]) Admit(K) bool + func (AdmitAlways[K]) Observe(K) + func (AdmitAlways[K]) Reset() + type Attr struct + Key string + Value any + type BulkLoader interface + LoadMulti func(ctx context.Context, keys []K) (map[K]LoadResult[V], error) + type Cache struct + func Must[K comparable, V any](c *Cache[K, V], err error) *Cache[K, V] + func NewUnbounded[K comparable, V any](opts ...Option) (*Cache[K, V], error) + func New[K comparable, V any](opts ...Option) (*Cache[K, V], error) + func (c *Cache[K, V]) Bytes() int64 + func (c *Cache[K, V]) Capacity() int64 + func (c *Cache[K, V]) Clear() + func (c *Cache[K, V]) Clone() (*Cache[K, V], error) + func (c *Cache[K, V]) Close() error + func (c *Cache[K, V]) Coldest(n int) []KeyedItem[K, V] + func (c *Cache[K, V]) CompareAndSwap(key K, old, newValue V) bool + func (c *Cache[K, V]) Compute(key K, fn func(cur V, ok bool) (V, ComputeAction, error)) (V, error) + func (c *Cache[K, V]) ComputeIfAbsent(key K, fn func() (V, time.Duration, error)) (value V, computed bool, err error) + func (c *Cache[K, V]) ComputeIfPresent(key K, fn func(cur V) (V, ComputeAction, error)) (V, error) + func (c *Cache[K, V]) Delete(key K) bool + func (c *Cache[K, V]) DeleteCtx(ctx context.Context, key K) (bool, error) + func (c *Cache[K, V]) DeleteExpired() int + func (c *Cache[K, V]) DeleteIf(key K, pred func(V) bool) bool + func (c *Cache[K, V]) DeleteMulti(keys []K) int + func (c *Cache[K, V]) DeletePrefix(prefix string) int + func (c *Cache[K, V]) DeleteWhere(pred func(key K, value V) bool) int + func (c *Cache[K, V]) Dump(w io.Writer) error + func (c *Cache[K, V]) Expiry(key K) (time.Time, bool) + func (c *Cache[K, V]) Get(key K) (V, bool) + func (c *Cache[K, V]) GetCtx(ctx context.Context, key K) (V, bool, error) + func (c *Cache[K, V]) GetMulti(keys []K) map[K]V + func (c *Cache[K, V]) GetMultiOrLoad(ctx context.Context, keys []K) (map[K]V, error) + func (c *Cache[K, V]) GetOrLoad(ctx context.Context, key K) (V, error) + func (c *Cache[K, V]) GetOrLoadFn(ctx context.Context, key K, ...) (V, error) + func (c *Cache[K, V]) GetOrSet(key K, value V) (V, bool, error) + func (c *Cache[K, V]) GetWithExpiry(key K) (V, time.Time, bool) + func (c *Cache[K, V]) Has(key K) bool + func (c *Cache[K, V]) Histogram() Histogram + func (c *Cache[K, V]) Hottest(n int) []KeyedItem[K, V] + func (c *Cache[K, V]) InvalidateTag(tag string) int + func (c *Cache[K, V]) InvalidateTags(tags ...string) int + func (c *Cache[K, V]) ItemMetadata(key K) (Metadata, bool) + func (c *Cache[K, V]) Items() []KeyedItem[K, V] + func (c *Cache[K, V]) Keys() []K + func (c *Cache[K, V]) Len() int + func (c *Cache[K, V]) Load(r io.Reader) (n int, err error) + func (c *Cache[K, V]) LoadFile(path string) (n int, err error) + func (c *Cache[K, V]) LongestLived(n int) []KeyedItem[K, V] + func (c *Cache[K, V]) Merge(r io.Reader) (int, error) + func (c *Cache[K, V]) Peek(key K) (V, bool) + func (c *Cache[K, V]) PeekOrAdd(key K, value V) (V, bool, error) + func (c *Cache[K, V]) Range(fn func(key K, value V) bool) + func (c *Cache[K, V]) Refresh(ctx context.Context, key K) error + func (c *Cache[K, V]) RefreshAll(ctx context.Context) int + func (c *Cache[K, V]) Reset() + func (c *Cache[K, V]) ResetStats() + func (c *Cache[K, V]) Resize(newSize int64) int + func (c *Cache[K, V]) Save(w io.Writer) (err error) + func (c *Cache[K, V]) SaveFile(path string) error + func (c *Cache[K, V]) Set(key K, value V) error + func (c *Cache[K, V]) SetCtx(ctx context.Context, key K, value V) error + func (c *Cache[K, V]) SetIfAbsent(key K, value V) (bool, error) + func (c *Cache[K, V]) SetIfPresent(key K, value V) (bool, error) + func (c *Cache[K, V]) SetMulti(items map[K]V) error + func (c *Cache[K, V]) SetWithOptions(key K, value V, opts ...SetOption) error + func (c *Cache[K, V]) SetWithTTL(key K, value V, ttl time.Duration) error + func (c *Cache[K, V]) SetWithTags(key K, value V, tags ...string) error + func (c *Cache[K, V]) SoonestExpiring(n int) []KeyedItem[K, V] + func (c *Cache[K, V]) Stats() Stats + func (c *Cache[K, V]) Subscribe(buf int, kinds ...EventKind) (<-chan Event[K, V], func()) + func (c *Cache[K, V]) Sync(ctx context.Context) error + func (c *Cache[K, V]) TTL(key K) (time.Duration, bool) + func (c *Cache[K, V]) Tags(key K) []string + func (c *Cache[K, V]) Touch(key K) bool + func (c *Cache[K, V]) TouchWithTTL(key K, ttl time.Duration) bool + func (c *Cache[K, V]) Update(key K, fn func(cur V) V) (V, error) + func (c *Cache[K, V]) View() *CacheView[K, V] + type CacheKeyer interface + CacheKey func() string + type CacheTTLer interface + CacheTTL func() time.Duration + type CacheTagger interface + CacheTags func() []string + type CacheView struct + func (v *CacheView[K, V]) Bytes() int64 + func (v *CacheView[K, V]) Expiry(key K) (time.Time, bool) + func (v *CacheView[K, V]) Get(key K) (V, bool) + func (v *CacheView[K, V]) Has(key K) bool + func (v *CacheView[K, V]) Keys() []K + func (v *CacheView[K, V]) Len() int + func (v *CacheView[K, V]) Peek(key K) (V, bool) + func (v *CacheView[K, V]) Range(fn func(key K, value V) bool) + func (v *CacheView[K, V]) Stats() Stats + func (v *CacheView[K, V]) TTL(key K) (time.Duration, bool) + type Cacheable interface + type CapacityError struct + Cause error + Key any + LimitField string + Reason string + func (e *CapacityError) Error() string + func (e *CapacityError) Is(target error) bool + func (e *CapacityError) Unwrap() error + type Clock interface + AfterFunc func(d time.Duration, fn func()) Timer + Now func() time.Time + type Codec interface + Marshal func(v any) ([]byte, error) + Name func() string + Unmarshal func(data []byte, v any) error + type CodecError struct + Codec string + Err error + Op string + func (e *CodecError) Error() string + func (e *CodecError) Is(target error) bool + func (e *CodecError) Unwrap() error + type CompressedCodec struct + func NewCompressedCodec(base Codec, level int) CompressedCodec + func (c CompressedCodec) Marshal(v any) ([]byte, error) + func (c CompressedCodec) Name() string + func (c CompressedCodec) Unmarshal(data []byte, v any) error + type ComputeAction uint8 + const ComputeDelete + const ComputeNoOp + const ComputeStore + func (a ComputeAction) String() string + type ConfigError struct + Field string + Message string + func (e *ConfigError) Error() string + func (e *ConfigError) Is(target error) bool + type Doorkeeper struct + func NewDoorkeeper[K comparable](expected int, fn func(K) uint64) *Doorkeeper[K] + func (d *Doorkeeper[K]) Admit(key K) bool + func (d *Doorkeeper[K]) Observe(key K) + func (d *Doorkeeper[K]) Reset() + type EncryptedCodec struct + func NewEncryptedCodec(base Codec, key []byte) (*EncryptedCodec, error) + func (c *EncryptedCodec) Marshal(v any) ([]byte, error) + func (c *EncryptedCodec) Name() string + func (c *EncryptedCodec) Unmarshal(data []byte, v any) error + type Event struct + At time.Time + Err error + Key K + Kind EventKind + Reason EvictionReason + Tags []string + Value V + type EventKind uint8 + const EventEvict + const EventExpire + const EventInsert + const EventInvalidateTag + const EventLoad + const EventLoadError + const EventLoadRateLimited + const EventLoadTimeout + const EventResize + const EventSnapshot + const EventUpdate + func (k EventKind) String() string + type EvictionReason uint8 + const EvictReasonCapacity + const EvictReasonClear + const EvictReasonComputed + const EvictReasonDeleted + const EvictReasonDeletedPrefix + const EvictReasonDeletedWhere + const EvictReasonExpireFunc + const EvictReasonExpired + const EvictReasonRemote + const EvictReasonReplaced + const EvictReasonResize + const EvictReasonStoreRollback + const EvictReasonTag + func (r EvictionReason) String() string + type FakeClock struct + func NewFakeClock(t time.Time) *FakeClock + func (c *FakeClock) Advance(d time.Duration) + func (c *FakeClock) AfterFunc(d time.Duration, fn func()) Timer + func (c *FakeClock) Now() time.Time + func (c *FakeClock) Set(t time.Time) + type GobCodec struct + func (GobCodec) Marshal(v any) ([]byte, error) + func (GobCodec) Name() string + func (GobCodec) Unmarshal(data []byte, v any) error + type Histogram struct + AgeBuckets [8]int64 + HitsBuckets [8]int64 + TotalEntries int + TotalWeight int64 + WeightBuckets [8]int64 + type Item struct + Expiry time.Time + Hits uint32 + Inserted time.Time + LastAccess time.Time + Sliding bool + Tags []string + Value V + Weight int64 + type JSONCodec struct + func (JSONCodec) Marshal(v any) ([]byte, error) + func (JSONCodec) Name() string + func (JSONCodec) Unmarshal(data []byte, v any) error + type KeyedItem struct + Key K + type LoadError struct + Err error + Key any + func (e *LoadError) Error() string + func (e *LoadError) Is(target error) bool + func (e *LoadError) Unwrap() error + type LoadResult struct + Err error + TTL time.Duration + Value V + type Loader interface + Load func(ctx context.Context, key K) (value V, ttl time.Duration, err error) + type LoaderFunc func(ctx context.Context, key K) (V, time.Duration, error) + func (f LoaderFunc[K, V]) Load(ctx context.Context, key K) (V, time.Duration, error) + type MemoryStore struct + func NewMemoryStore[K comparable, V any](clock Clock) *MemoryStore[K, V] + func (m *MemoryStore[K, V]) Close() error + func (m *MemoryStore[K, V]) Delete(ctx context.Context, key K) (bool, error) + func (m *MemoryStore[K, V]) Get(ctx context.Context, key K) (V, bool, error) + func (m *MemoryStore[K, V]) Iterate(ctx context.Context, fn func(key K, value V) bool) error + func (m *MemoryStore[K, V]) Len(ctx context.Context) (int, error) + func (m *MemoryStore[K, V]) Set(ctx context.Context, key K, value V, ttl time.Duration) error + type Metadata struct + Expiry time.Time + Hits uint32 + Inserted time.Time + LastAccess time.Time + Sliding bool + Tags []string + Weight int64 + type Number interface + type Option func(*config) + func WithAdmissionPolicy[K comparable](p AdmissionPolicy[K]) Option + func WithAsyncWrites() Option + func WithAutoLoad(path string) Option + func WithAutoLoadIgnoreErrors(b bool) Option + func WithAutoSave(path string, interval time.Duration) Option + func WithBulkLoader[K comparable, V any](l BulkLoader[K, V]) Option + func WithCallbackTimeout(d time.Duration) Option + func WithClock(clk Clock) Option + func WithCodec(codec Codec) Option + func WithCollisionTracking(b bool) Option + func WithCompressedCodec(base Codec, level int) Option + func WithCopyOnGet[V any](fn func(V) V) Option + func WithDefaultTTL(d time.Duration) Option + func WithDoorkeeper(b bool) Option + func WithEncryptedCodec(base Codec, key []byte) Option + func WithErrorTTL(d time.Duration) Option + func WithEventsBuffer(n int) Option + func WithExpireFunc[K comparable, V any](fn func(key K, value V, meta Metadata) bool) Option + func WithExpvar(name string) Option + func WithFlatStorage() Option + func WithGroup(name string, capacity int) Option + func WithHasher[K comparable](fn func(K) uint64) Option + func WithInvalidationPublisher[K comparable](fn func(key K, reason EvictionReason)) Option + func WithInvalidationSubscriber[K comparable](ch <-chan K) Option + func WithJanitorInterval(d time.Duration) Option + func WithLoaderRateLimit(perSecond int) Option + func WithLoaderTimeout(d time.Duration) Option + func WithLoader[K comparable, V any](l Loader[K, V]) Option + func WithLockFreeRead() Option + func WithLogger(l *slog.Logger) Option + func WithMaxBytes(n int64) Option + func WithMaxConcurrentLoads(n int) Option + func WithMaxEntries(n int) Option + func WithMaxKeySize(n int) Option + func WithMaxSnapshotBytes(n int64) Option + func WithMaxTagsPerEntry(n int) Option + func WithMaxTagsTotal(n int) Option + func WithMaxValueWeight(n int64) Option + func WithName(name string) Option + func WithNegativeCache(negativeTTL time.Duration) Option + func WithOnEvict[K comparable, V any](fn func(key K, value V, reason EvictionReason)) Option + func WithOnExpire[K comparable, V any](fn func(key K, value V)) Option + func WithOnHit[K comparable, V any](fn func(key K, value V)) Option + func WithOnLoad[K comparable, V any](fn func(key K, value V, ttl time.Duration, err error)) Option + func WithOnMiss[K comparable](fn func(key K)) Option + func WithPolicy(p Policy) Option + func WithPurgeVisitor[K comparable, V any](fn func(key K, value V) error) Option + func WithRefreshAhead(refreshAt float64) Option + func WithSafeKeys(b bool) Option + func WithShardedStats(b bool) Option + func WithShards(n int) Option + func WithSlidingTTL(b bool) Option + func WithSnapshotMetadata(meta map[string]string) Option + func WithStaleWhileRevalidate(staleFor time.Duration) Option + func WithStatsEnabled(b bool) Option + func WithStore[K comparable, V any](store Store[K, V]) Option + func WithTTLBuckets(slots, tickPerBucket int) Option + func WithTTLJitter(j time.Duration) Option + func WithTracer(t Tracer) Option + func WithWeigher[V any](fn Weigher[V]) Option + type Policy uint8 + const Policy2Q + const PolicyARC + const PolicyFIFO + const PolicyLFU + const PolicyLRU + const PolicyS3FIFO + const PolicyTinyLFU + func (p Policy) String() string + type PolicyDetail2Q struct + A1inSize int + A1outSize int + AmSize int + type PolicyDetailARC struct + B1Size int + B2Size int + P int + T1Size int + T2Size int + type PolicyDetailFIFO struct + Size int + type PolicyDetailLFU struct + DistinctBuckets int + Size int + type PolicyDetailLRU struct + Size int + type PolicyDetailS3FIFO struct + GhostSize int + MainBudget int + MainSize int + SmallBudget int + SmallSize int + type PolicyDetailTinyLFU struct + MainSize int + SketchOps uint64 + WindowSize int + type Prefixer interface + HasPrefix func(prefix string) bool + type RawCodec struct + func (RawCodec) Marshal(v any) ([]byte, error) + func (RawCodec) Name() string + func (RawCodec) Unmarshal(data []byte, v any) error + type RealClock struct + func (RealClock) AfterFunc(d time.Duration, fn func()) Timer + func (RealClock) Now() time.Time + type SetOption func(*setConfig) + func SetExpireAt(t time.Time) SetOption + func SetSliding(sliding bool) SetOption + func SetTTL(ttl time.Duration) SetOption + func SetTags(tags ...string) SetOption + func SetWeight(w int64) SetOption + type SnapshotError struct + Err error + Message string + Offset int64 + Op string + Path string + func (e *SnapshotError) Error() string + func (e *SnapshotError) Is(target error) bool + func (e *SnapshotError) Unwrap() error + type SnapshotInfo struct + Codec string + Count int64 + Metadata map[string]string + Name string + SaveTime time.Time + SizeBytes int64 + Version uint8 + func InspectSnapshot(path string) (*SnapshotInfo, error) + type SnapshotMarshaler interface + SnapshotMarshal func() ([]byte, error) + type SnapshotUnmarshaler interface + SnapshotUnmarshal func(data []byte) error + type Span interface + End func(err error) + SetAttr func(key string, value any) + type Stats struct + AdmissionRejects uint64 + At time.Time + Bytes int64 + Capacity int64 + Compactions uint64 + Deletes uint64 + Entries int64 + EventsDropped uint64 + Evictions uint64 + EvictionsByReason [numEvictionReasons]uint64 + Expirations uint64 + HashCollisions uint64 + Hits uint64 + Inserts uint64 + LastResetAt time.Time + LastSnapshotAt time.Time + LoadCachedError uint64 + LoadCoalesced uint64 + LoadErrors uint64 + LoadHits uint64 + LoadLatency time.Duration + LoadLatencyP50 time.Duration + LoadLatencyP99 time.Duration + LoadRateLimited uint64 + LoadTimeouts uint64 + LoadsTotal uint64 + Misses uint64 + NegativeHits uint64 + PolicyDetail any + PolicyName string + RefreshAhead uint64 + Resizes uint64 + StaleWhileRevalidate uint64 + TagCleanupBacklog int + TagInvalidations uint64 + TagsTracked int + Updates uint64 + Uptime time.Duration + func (s Stats) HitRate() float64 + type Store interface + Close func() error + Delete func(ctx context.Context, key K) (bool, error) + Get func(ctx context.Context, key K) (V, bool, error) + Iterate func(ctx context.Context, fn func(key K, value V) bool) error + Len func(ctx context.Context) (int, error) + Set func(ctx context.Context, key K, value V, ttl time.Duration) error + type Tiered struct + func NewTiered[K comparable, V any](l1, l2 *Cache[K, V]) *Tiered[K, V] + func (t *Tiered[K, V]) Close() error + func (t *Tiered[K, V]) Delete(key K) bool + func (t *Tiered[K, V]) Get(key K) (V, bool) + func (t *Tiered[K, V]) GetCtx(ctx context.Context, key K) (V, bool, error) + func (t *Tiered[K, V]) Has(key K) bool + func (t *Tiered[K, V]) InvalidateTag(tag string) int + func (t *Tiered[K, V]) InvalidateTags(tags ...string) int + func (t *Tiered[K, V]) L1() *Cache[K, V] + func (t *Tiered[K, V]) L2() *Cache[K, V] + func (t *Tiered[K, V]) Len() int + func (t *Tiered[K, V]) Set(key K, value V) error + func (t *Tiered[K, V]) SetWithOptions(key K, value V, opts ...SetOption) error + func (t *Tiered[K, V]) SetWithTTL(key K, value V, ttl time.Duration) error + func (t *Tiered[K, V]) Stats() TieredStats + func (t *Tiered[K, V]) Sync(ctx context.Context) error + type TieredStats struct + L1 Stats + L1Hits uint64 + L2 Stats + L2Hits uint64 + Misses uint64 + Promotions uint64 + type Timer interface + Reset func(d time.Duration) bool + Stop func() bool + type Tracer interface + Start func(ctx context.Context, op string, attrs ...Attr) (context.Context, Span) + type Weigher func(V) int64 + func BytesWeigher() Weigher[[]byte] + func StringWeigher() Weigher[string] + func UnitWeigher[V any]() Weigher[V]