lru

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2021 License: MIT Imports: 2 Imported by: 0

README

go-hashlru

A simple thread-safe, fixed size LRU written in Go. Based on dominictarr's Hashlru Algorithm. 🔃

Uses map[interface{}]interface{} to allow kv pairs of any type.

Visit example/example.go in the root directory for a simple example.


Install

$ go get github.com/saurabh0719/go-hashlru

API Reference

HashLRU Struct :

type HashLRU struct {
	maxSize  int
	size     int
	oldCache map[interface{}]interface{}
	newCache map[interface{}]interface{}
	lock     sync.RWMutex
}
1. func NewHLRU
func NewHLRU(maxSize int) (*HashLRU, error)

Returns a new instance of HashLRU of the given size: maxSize.

2. func (lru *HashLRU) Set
func (lru *HashLRU) Set(key, value interface{})

Adds a new key-value pair to the cache.

3. func (lru *HashLRU) Get
func (lru *HashLRU) Get(key interface{}) (interface{}, bool)

Get the value of any key. Returns value, true if the kv pair is found, else returns nil, false.

4. func (lru *HashLRU) Has
func (lru *HashLRU) Has(key interface{}) bool

Returns true if the key exists, else returns false.

5. func (lru *HashLRU) Remove
func (lru *HashLRU) Remove(key interface{}) (interface{}, bool)

Deletes the key-value pair and returns deletedValue, true if it exists, else returns nil, false.

6. func (lru *HashLRU) Len
func (lru *HashLRU) Len() int

Returns the total number of key-value pairs present in the cache.

7. func (lru *HashLRU) Clear
func (lru *HashLRU) Clear()

Empties the Cache.

8. func (lru *HashLRU) Resize
func (lru *HashLRU) Resize(newSize int) (int, error)

Update the maxSize of the cache. Items will be evicted automatically to adjust. Returns the number of evicted key-value pairs due to the re-size.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HashLRU

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

func NewHLRU

func NewHLRU(maxSize int) (*HashLRU, error)

Returns a new hashlru instance

func (*HashLRU) Clear

func (lru *HashLRU) Clear()

Clears all entries.

func (*HashLRU) Get

func (lru *HashLRU) Get(key interface{}) (interface{}, bool)

func (*HashLRU) Has

func (lru *HashLRU) Has(key interface{}) bool

Checks if a key exists in cache

func (*HashLRU) Len

func (lru *HashLRU) Len() int

Returns the number of items in the cache.

func (*HashLRU) Remove

func (lru *HashLRU) Remove(key interface{}) (interface{}, bool)

Removes a key from the cache

func (*HashLRU) Resize

func (lru *HashLRU) Resize(newSize int) (int, error)

Resizes cache, returning number of items deleted

func (*HashLRU) Set

func (lru *HashLRU) Set(key, value interface{})

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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