Git Product home page Git Product logo

tenure-go's Introduction

tenure-go

Tenure-go is a thread-safe LRU cache instance that uses hashmap lookups and an Open Doubly Linked List to enact the Least-Recently Used algorithm.

Tenure-go's internal cache utilizes the Go's sync/mutex locking mechanism to ensure thread-safety.

Install

go get -u github.com/MatthewZito/tenure-go
import (
	tenure "github.com/MatthewZito/tenure-go"
)

lru, err := New(10, cb)

lru.Put("key", "value")

--

Usage

type Callback

type Callback func(key interface{}, value interface{})

type LRUCache

type LRUCache struct {
}

func New

func New(bufCap int, onItemEvicted Callback) (*LRUCache, error)

New initializes a new LRU cache with a buffer capacity of bufCap It accepts as a second parameter a callback to be invoked upon successful invocation of the Least Recently-Used cache policy i.e. when a key/value pair is removed All transactions utilize locks and are therefore thread-safe

func (*LRUCache) AdjustCapacity

func (lc *LRUCache) AdjustCapacity(bufCap int) (numEvicted int)

AdjustCapacity resizes the cache capacity Invoking this transaction will evict all least recently-used items to adjust the cache, where necessary

func (*LRUCache) Capacity

func (lc *LRUCache) Capacity() int

Capacity returns the current maximum buffer capacity of the cache

func (*LRUCache) Del

func (lc *LRUCache) Del(key interface{}) (wasDeleted bool)

Del deletes an item corresponding to a given key from the cache, if extant A boolean flag is returned, indicating whether of not the transaction occurred

func (*LRUCache) Drop

func (lc *LRUCache) Drop()

Drop drops all items from the cache

func (*LRUCache) Get

func (lc *LRUCache) Get(key interface{}) (value interface{}, ok bool)

Get attempts to retrieve the value for the given key from the cache Returns the corresponding value and true if extant; else, returns nil, false Get transactions will move the item to the head of the cache, designating it as most recently-used

func (*LRUCache) Has

func (lc *LRUCache) Has(key interface{}) (ok bool)

Has returns a boolean flag verifying the existence (or lack thereof) of a given key in the cache without enacting the eviction policy

func (*LRUCache) Keys

func (lc *LRUCache) Keys() []interface{}

Keys returns a slice of the keys currently extant in the cache

func (*LRUCache) LeastRecentlyUsed

func (lc *LRUCache) LeastRecentlyUsed() (key interface{}, value interface{})

LeastRecentlyUsed returns the least recently-used key / value pair, or nil if not extant

func (*LRUCache) Put

func (lc *LRUCache) Put(key, value interface{}) (wasEvicted bool)

Put adds or inserts a given key / value pair into the cache Put transactions will move the key to the head of the cache, designating it as 'most recently-used' If the cache has reached the specified capacity, Put transactions will also enact the eviction policy thereby removing the least recently-used item Returns a boolean flag indicating whether an eviction occurred

func (*LRUCache) Size

func (lc *LRUCache) Size() int

Size returns the current size of the cache

type LRUController

type LRUController interface {
	Get(key interface{}) (value interface{}, ok bool)
	Put(key, value interface{}) (wasEvicted bool)
	Del(key interface{}) (wasDeleted bool)
	Keys() []interface{}
	Peek(key interface{}) (value interface{})
	Has(key interface{}) (ok bool)
	Purge()
	Size() int
	AdjustCapacity(bufCap int) (numEvicted int)
}

tenure-go's People

Contributors

exbotanical avatar

Watchers

 avatar

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.