Documentation ¶
Overview ¶
Package memcache provides an in-memory cache.Cache on top of our simplelru
Package memcache provides an in-memory LRU cache
Index ¶
- Constants
- type Adder
- type AdderGetter
- type Cache
- type Getter
- type LRU
- func (m *LRU[K]) Add(key K, value []byte, expire time.Time) bool
- func (m *LRU[K]) Evict(key K)
- func (m *LRU[K]) EvictExpired(ctx context.Context, period time.Duration) error
- func (m *LRU[K]) Get(key K) ([]byte, *time.Time, bool)
- func (m *LRU[K]) Items() int
- func (m *LRU[K]) Size() int64
- func (m *LRU[K]) Stats() cache.Stats
- type SingleFlight
- type Store
Constants ¶
const ( KiB = 1024 // KiB is 2^10 (kilobyte) MiB = KiB * KiB // MiB is 2^20 (megabyte) GiB = KiB * MiB // GiB is 2^30 (gigabyte) )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Adder ¶
type Adder[K comparable] interface { Add(key K, value []byte, expire time.Time) bool }
Adder represents an interface providing the Add() method of LRU
type AdderGetter ¶
type AdderGetter[K comparable] interface { Getter[K] Adder[K] }
AdderGetter represents an interface providing both Get and Add of LRU
type Cache ¶
type Cache[K comparable] struct { *SingleFlight[K] // contains filtered or unexported fields }
Cache is a LRU with TTL cache.Cache
func NewCache ¶
NewCache creates a new Cache with a maximum size and cache.Getter
type Getter ¶
type Getter[K comparable] interface { Get(key K) ([]byte, *time.Time, bool) }
Getter represents an interface providing the Get() method of LRU
type LRU ¶
type LRU[K comparable] struct { // contains filtered or unexported fields }
LRU is a least-recently-used cache of bytes with TTL and maximum size
func NewLRU ¶
func NewLRU[K comparable](cacheBytes int64, onSet func(K, []byte, int64, *time.Time), onEvict func(K, []byte, int64)) *LRU[K]
NewLRU creates a new []byte LRU with maximum size and eviction
func (*LRU[K]) Add ¶
Add adds an entry and cache duration, and returns true if entries were removed to free capacity. if expire is 0, it never expires.
func (*LRU[K]) EvictExpired ¶
EvictExpired periodically scans for expired entries and evicts them from the cache. It runs until the provided context is cancelled.
func (*LRU[K]) Get ¶
Get attempts to find an entry in the cache, and returns its value, expiration date if any, and if it was found or not
type SingleFlight ¶
type SingleFlight[K comparable] struct { // contains filtered or unexported fields }
SingleFlight is a man-in-the-middle between cache.Cache and our LRU to prevent stampedes
func NewSingleFlight ¶
func NewSingleFlight[K comparable](name string, inward AdderGetter[K], outward cache.Getter[K]) *SingleFlight[K]
NewSingleFlight creates a new SingleFlight controller, with an LRU for local cache and a cache.Getter to acquire the data externally. SingleFlight will prevent multiple requests for the same key to reach out at the same time.
func (*SingleFlight[K]) Get ¶
Get attempts to get the value of a key from its internal cache, otherwise reaches out to the provided cache.Getter, but only once. While this is in process any other request for the same key will be held until we have a response from from the first.
func (*SingleFlight[K]) Name ¶
func (sf *SingleFlight[K]) Name() string
Name returns the name of the Cache namespace
func (*SingleFlight[K]) Set ¶
func (sf *SingleFlight[K]) Set(_ context.Context, key K, value []byte, expire time.Time, _ cache.Type) error
Set stores the value for a key inward, and shares it with anyway waiting for it
func (*SingleFlight[K]) SetLogger ¶
func (sf *SingleFlight[K]) SetLogger(log slog.Logger)
SetLogger attaches a slog.Logger to this SingleFlight quasi-cache.Cache