lru

package module
v0.0.0-...-ec8b77f Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2019 License: MPL-2.0 Imports: 4 Imported by: 2

README

golang-lru

GoDoc FOSSA Status

Note: LRU is the shorthand for Least recently used. Discards the least recently used items first. This algorithm requires keeping track of what was used when, which is expensive if one wants to make sure the algorithm always discards the least recently used item. General implementations of this technique require keeping "age bits" for cache-lines and track the "Least Recently Used" cache-line based on age-bits. In such an implementation, every time a cache-line is used, the age of all other cache-lines changes. LRU is actually a family of caching algorithms with members including 2Q by Theodore Johnson and Dennis Shasha, and LRU/K by Pat O'Neil, Betty O'Neil and Gerhard Weikum.

Thanks for:Advaitjavadekar's Pictrue.

The access sequence for the below example is A B C D E D F. Color

In the above example once A B C D gets installed in the blocks with sequence numbers (Increment 1 for each new Access) and when E is accessed, it is a miss and it needs to be installed in one of the blocks. According to the LRU Algorithm, since A has the lowest Rank(A(0)), E will replace A.

https://en.wikipedia.org/wiki/File:Lruexample.png

  • The lru package which implements a fixed-size.
  • Thread safe LRU cache.
  • Based on the cache in google Groupcache lru.
  • Simple switching between LRU,Cache,TwoQueueCache and ARCCache.

Example

Usage for LRU:

package main

import (
	"fmt"

	lru "github.com/flyaways/golang-lru"
)

var (
	cache lru.LRUCache
	err   error
)

func main() {
	cache, err = lru.New(8)
	/*
		cache, err = NewLRU(8, func(key interface{}, value interface{}) {
			fmt.Println(time.Now().Format(time.RFC3339Nano))
		})
		cache, err = lru.NewWithEvict(8, func(key interface{}, value interface{}) {
			fmt.Println(time.Now().Format(time.RFC3339Nano))
		})
		cache, err = lru.New2Q(8)
		cache, err = lru.New2QParams(8, 0.5, 0.5)
		cache, err = lru.NewARC(8)
	*/

	if err != nil {
		panic(err)
	}

	for i := 0; i < 16; i++ {
		fmt.Println(i, cache.Add(i, nil))
	}

	fmt.Println(cache.Get(6))
	fmt.Println(cache.Contains(6))
	fmt.Println(cache.Peek(6))
	fmt.Println(cache.GetOldest())
	cache.Remove(6)
	fmt.Println(cache.RemoveOldest())
	fmt.Println(cache.Keys())
	fmt.Println(cache.Len())
	cache.Purge()
	fmt.Println(cache.Len())
}

More examples can be found at github.com/flyaways/golang-lru/_examples.

Reference

Copyright 2018 The golang-lru Authors. All rights reserved.

for the golang-lru Authors. Code is released under the Apache 2 license.

FOSSA Status

Documentation

Overview

Package lru provides three different LRU caches of varying sophistication.

Cache is a simple LRU cache. It is based on the LRU implementation in groupcache: https://github.com/golang/groupcache/tree/master/lru

TwoQueueCache tracks frequently used and recently used entries separately. This avoids a burst of accesses from taking out frequently used entries, at the cost of about 2x computational overhead and some extra bookkeeping.

ARCCache is an adaptive replacement cache. It tracks recent evictions as well as recent usage in both the frequent and recent caches. Its computational overhead is comparable to TwoQueueCache, but the memory overhead is linear with the size of the cache.

ARC has been patented by IBM, so do not use it if that is problematic for your program.

All caches in this package take locks while operating, and are therefore thread-safe for consumers.

Index

Constants

