lrucache

package module
v0.0.0-...-08bf42a Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2020 License: Apache-2.0 Imports: 3 Imported by: 0

README

golang-lrucache

This is a bit of code I wrote for an interview test.

Documentation

Overview

Package lrucache contains a simple LRU cache, designed to wrap a function.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	ResponsesList   *list.List
	ResponsesLookup map[uint64]*list.Element

	sync.Mutex
	// contains filtered or unexported fields
}

Cache represents an instance of an LRU cache.

func New

func New(size int, f CachedFunc) Cache

New returns a new Cache instance.

func (*Cache) Call

func (c *Cache) Call(a uint64) uint64

Call checks the lookup table to see if there is already a cached response for this input. If there already is, move the cache entry to the front of the list. If not, check if our cache is full. If full, delete the least recently used entry (at the end of the list) and insert the new one in the front.

type CachedFunc

type CachedFunc = func(a uint64) uint64

CachedFunc represents a function that we want to cache the responses from.

type Entry

type Entry struct {
	Key   uint64
	Value uint64
}

Entry is a list Entry, we're adding both key and value here so that we can easily remove it from the lookup table.

Jump to

Keyboard shortcuts

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