lru

package
v0.0.0-...-b8ff64e Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2018 License: MIT Imports: 4 Imported by: 1

Documentation

Overview

Package lru implements an LRU cache. Adapted from golang.org/x/build/internal/lru.

Index

Constants

View Source
const LongDuration = 365 * 24 * time.Hour

LongDuration is one year; use when you don't really want a timeout on the cache (e.g. in tests).

Variables

View Source
var ErrCacheMiss = errors.New("lru cache miss")

ErrCacheMiss is an error indicating that the value is not in the cache.

Functions

This section is empty.

Types

type Cache

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

Cache is an LRU cache, safe for concurrent access.

func New

func New(maxEntries int, lifetime time.Duration) *Cache

New returns a new cache with the provided maximum items.

func (*Cache) Get

func (c *Cache) Get(key string) (value string, err error)

Get fetches the key's value from the cache. The error result will be nil if the item was found. Note that while the entry will be moved to the front of the queue, its expiration clock will not restart, and it will be removed if it has expired.

func (*Cache) Len

func (c *Cache) Len() int

Len returns the number of items in the cache.

func (*Cache) RemoveOldest

func (c *Cache) RemoveOldest() (key, value string)

RemoveOldest removes the oldest item in the cache and returns its key and value. If the cache is empty, two empty strings are returned.

func (*Cache) Set

func (c *Cache) Set(key, value string) bool

Set adds the provided key and value to the cache, evicting an old item if necessary. Returns true if item as added to cache.

func (*Cache) Unset

func (c *Cache) Unset(key string) error

Unset removes the provided key from the cache.

Jump to

Keyboard shortcuts

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