lru

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2022 License: BSD-2-Clause Imports: 3 Imported by: 0

README

go-lru

A generic, thread-safe LRU cache library, implemented via a generic doubly linked list (also available separately in package "github.com/binjamil/go-lru/list")

Documentation

For complete docs, check out Go Packages

Installation

go get github.com/binjamil/go-lru

Usage

import "github.com/binjamil/go-lru"

func Consumer() {
    lru, err := lru.New[int, string](69)
    if err != nil {
        panic(err)
    }

    for i := 0; i < 69; i++ {
        lru.Add(i, "Lalo Salamanca") // Type-safe Add
    }

    var val string
    val, ok := lru.Get(0) // Type-safe Get
    if ok {
        println(val)
    }
}

Documentation

Overview

Package lru provides a fixed size LRU cache

- O(1) Add, Get, Contains and Remove - Generic implementation for better type-safety - Thread-safe operations via mutexes

LRU is implemented via a generic doubly linked list, available in package "github.com/binjamil/go-lru/list"

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LRU

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

LRU is a thread-safe fixed size LRU cache.

func New

func New[K comparable, V any](capacity uint) (*LRU[K, V], error)

New creates an LRU cache with the given capacity.

func (*LRU[K, V]) Add

func (c *LRU[K, V]) Add(key K, val V) (evicted bool)

Add adds a value to the cache and returns true if an eviction occurred.

func (*LRU[K, V]) Contains

func (c *LRU[K, V]) Contains(key K) bool

Contains checks if the specified key is present in the LRU without updating the "recently used"-ness of the key.

func (*LRU[K, V]) Get

func (c *LRU[K, V]) Get(key K) (val V, ok bool)

Get looks up a key's value and returns (value, true) if it exists. If the value doesn't exist, it returns (nil, false).

func (*LRU[K, V]) Len

func (c *LRU[K, V]) Len() uint

Len returns the length of the LRU.

func (*LRU[K, V]) Remove

func (c *LRU[K, V]) Remove(key K) (present bool)

Remove removes the specified key and returns if the key was present.

Directories

Path Synopsis
Package list implements a generic doubly linked list.
Package list implements a generic doubly linked list.

Jump to

Keyboard shortcuts

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