dlru

package
v1.22.2 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2026 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Overview

Package dlru implements an LRU cache that is distributed among the peers of a microservice.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is an LRU cache that is distributed among the peers of a microservice. The cache is tied to the microservice and is typically constructed in the OnStartup callback of the microservice and destroyed in the OnShutdown.

con := connector.New("www.example.com")
var myCache dlru.Cache
con.SetOnStartup(func(ctx context.Context) error {
	myCache = dlru.NewCache(ctx, con, ":444/my-cache")
})
con.SetOnShutdown(func(ctx context.Context) error {
	myCache.Close(ctx)
})

func NewCache

func NewCache(ctx context.Context, svc Service, path string) (*Cache, error)

NewCache starts a new cache for the service at a given path. For security reasons, it is advised to use a non-public port for the path, such as :444/my-cache . By default, the cache size limit is set to 32MB and the TTL to 1 hour.

func (*Cache) Clear

func (c *Cache) Clear(ctx context.Context) error

Clear the cache.

func (*Cache) Close

func (c *Cache) Close(ctx context.Context) error

Close closes and clears the cache.

func (*Cache) Delete

func (c *Cache) Delete(ctx context.Context, key string) error

Delete an element from the cache.

func (*Cache) DeleteContains

func (c *Cache) DeleteContains(ctx context.Context, keySubstring string) error

DeleteContains all element from the cache whose keys contain the given substring.

func (*Cache) DeletePrefix

func (c *Cache) DeletePrefix(ctx context.Context, keyPrefix string) error

DeletePrefix all element from the cache whose keys have the given prefix.

func (*Cache) Get added in v1.18.0

func (c *Cache) Get(ctx context.Context, key string, value any, options ...LoadOption) (found bool, err error)

Get retrieves an element from the cache, marking it as the most recently used. The value must be either a pointer to []byte, string or object that can be unmarshaled from JSON.

func (*Cache) Has added in v1.18.0

func (c *Cache) Has(ctx context.Context, key string, options ...LoadOption) (found bool, err error)

Has checks if an element is in the cache, without marking it as the most recently used.

func (*Cache) Hits

func (c *Cache) Hits() int

Hits returns the total number of cache hits. This number can technically overflow.

func (*Cache) Len

func (c *Cache) Len(ctx context.Context) (int, error)

Len is the total number of elements stored in all the shards of the cache.

func (*Cache) Load

func (c *Cache) Load(ctx context.Context, key string, options ...LoadOption) (value []byte, ok bool, err error)

Load an element from the cache, locally or from peers. If the element is found, it is bumped to the head of the cache.

func (*Cache) LocalCache

func (c *Cache) LocalCache() *lru.Cache[string, []byte]

LocalCache returns the underlying LRU cache that is backing the cache in this peer. Modifying the local cache is unadvisable and may result in inconsistencies. Access is provided mainly for testing purposes.

func (*Cache) MaxAge

func (c *Cache) MaxAge() time.Duration

MaxAge returns the age limit of elements in this cache. Elements that are bumped have their life span reset and will therefore survive longer.

func (*Cache) MaxMemory

func (c *Cache) MaxMemory() int

MaxAge returns the age limit of elements in this cache. Elements that are bumped have their life span reset and will therefore survive longer.

func (*Cache) Misses

func (c *Cache) Misses() int

Misses returns the total number of cache misses. This number can technically overflow.

func (*Cache) Peek added in v1.18.0

func (c *Cache) Peek(ctx context.Context, key string, value any, options ...LoadOption) (found bool, err error)

Peek retrieves an element from the cache, without marking it as the most recently used. The value must be either a pointer to []byte, string or object that can be unmarshaled from JSON.

func (*Cache) Set added in v1.18.0

func (c *Cache) Set(ctx context.Context, key string, value any, options ...StoreOption) (err error)

CacheSet puts an element in the cache, marking it as the most recently used. The value must be either []byte, string or an object that can be marshaled to JSON.

func (*Cache) SetMaxAge

func (c *Cache) SetMaxAge(ttl time.Duration) error

SetMaxAge sets the age limit of elements in this cache. Elements that are bumped have their life span reset and will therefore survive longer.

func (*Cache) SetMaxMemory

func (c *Cache) SetMaxMemory(bytes int) error

SetMaxMemory limits the memory used by the cache.

func (*Cache) SetMaxMemoryMB

func (c *Cache) SetMaxMemoryMB(megaBytes int) error

SetMaxMemoryMB limits the memory used by the cache.

func (*Cache) Store

func (c *Cache) Store(ctx context.Context, key string, value []byte, options ...StoreOption) error

Store an element in the cache.

func (*Cache) Weight

func (c *Cache) Weight(ctx context.Context) (int, error)

Weight is the total memory used by all the shards of the cache.

type LoadOption

type LoadOption func(opts *cacheOptions)

LoadOption customizes loading from the cache.

func Bump

func Bump(bump bool) LoadOption

Bump controls whether a loaded element is bumped to the head of the cache. The default behavior is to bump.

func ConsistencyCheck

func ConsistencyCheck(check bool) LoadOption

ConsistencyCheck controls whether to validate that all peers have the same value. Skipping the consistency check improves performance significantly when the value is available locally. The default behavior is to perform the consistency check.

func MaxAge

func MaxAge(ttl time.Duration) LoadOption

MaxAge indicates to discard elements that have not been inserted or bumped recently.

func NoBump

func NoBump() LoadOption

NoBump prevents a loaded element from being bumped to the head of the cache.

type Service

Service is an interface abstraction of a microservice used by the distributed cache. The connector implements this interface.

type StoreOption

type StoreOption func(opts *cacheOptions)

StoreOption customizes storing in the cache.

func Compress added in v1.18.0

func Compress(compress bool) StoreOption

Compress indicates whether to compress the stored data.

func Replicate

func Replicate(replicate bool) StoreOption

Replicate controls whether to replicate the stored element to all peers. Replication reduces the capacity of the cache but may increase performance when used in conjunction with skipping the consistency check on load. The default behavior is not to replicate.

Jump to

Keyboard shortcuts

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