View Source
const (
	// Default2QRecentRatio is the ratio of the 2Q cache dedicated
	// to recently added entries that have only been accessed once.
	Default2QRecentRatio = 0.25

	// Default2QGhostEntries is the default ratio of ghost
	// entries kept to track entries recently evicted
	Default2QGhostEntries = 0.50
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ARCCache

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

ARCCache is a thread-safe fixed size Adaptive Replacement Cache (ARC). ARC is an enhancement over the standard LRU cache in that tracks both frequency and recency of use. This avoids a burst in access to new entries from evicting the frequently used older entries. It adds some additional tracking overhead to a standard LRU cache, computationally it is roughly 2x the cost, and the extra memory overhead is linear with the size of the cache. ARC has been patented by IBM, but is similar to the TwoQueueCache (2Q) which requires setting parameters.

func NewARC

func NewARC(size int) (*ARCCache, error)

NewARC creates an ARC of the given size

func (*ARCCache) Add

func (c *ARCCache) Add(key, value interface{}) bool

Add adds a value to the cache.

func (*ARCCache) Contains

func (c *ARCCache) Contains(key interface{}) bool

Contains is used to check if the cache contains a key without updating recency or frequency.

func (*ARCCache) Get

func (c *ARCCache) Get(key interface{}) (value interface{}, ok bool)

Get looks up a key's value from the cache.

func (*ARCCache) GetOldest

func (c *ARCCache) GetOldest() (interface{}, interface{}, bool)

GetOldest the oldest item from the cache.

func (*ARCCache) Keys

func (c *ARCCache) Keys() []interface{}

Keys returns all the cached keys

func (*ARCCache) Len

func (c *ARCCache) Len() int

Len returns the number of cached entries

func (*ARCCache) Peek

func (c *ARCCache) Peek(key interface{}) (value interface{}, ok bool)

Peek is used to inspect the cache value of a key without updating recency or frequency.

func (*ARCCache) Purge

func (c *ARCCache) Purge()

Purge is used to clear the cache

func (*ARCCache) Remove

func (c *ARCCache) Remove(key interface{}) bool

Remove is used to purge a key from the cache

func (*ARCCache) RemoveOldest

func (c *ARCCache) RemoveOldest() (interface{}, interface{}, bool)

RemoveOldest removes the oldest item from the cache.

type Cache

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

Cache is a thread-safe fixed size LRU cache.

func New

func New(size int) (*Cache, error)

New creates an LRU of the given size.

func NewWithEvict

func NewWithEvict(size int, onEvicted func(key interface{}, value interface{})) (*Cache, error)

NewWithEvict constructs a fixed size cache with the given eviction callback.

func (*Cache) Add

func (c *Cache) Add(key, value interface{}) (evicted bool)

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

func (*Cache) Contains

func (c *Cache) Contains(key interface{}) bool

Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.

func (*Cache) Get

func (c *Cache) Get(key interface{}) (value interface{}, ok bool)

Get looks up a key's value from the cache.

func (*Cache) GetOldest

func (c *Cache) GetOldest() (interface{}, interface{}, bool)

GetOldest the oldest item from the cache.

func (*Cache) Keys

func (c *Cache) Keys() []interface{}

Keys returns a slice of the keys in the cache, from oldest to newest.

func (*Cache) Len

func (c *Cache) Len() int

Len returns the number of items in the cache.

func (*Cache) Peek

func (c *Cache) Peek(key interface{}) (value interface{}, ok bool)

Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.

func (*Cache) Purge

func (c *Cache) Purge()

Purge is used to completely clear the cache.

func (*Cache) Remove

func (c *Cache) Remove(key interface{}) bool

Remove removes the provided key from the cache.

func (*Cache) RemoveOldest

func (c *Cache) RemoveOldest() (interface{}, interface{}, bool)

RemoveOldest removes the oldest item from the cache.

type EvictCallback

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

EvictCallback is used to get a callback when a cache entry is evicted

type LRU

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

LRU implements a non-thread safe fixed size LRU cache

func NewLRU

func NewLRU(size int, onEvict EvictCallback) (*LRU, error)

NewLRU constructs an LRU of the given size

func (*LRU) Add

func (c *LRU) Add(key, value interface{}) (evicted bool)

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

func (*LRU) Contains

func (c *LRU) Contains(key interface{}) (ok bool)

Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.

func (*LRU) Get

func (c *LRU) Get(key interface{}) (value interface{}, ok bool)

Get looks up a key's value from the cache.

func (*LRU) GetOldest

func (c *LRU) GetOldest() (key interface{}, value interface{}, ok bool)

GetOldest returns the oldest entry

func (*LRU) Keys

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

Keys returns a slice of the keys in the cache, from oldest to newest.

func (*LRU) Len

func (c *LRU) Len() int

Len returns the number of items in the cache.

func (*LRU) Peek

func (c *LRU) Peek(key interface{}) (value interface{}, ok bool)

Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.

func (*LRU) Purge

func (c *LRU) Purge()

Purge is used to completely clear the cache.

func (*LRU) Remove

func (c *LRU) Remove(key interface{}) (present bool)

Remove removes the provided key from the cache, returning if the key was contained.

func (*LRU) RemoveOldest

func (c *LRU) RemoveOldest() (key interface{}, value interface{}, ok bool)

RemoveOldest removes the oldest item from the cache.

type LRUCache

type LRUCache interface {
	// Adds a value to the cache, returns true if an eviction occurred and
	// updates the "recently used"-ness of the key.
	Add(key, value interface{}) bool

	// Returns key's value from the cache and
	// updates the "recently used"-ness of the key. #value, isFound
	Get(key interface{}) (value interface{}, ok bool)

	// Check if a key exsists in cache without updating the recent-ness.
	Contains(key interface{}) (ok bool)

	// Returns key's value without updating the "recently used"-ness of the key.
	Peek(key interface{}) (value interface{}, ok bool)

	// Removes a key from the cache.
	Remove(key interface{}) bool

	// Removes the oldest entry from cache.
	RemoveOldest() (interface{}, interface{}, bool)

	// Returns the oldest entry from the cache. #key, value, isFound
	GetOldest() (interface{}, interface{}, bool)

	// Returns a slice of the keys in the cache, from oldest to newest.
	Keys() []interface{}

	// Returns the number of items in the cache.
	Len() int

	// Clear all cache entries
	Purge()
}

LRUCache is the interface for simple LRU cache.

type TwoQueueCache

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

TwoQueueCache is a thread-safe fixed size 2Q cache. 2Q is an enhancement over the standard LRU cache in that it tracks both frequently and recently used entries separately. This avoids a burst in access to new entries from evicting frequently used entries. It adds some additional tracking overhead to the standard LRU cache, and is computationally about 2x the cost, and adds some metadata over head. The ARCCache is similar, but does not require setting any parameters.

func New2Q

func New2Q(size int) (*TwoQueueCache, error)

New2Q creates a new TwoQueueCache using the default values for the parameters.

func New2QParams

func New2QParams(size int, recentRatio float64, ghostRatio float64) (*TwoQueueCache, error)

New2QParams creates a new TwoQueueCache using the provided parameter values.

func (*TwoQueueCache) Add

func (c *TwoQueueCache) Add(key, value interface{}) bool

Add adds a value to the cache.

func (*TwoQueueCache) Contains

func (c *TwoQueueCache) Contains(key interface{}) bool

Contains is used to check if the cache contains a key without updating recency or frequency.

func (*TwoQueueCache) Get

func (c *TwoQueueCache) Get(key interface{}) (value interface{}, ok bool)

Get looks up a key's value from the cache.

func (*TwoQueueCache) GetOldest

func (c *TwoQueueCache) GetOldest() (interface{}, interface{}, bool)

GetOldest the oldest item from the cache.

func (*TwoQueueCache) Keys

func (c *TwoQueueCache) Keys() []interface{}

Keys returns a slice of the keys in the cache. The frequently used keys are first in the returned slice.

func (*TwoQueueCache) Len

func (c *TwoQueueCache) Len() int

Len returns the number of items in the cache.

func (*TwoQueueCache) Peek

func (c *TwoQueueCache) Peek(key interface{}) (value interface{}, ok bool)

Peek is used to inspect the cache value of a key without updating recency or frequency.

func (*TwoQueueCache) Purge

func (c *TwoQueueCache) Purge()

Purge is used to completely clear the cache.

func (*TwoQueueCache) Remove

func (c *TwoQueueCache) Remove(key interface{}) bool

Remove removes the provided key from the cache.

func (*TwoQueueCache) RemoveOldest

func (c *TwoQueueCache) RemoveOldest() (interface{}, interface{}, bool)

RemoveOldest removes the oldest item from the cache.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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