lru

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2022 License: MIT Imports: 2 Imported by: 1

README

LRU Cache

A simple LRU cache using go generics.

Examples

Basic usage.

func main() {
    cache := lru.New[string, string]()
    cache.Set("key", "value")
    value, _ := cache.Get("key")
    fmt.Println(value)
}

Set the capacity using the lru.WithCapacity option. The default capacity is set to 10000.

func main() {
    cache := lru.New[string, string](lru.WithCapacity(100))
    ...
}

A thread safe implementation is included for convenience.

func main() {
    cache := lru.NewSync[string, string](lru.WithCapacity(100))
    ...
}

Documentation

Index

Constants

View Source
const (
	// DefaultCacity is the default cache capacity
	DefaultCapacity = 10000
)

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 a lru cache. It automatically removes elements as new elements are added if the capacity is reached. Items are removes based on how recently they were used where the oldest items are removed first.

func New

func New[K comparable, V any](cacheOptions ...CacheOption) *Cache[K, V]

New initializes a new lru cache with the given capacity.

func (*Cache[K, V]) Delete

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

Delete an item from the cache.

func (*Cache[K, V]) Flush

func (c *Cache[K, V]) Flush()

Flush deletes all items from the cache.

func (*Cache[K, V]) Get

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

Get an item from the cache. This operation updates recent usage of the item.

func (*Cache[K, V]) Len

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

Len is the number of key value pairs in the cache.

func (*Cache[K, V]) Peek

func (c *Cache[K, V]) Peek(key K) (value V, ok bool)

Peek gets an item from the cache without updating the recent usage.

func (*Cache[K, V]) Set

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

Set the given key value pair. This operation updates the recent usage of the item.

type CacheOption

type CacheOption interface {
	// contains filtered or unexported methods
}

CacheOption configurs a lru cache.

func WithCapacity

func WithCapacity(capacity int) CacheOption

WithCapacity configures how many items can be stored before old items begin to be deleted.

type SyncCache

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

SyncCache is a threadsafe lru cache.

func NewSync

func NewSync[K comparable, V any](options ...CacheOption) *SyncCache[K, V]

New initializes a new lru cache with the given capacity.

func (*SyncCache[K, V]) Delete

func (c *SyncCache[K, V]) Delete(key K) bool

Delete an item from the cache.

func (*SyncCache[K, V]) Flush

func (c *SyncCache[K, V]) Flush()

Flush deletes all items from the cache.

func (*SyncCache[K, V]) Get

func (c *SyncCache[K, V]) Get(key K) (value V, ok bool)

Get an item from the cache. This operation updates recent usage of the item.

func (*SyncCache[K, V]) Len

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

Len is the number of key value pairs in the cache.

func (*SyncCache[K, V]) Peek

func (c *SyncCache[K, V]) Peek(key K) (value V, ok bool)

Peek gets an item from the cache without updating the recent usage.

func (*SyncCache[K, V]) Set

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

Set the given key value pair. This operation updates the recent usage of the item.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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