lrucache

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2024 License: MIT Imports: 4 Imported by: 4

Documentation

Overview

Package lrucache provides in-memory LRU cache with expiration mechanism and collecting Prometheus metrics.

Example
const metricsNamespace = "myservice"

// There are 2 types of entries will be stored in cache: users and posts.
const (
	cacheEntryTypeUser EntryType = iota
	cacheEntryTypePost
)

type User struct {
	ID   int
	Name string
}

type Post struct {
	ID    int
	Title string
}

// Make, configure and register Prometheus metrics collector.
metricsCollector := NewMetricsCollector(metricsNamespace)
metricsCollector.SetupEntryTypeLabels(map[EntryType]string{
	cacheEntryTypeUser: "user",
	cacheEntryTypePost: "post",
})
metricsCollector.MustRegister()

// Make LRU cache for storing maximum 1000 entries
cache, err := New[string](1000, metricsCollector)
if err != nil {
	log.Fatal(err)
}

// Add entries to cache.
cache.Add("user:1", User{1, "John"}, cacheEntryTypeUser)
cache.Add("post:1", Post{1, "My first post."}, cacheEntryTypePost)

// Get entries from cache.
if val, found := cache.Get("user:1", cacheEntryTypeUser); found {
	user := val.(User)
	fmt.Printf("%d, %s\n", user.ID, user.Name)
}
if val, found := cache.Get("post:1", cacheEntryTypePost); found {
	post := val.(Post)
	fmt.Printf("%d, %s\n", post.ID, post.Title)
}
Output:

1, John
1, My first post.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EntryType

type EntryType int

EntryType is a type of storing in cache entries.

const EntryTypeDefault EntryType = 0

EntryTypeDefault is a default entry type.

type LRUCache

type LRUCache[K comparable] struct {
	MetricsCollector *MetricsCollector
	// contains filtered or unexported fields
}

LRUCache represents an LRU cache with eviction mechanism and Prometheus metrics.

func New

func New[K comparable](maxEntries int, metricsCollector *MetricsCollector) (*LRUCache[K], error)

New creates a new LRUCache with the provided maximum number of entries.

func (*LRUCache[K]) Add

func (c *LRUCache[K]) Add(key K, value interface{}, entryType EntryType)

Add adds a value to the cache with the provided key and type. If the cache is full, the oldest entry will be removed.

func (*LRUCache[K]) Get

func (c *LRUCache[K]) Get(key K, entryType EntryType) (value interface{}, ok bool)

Get returns a value from the cache by the provided key and type.

func (*LRUCache[K]) Len

func (c *LRUCache[K]) Len() int

Len returns the number of items in the cache.

func (*LRUCache[K]) Purge

func (c *LRUCache[K]) Purge()

Purge clears the cache.

func (*LRUCache[K]) Remove

func (c *LRUCache[K]) Remove(key K, entryType EntryType) bool

Remove removes a value from the cache by the provided key and type.

func (*LRUCache[K]) Resize

func (c *LRUCache[K]) Resize(size int)

Resize changes the cache size. Note that resizing the cache may cause some entries to be evicted.

type MetricsCollector

type MetricsCollector struct {
	EntriesAmount  *prometheus.GaugeVec
	HitsTotal      *prometheus.CounterVec
	MissesTotal    *prometheus.CounterVec
	EvictionsTotal *prometheus.CounterVec
	// contains filtered or unexported fields
}

MetricsCollector represents collector of metrics for analyze how (effectively or not) cache is used.

func NewMetricsCollector

func NewMetricsCollector(namespace string) *MetricsCollector

NewMetricsCollector creates a new metrics collector.

func (*MetricsCollector) MustRegister

func (c *MetricsCollector) MustRegister()

MustRegister does registration of metrics collector in Prometheus and panics if any error occurs.

func (*MetricsCollector) SetupEntryTypeLabels

func (c *MetricsCollector) SetupEntryTypeLabels(labels map[EntryType]string)

SetupEntryTypeLabels setups its own Prometheus metrics label for each storing cacheEntry type.

func (*MetricsCollector) Unregister

func (c *MetricsCollector) Unregister()

Unregister cancels registration of metrics collector in Prometheus.

Jump to

Keyboard shortcuts

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