lru

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2020 License: BSD-3-Clause Imports: 1 Imported by: 0

Documentation

Overview

Package lru implements a Least-Recently-Used (LRU) cache of objects keyed by a string.

A given key can have multiple values associated with it in the cache.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache implements a Least-Recently-Used cache of objects. Cache objects are not safe for concurrent use.

func New

func New(capacity int) *Cache

New creates an LRU-cache that can hold up to capacity objects.

func (*Cache) Get

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

Get returns a value in the cache associated with the provided key and removes it from the cache's storage. It returns (nil, false) if no value is associated with the key.

If there are multiple values associated with the provided key, Get will return the most recently used one.

func (*Cache) Put

func (c *Cache) Put(key string, value interface{}) (evictedKey string, evictedValue interface{}, evicted bool)

Put adds the provided (key, value) pair to the cache and returns the (key, value) pair that was evicted (if any) in order to make space for the provided pair.

It is legal to use the same key in multiple Put invocations as the cache can hold multiple values associated with the same key.

func (*Cache) Size

func (c *Cache) Size() int

Size returns the number of items currently in the cache.

Jump to

Keyboard shortcuts

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