Documentation
¶
Overview ¶
Package dlru implements an LRU cache that is distributed among the peers of a microservice.
Index ¶
- type Cache
- func (c *Cache) Clear(ctx context.Context) error
- func (c *Cache) Close(ctx context.Context) error
- func (c *Cache) Delete(ctx context.Context, key string) error
- func (c *Cache) DeleteContains(ctx context.Context, keySubstring string) error
- func (c *Cache) DeletePrefix(ctx context.Context, keyPrefix string) error
- func (c *Cache) Get(ctx context.Context, key string, value any, options ...LoadOption) (found bool, err error)
- func (c *Cache) Has(ctx context.Context, key string, options ...LoadOption) (found bool, err error)
- func (c *Cache) Hits() int
- func (c *Cache) Len(ctx context.Context) (int, error)
- func (c *Cache) Load(ctx context.Context, key string, options ...LoadOption) (value []byte, ok bool, err error)
- func (c *Cache) LocalCache() *lru.Cache[string, []byte]
- func (c *Cache) MaxAge() time.Duration
- func (c *Cache) MaxMemory() int
- func (c *Cache) Misses() int
- func (c *Cache) Peek(ctx context.Context, key string, value any, options ...LoadOption) (found bool, err error)
- func (c *Cache) Set(ctx context.Context, key string, value any, options ...StoreOption) (err error)
- func (c *Cache) SetMaxAge(ttl time.Duration) error
- func (c *Cache) SetMaxMemory(bytes int) error
- func (c *Cache) SetMaxMemoryMB(megaBytes int) error
- func (c *Cache) Store(ctx context.Context, key string, value []byte, options ...StoreOption) error
- func (c *Cache) Weight(ctx context.Context) (int, error)
- type LoadOption
- type Service
- type StoreOption
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 ¶
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) DeleteContains ¶
DeleteContains all element from the cache whose keys contain the given substring.
func (*Cache) DeletePrefix ¶
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
Has checks if an element is in the cache, without marking it as the most recently used.
func (*Cache) Hits ¶
Hits returns the total number of cache hits. This number can technically overflow.
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 ¶
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 ¶
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 ¶
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 ¶
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
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 ¶
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 ¶
SetMaxMemory limits the memory used by the cache.
func (*Cache) SetMaxMemoryMB ¶
SetMaxMemoryMB limits the memory used by 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 ¶
type Service interface {
service.PublisherSubscriber
service.Identifier
service.Logger
service.Meter
}
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.