GoLRU

package module
v0.0.0-...-00bfe4c Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2020 License: GPL-3.0 Imports: 4 Imported by: 0

README

GoLRU

A basic LRU cache written in Go

package main

func main() {
 LRU := GoLRU.New(1000)   // empty (keys are of type int)
 LRU.Put(2, "b")          // 2->b
 LRU.Put(1, "x")          // 2->b, 1->x (insertion-order)
 LRU.Put(1, "a")          // 2->b, 1->a (insertion-order)
 LRU.Oldest()             // 2
 LRU.Newest()             // 1
 LRU.SetMaxSize(2000)     // MaxSize <- 2000
 LRU.GetMaxSize()         // 2000
 _, _ = LRU.Get(2)        // b, true
 _, _ = LRU.Get(3)        // nil, false
 _ = LRU.Values()         // []interface {}{"b", "a"} (insertion-order)
 _ = LRU.Keys()           // []interface {}{2, 1} (insertion-order)
 LRU.Remove(1)            // 2->b
 LRU.Clear()              // empty
 LRU.Empty()              // true
 LRU.Size()               // 0
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LRU

type LRU struct {
	// contains filtered or unexported fields
}

func New

func New(s int) *LRU

func (*LRU) Clear

func (lm *LRU) Clear()

Clear removes all elements from the map.

func (*LRU) Empty

func (lm *LRU) Empty() bool

Empty returns true if map does not contain any elements

func (*LRU) Get

func (lm *LRU) Get(key interface{}) (value interface{}, found bool)

Get searches the element in the map by key and returns its value or nil if key is not found in tree. Second return parameter is true if key was found, otherwise false. Key should adhere to the comparator's type assertion, otherwise method panics.

func (*LRU) GetMaxSize

func (lm *LRU) GetMaxSize() int

Get the max size

func (*LRU) Iterator

func (lm *LRU) Iterator() linkedhashmap.Iterator

Return Iterator

func (*LRU) Keys

func (lm *LRU) Keys() []interface{}

Keys returns all keys in-order

func (*LRU) Newest

func (lm *LRU) Newest() (key interface{}, found bool)

Get newest key

func (*LRU) Oldest

func (lm *LRU) Oldest() (key interface{}, found bool)

Get oldest key

func (*LRU) Put

func (lm *LRU) Put(key interface{}, value interface{})

Put inserts key-value pair into the map. Key should adhere to the comparator's type assertion, otherwise method panics.

func (*LRU) Remove

func (lm *LRU) Remove(key interface{})

Remove removes the element from the map by key. Key should adhere to the comparator's type assertion, otherwise method panics.

func (*LRU) SetMaxSize

func (lm *LRU) SetMaxSize(s int)

Set the new max size

func (*LRU) Size

func (lm *LRU) Size() int

Size returns number of elements in the map.

func (*LRU) String

func (lm *LRU) String() string

String returns a string representation of container

func (*LRU) Values

func (lm *LRU) Values() []interface{}

Values returns all values in-order based on the key.

Jump to

Keyboard shortcuts

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