cache

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2025 License: Apache-2.0, Apache-2.0 Imports: 5 Imported by: 2

README

ttlcache Go Reference

Cache library with configurable eviction policy, supported LFU and LRU.

Install

$ go get github.com/moeryomenko/ttlcache

License

ttlcache is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE and/or LICENSE-MIT for details.

Documentation

Index

Constants

View Source
const (
	// Discards the least recently used items first.
	LRU evictionPolicy = iota
	// Discards the least frequently used items first.
	LFU
	// Adaptive replacement cache policy.
	ARC
	// Noop cache without replacement policy.
	NOOP
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Cache is cache with TTL and eviction over capacity.

func NewCache

func NewCache[K comparable, V any](ctx context.Context, capacity int, opts ...Option) *Cache[K, V]

NewCache returns cache with selected eviction policy.

func (*Cache[K, V]) Get

func (c *Cache[K, V]) Get(key K) (V, bool)

Get returns value by given key.

func (*Cache[K, V]) Len added in v1.0.0

func (c *Cache[K, V]) Len() int

Len returns current size of cache.

func (*Cache[K, V]) Remove added in v0.1.6

func (c *Cache[K, V]) Remove(key K)

Remove removes cache entry by given key.

func (*Cache[K, V]) Set

func (c *Cache[K, V]) Set(key K, value V)

Set sets new or updates key-value pair to cache, which can be evicted only by policy.

func (*Cache[K, V]) SetNX added in v1.0.0

func (c *Cache[K, V]) SetNX(key K, value V, expiry time.Duration)

SetNX sets new or updates key-value pair with given expiration time.

type Option added in v1.0.0

type Option func(*config)

Option is an option that can be applied to cache.

func WithEvictionPolicy added in v1.0.0

func WithEvictionPolicy(policy evictionPolicy) Option

WithEvictionPolicy sets eviction policy for cache.

func WithTTLEpochGranularity added in v1.0.0

func WithTTLEpochGranularity(period time.Duration) Option

WithTTLEpochGranularity sets ttl epoch granularity.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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