Categorygithub.com/flyaways/golang-lru
modulepackage
0.0.0-20190617091412-ec8b77fcae6c
Repository: https://github.com/flyaways/golang-lru.git
Documentation: pkg.go.dev

# 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 and License

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

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

FOSSA Status

# Functions

New creates an LRU of the given size.
New2Q creates a new TwoQueueCache using the default values for the parameters.
New2QParams creates a new TwoQueueCache using the provided parameter values.
NewARC creates an ARC of the given size.
NewLRU constructs an LRU of the given size.
NewWithEvict constructs a fixed size cache with the given eviction callback.

# Constants

Default2QGhostEntries is the default ratio of ghost entries kept to track entries recently evicted.
Default2QRecentRatio is the ratio of the 2Q cache dedicated to recently added entries that have only been accessed once.

# Structs

ARCCache is a thread-safe fixed size Adaptive Replacement Cache (ARC).
Cache is a thread-safe fixed size LRU cache.
LRU implements a non-thread safe fixed size LRU cache.
TwoQueueCache is a thread-safe fixed size 2Q cache.

# Interfaces

LRUCache is the interface for simple LRU cache.

# Type aliases

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