Documentation
¶
Overview ¶
Package cache provides a fast, in-memory, generic key-value cache for Go backend services. It shards its keyspace across independent locks for concurrent throughput, supports per-entry TTLs, and evicts via a pluggable eviction.Policy (LRU, LFU, FIFO, or a custom implementation) once a capacity is set.
Index ¶
- type Cache
- type Option
- func WithCapacity[K comparable, V any](perShard int) Option[K, V]
- func WithDefaultTTL[K comparable, V any](ttl time.Duration) Option[K, V]
- func WithEvictionPolicy[K comparable, V any](f eviction.Factory[K]) Option[K, V]
- func WithJanitorInterval[K comparable, V any](d time.Duration) Option[K, V]
- func WithOnEvict[K comparable, V any](fn func(key K, value V)) Option[K, V]
- func WithShards[K comparable, V any](n int) Option[K, V]
- type SetOption
- type Stats
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[K comparable, V any] interface { // Get returns the value stored under key and true, or the zero value // and false if the key is absent or has expired. Get(key K) (V, bool) // Set stores value under key, applying the cache's default TTL unless // a SetOption overrides it. Set never fails: on a full, capacity-bounded // cache it evicts via the configured Policy to make room. Set(key K, value V, opts ...SetOption) // Delete removes key and reports whether it was present. Delete(key K) bool // Has reports whether key is present and unexpired, without affecting // eviction order the way Get does. Has(key K) bool // Len returns the number of entries currently stored, including any // not-yet-swept expired entries. Len() int // Keys returns a snapshot of all live (unexpired at call time) keys. // It allocates and copies, so avoid it on hot paths. Keys() []K // Clear removes every entry from the cache. Clear() // Close stops the cache's background janitor goroutine. Safe to call // once; the cache remains usable afterwards, it just stops sweeping // expired entries proactively (Get still treats them as absent). Close() error // Stats returns a snapshot of hit/miss/eviction counters accumulated // since the cache was created. Stats() Stats }
Cache is the public interface implemented by *Cache. It exists so consumers can depend on an interface (and swap in a mock, or an alternative implementation) instead of a concrete type.
func New ¶
func New[K comparable, V any](opts ...Option[K, V]) Cache[K, V]
New builds a Cache configured by the given options. With no options it returns an unbounded cache with 32 shards and no TTL: entries live until explicitly deleted.
Example ¶
package main
import (
"fmt"
"time"
cache "github.com/vikash-paf/go-cache"
)
func main() {
c := cache.New[string, int](cache.WithDefaultTTL[string, int](time.Minute))
defer c.Close()
c.Set("visits", 1)
v, ok := c.Get("visits")
fmt.Println(v, ok)
}
Output: 1 true
type Option ¶
type Option[K comparable, V any] func(*config[K, V])
Option configures a Cache at construction time.
func WithCapacity ¶
func WithCapacity[K comparable, V any](perShard int) Option[K, V]
WithCapacity bounds each shard to at most n entries, evicting via the configured eviction Policy once full. Capacity is enforced per shard rather than globally so a hot shard can never block on a global counter; with N shards, total capacity is approximately n*N. A value of 0 (the default) means unbounded.
func WithDefaultTTL ¶
func WithDefaultTTL[K comparable, V any](ttl time.Duration) Option[K, V]
WithDefaultTTL sets the time-to-live applied to entries written with Set when no per-item TTL is given via WithTTL. Zero (the default) means entries never expire unless given an explicit TTL.
func WithEvictionPolicy ¶
func WithEvictionPolicy[K comparable, V any](f eviction.Factory[K]) Option[K, V]
WithEvictionPolicy sets the eviction.Factory used to build a fresh eviction.Policy for every shard. Only meaningful together with WithCapacity. Defaults to eviction.NewLRU when a capacity is set but no policy is chosen.
func WithJanitorInterval ¶
func WithJanitorInterval[K comparable, V any](d time.Duration) Option[K, V]
WithJanitorInterval sets how often each shard sweeps for expired entries in the background. Expired entries are also skipped lazily on Get regardless of this setting, so the janitor only affects how quickly memory for expired-but-unread entries is reclaimed. A value <= 0 disables the background sweep entirely. Default: 1 second.
func WithOnEvict ¶
func WithOnEvict[K comparable, V any](fn func(key K, value V)) Option[K, V]
WithOnEvict registers a callback invoked whenever an entry is removed due to capacity eviction or TTL expiry (not on explicit Delete). The callback runs synchronously on the goroutine that triggered the eviction, so it must be fast and must not call back into the same Cache.
func WithShards ¶
func WithShards[K comparable, V any](n int) Option[K, V]
WithShards sets the number of internal shards used to partition the keyspace. More shards reduce lock contention under concurrent access at the cost of slightly higher memory overhead and less precise global capacity accounting. Must be a positive number; non-positive values are ignored. Default: 32.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package eviction defines the pluggable eviction strategy used by a cache shard once it reaches capacity, plus a set of ready-to-use policies (LRU, LFU, FIFO).
|
Package eviction defines the pluggable eviction strategy used by a cache shard once it reaches capacity, plus a set of ready-to-use policies (LRU, LFU, FIFO). |