memcache

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2024 License: MIT Imports: 7 Imported by: 0

README

In-memory cache with TTL and restricted size

Go Reference

LRU

This package provides a thread-safe []byte TTL with maximum size control based on our simplelru.

This LRU does NOT implement the Cache interface.

See also

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

View Source
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

func NewCache[K comparable](name string, cacheBytes int64, getter cache.Getter[K]) *Cache[K]

NewCache creates a new Cache with a maximum size and cache.Getter

func (*Cache[K]) Remove

func (g *Cache[K]) Remove(_ context.Context, key K)

Remove evicts an entry from the Cache

func (*Cache[K]) Stats

func (g *Cache[K]) Stats(_ cache.Type) cache.Stats

Stats returns statistics about the Cache. This implementation doesn't distinguish among types

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

func (m *LRU[K]) Add(key K, value []byte, expire time.Time) bool

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]) Evict

func (m *LRU[K]) Evict(key K)

Evict removes an entry if present

func (*LRU[K]) EvictExpired

func (m *LRU[K]) EvictExpired(ctx context.Context, period time.Duration) error

EvictExpired periodically scans for expired entries and evicts them from the cache. It runs until the provided context is cancelled.

func (*LRU[K]) Get

func (m *LRU[K]) Get(key K) ([]byte, *time.Time, bool)

Get attempts to find an entry in the cache, and returns its value, expiration date if any, and if it was found or not

func (*LRU[K]) Items

func (m *LRU[K]) Items() int

Items returns the number of entries in the cache

func (*LRU[K]) Size

func (m *LRU[K]) Size() int64

Size returns the added size if bytes of all entries in the cache

func (*LRU[K]) Stats

func (m *LRU[K]) Stats() cache.Stats

Stats returns statistics about the LRU

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

func (sf *SingleFlight[K]) Get(ctx context.Context, key K, dest cache.Sink) error

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

type Store

type Store[K comparable] struct {
	// contains filtered or unexported fields
}

Store manages in-memory [cache.Cache]s

func New

func New[K comparable]() *Store[K]

New creates a new Store

func (*Store[K]) DeregisterCache

func (s *Store[K]) DeregisterCache(name string)

DeregisterCache disconnects a Cache from the Store

func (*Store[K]) GetCache

func (s *Store[K]) GetCache(name string) cache.Cache[K]

GetCache finds a Cache by name in the Store

func (*Store[K]) NewCache

func (s *Store[K]) NewCache(name string, cacheBytes int64, getter cache.Getter[K]) cache.Cache[K]

NewCache creates a new in-memory Cache attached to the Store

func (*Store[K]) SetLogger

func (s *Store[K]) SetLogger(log slog.Logger)

SetLogger attaches a slog.Logger to the store and any new Cache created through it

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL