arc

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2023 License: MIT Imports: 4 Imported by: 0

README

generic Adaptive Replacement Cache (ARC)

usage

import (
	"fmt"

	arc "github.com/larvan2/generic-arc"
)

func main() {
	const (
		k1 = "Hello"
		v1 = "World"
	)
	cache := arc.New[string, string](arc.WithSize[string, string](3))

	// Insert the first value
	cache.Set(k1, v1)

	fmt.Println(cache.Get(k1))
}


arc

An Adaptive Replacement Cache (ARC) written in Go.

GoDoc

This project implements "ARC", a self-tuning, low overhead replacement cache. The goal of this project is to expose an interface compareable to common LRU cache management systems. ARC uses a learning rule to adaptively and continually revise its assumptions about the workload in order to adjust the internal LRU and LFU cache sizes.

This implementation is based on Nimrod Megiddo and Dharmendra S. Modha's "ARC: A SELF-TUNING, LOW OVERHEAD REPLACEMENT CACHE", while definitely useable and thread safe, this is still an experiment and shouldn't be considered production-ready.

<------- cache size c ------>
+-----------------+----------
| LFU             | LRU     |
+-----------------+----------
                  ^
                  |
                  p (dynamically adjusted by learning rule)

B1 [...]
B2 [...]

The cache is implemented using two internal caching systems L1 and L2. The cache size c defines the maximum number of entries stored (excluding ghost entries). Ghost entries are being stored in two "ghost registries" B1 and B1. Ghost entries no longer have a value associated with them.

Ghost entries are being used in order to keep track of expelled pages. They no longer have a value associated with them, but can be promoted into the internal LRU cache.

Frequently requested pages are being promoted into the LFU.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ARC

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

func New

func New[K comparable, V any](options ...Option[K, V]) *ARC[K, V]

New returns a new Adaptive Replacement Cache (ARC).

func (*ARC[K, V]) Get

func (a *ARC[K, V]) Get(key K) (value V, ok bool)

Get retrieves a previously via Set inserted entry. This optimizes future access to this entry (side effect).

func (*ARC[K, V]) GetWithExpire

func (a *ARC[K, V]) GetWithExpire(key K) (V, time.Time, bool)

GetWithExpire returns any representation of a cached response, a time.Time Give expected expires, and a bool set to true if the key was found. This method will NOT update the expires.

func (*ARC[K, V]) Len

func (a *ARC[K, V]) Len() int

Len determines the number of currently cached entries. This method is side-effect free in the sense that it does not attempt to optimize random cache access.

func (*ARC[K, V]) Set

func (a *ARC[K, V]) Set(key K, value V)

Set inserts a new key-value pair into the cache. This optimizes future access to this entry (side effect).

func (*ARC[K, V]) SetWithExpire

func (a *ARC[K, V]) SetWithExpire(key K, value V, expires time.Time)

SetWithExpire stores any representation of a response for a given key and given expires. The expires time will round to second.

type Option

type Option[K comparable, V any] func(*ARC[K, V])

Option is part of Functional Options Pattern

func WithSize

func WithSize[K comparable, V any](maxSize int) Option[K, V]

Jump to

Keyboard shortcuts

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