GithubHelp home page GithubHelp logo

go-lru's Introduction

lru

-- import "."

Package lru contains an implementation for LRU caching with items of arbitrary sizes. Most existing LRU cache implementation have a maximum item size, while this specific version defines a maximum cache size with items of arbitrary dimensions. Usefull for storing blob data (files, text etc.) with a maximum memory usage.

Usage

type LRUCache

type LRUCache struct {
}

LRU cache used for storing items of arbitrary size.

func New

func New(maxSize int, producer func(key interface{}) *LRUItem, onevict func(interface{}, *LRUItem)) (*LRUCache, error)

New Creates a new LRW cache @param {maxSize} - the maximum size of this cache @param {producer} - optional callback function which gets invoked on cache misses. The

result of this callback is stored in the cache for the key which was missing.
Can be null, in which case all cache misses will return null

@param {onevict} - optional function which will get called when an item is evicted from the cache

func (*LRUCache) EmptySpace

func (cache *LRUCache) EmptySpace() int

EmptySpace returns the remaining empty space within the cache.

func (*LRUCache) Evict

func (cache *LRUCache) Evict(key interface{}) *LRUItem

Evict removes an element from the cache. @param {interface{}} key - the key of the element to be removed @return {*LRUItem} - if an element with the specified key is found and removed, then the return value is the deleted element. Otherwise, nil is returned.

func (*LRUCache) EvictIf

func (cache *LRUCache) EvictIf(predicate func(interface{}) bool) *LRUItem

Evict removes an element from the cache. @param {interface{}} key - the key of the element to be removed @return {*LRUItem} - if an element with the specified key is found and removed, then the return value is the deleted element. Otherwise, nil is returned.

func (*LRUCache) Get

func (cache *LRUCache) Get(key interface{}) *LRUItem

Get retrieves an element from the cache. In case of cache miss and a producer is defined, then the producer is invoked and the result is stored in the cache. If the producer is null, no result is returned (nil return value)

@param key - the key to search in the cache @return - if found, returns the corresponding cached element. If not found, the result

can either be null if the producer was not given, or the retult of the producer

func (*LRUCache) Has

func (cache *LRUCache) Has(key interface{}) bool

Has checks if an element with the specified key exists within the cache @param {interface{}} key - the key of the element @return {bool} - if an item with the specified key exists

func (*LRUCache) MakeRoom

func (cache *LRUCache) MakeRoom(size int)

MakeRoom evicts elements from the cache until the specified empty space is made. Ff the cache already has enough empty space, then no elements are evicted. @param {int} size - how much empty space should be ensured

func (*LRUCache) MaxSize

func (cache *LRUCache) MaxSize() int

MaxSize returns the maximum size this cache can hold.

func (*LRUCache) Put

func (cache *LRUCache) Put(key interface{}, element *LRUItem) error

Put adds a new element to the cache. If an item with the same key already exists, then the operation fails and an error is returned. @param {interface{}} key - the key for the new cached item @param {*LRUItem} element - the element to be inserted

func (*LRUCache) Replace

func (cache *LRUCache) Replace(key interface{}, element *LRUItem) error

Replace adds a new element to the cache. If an item with the same key already exists, then it is evicted and replaced with the given element. @param {interface{}} key - the key for the new cached item @param {*LRUItem} element - the element to be inserted

type LRUItem

type LRUItem struct {
	// Actual value stored for this specific item
	Value interface{}
	// The size for this item
	Size int
}

Single cache item

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.