cache

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2025 License: Unlicense Imports: 4 Imported by: 0

Documentation

Overview

Package cache implemented some basic in memory caching strategies.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LRU

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

A cache using the Least Recently Used eviction policy.

func MustNewLRU

func MustNewLRU[K comparable, T any](cap int) *LRU[K, T]

Like NewLRU but will panic on error.

func NewLRU

func NewLRU[K comparable, T any](cap int) (*LRU[K, T], error)

NewLRU creates a new cache with LRU eviction policy. It accepts cap as the only argument, specifying the maximum capacity of the cache. It will return an error if cap is less than 1.

func (*LRU[K, T]) Get

func (c *LRU[K, T]) Get(key K) (T, error)

Get the value associated with the given key argument. Get will return collection.ErrNotFound if there is no such key, or collection.ErrIsEmpty if the cache is empty.

func (*LRU[K, T]) Put

func (c *LRU[K, T]) Put(key K, value T)

Put a new value with an associated key into the cache. This will update the value if the key already exist.

type MRU

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

A cache using the Most Recently Used eviction policy.

func MustNewMRU

func MustNewMRU[K comparable, T any](cap int) *MRU[K, T]

Like NewMRU but will panic on error.

func NewMRU

func NewMRU[K comparable, T any](cap int) (*MRU[K, T], error)

NewLRU creates a new cache with LRU eviction policy. It accepts cap as the only argument, specifying the maximum capacity of the cache. Return an error if cap is less than 1.

func (*MRU[K, T]) Get

func (c *MRU[K, T]) Get(key K) (T, error)

Get the value associated with the given key argument. If there is no such key returns collection.ErrNotFound, or if the cache is empty then returns collection.ErrIsEmpty. This marks the key as recently used.

func (*MRU[K, T]) Put

func (c *MRU[K, T]) Put(key K, value T)

Put a new value with an associated key into the cache. Update the value if the key already exist. This marks the key as recently used.

Jump to

Keyboard shortcuts

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