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 ¶
- type EntryType
- type LRUCache
- func (c *LRUCache[K]) Add(key K, value interface{}, entryType EntryType)
- func (c *LRUCache[K]) Get(key K, entryType EntryType) (value interface{}, ok bool)
- func (c *LRUCache[K]) Len() int
- func (c *LRUCache[K]) Purge()
- func (c *LRUCache[K]) Remove(key K, entryType EntryType) bool
- func (c *LRUCache[K]) Resize(size int)
- type MetricsCollector
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 ¶
Add adds a value to the cache with the provided key and type. If the cache is full, the oldest entry will be removed.
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.