memcache

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 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 struct {
	*SingleFlight
	// contains filtered or unexported fields
}

Cache is a LRU with TTL cache.Cache

func NewCache

func NewCache(name string, cacheBytes int64, getter cache.Getter) *Cache

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

func (*Cache) Remove

func (g *Cache) Remove(_ context.Context, key string)

Remove evicts an entry from the Cache

func (*Cache) Stats

func (g *Cache) Stats(_ cache.Type) cache.Stats

Stats returns statistics about the Cache. This implementation doesn't distinuish 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 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(name string, inward AdderGetter[string], outward cache.Getter) *SingleFlight

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) Get

func (sf *SingleFlight) Get(ctx context.Context, key string, 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) Name

func (sf *SingleFlight) Name() string

Name returns the name of the Cache namespace

func (*SingleFlight) Set

func (sf *SingleFlight) Set(_ context.Context, key string, 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) SetLogger

func (sf *SingleFlight) SetLogger(log slog.Logger)

SetLogger attaches a slog.Logger to this SingleFlight quasi-cache.Cache

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store manages in-memory [cache.Cache]s

func New

func New() *Store

New creates a new Store

func (*Store) DeregisterCache

func (s *Store) DeregisterCache(name string)

DeregisterCache disconnects a Cache from the Store

func (*Store) GetCache

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

GetCache finds a Cache by name in the Store

func (*Store) NewCache

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

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

func (*Store) SetLogger

func (s *Store) 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