lrucache

package module
v0.0.0-...-735e4c3 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2016 License: Apache-2.0 Imports: 4 Imported by: 1

Documentation

Overview

lrucache implements a simple memory-based LRU cache.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound  = errors.New("item not found")
	ErrNotStored = errors.New("item not stored")
)

Functions

This section is empty.

Types

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

New creates an LRU cache of the given size. if size<=0, will panic.

func (*Cache) Add

func (cache *Cache) Add(key Key, value Value) (err error)

Add adds key-value to cache. if there already exists a item with the same key, it returns ErrNotStored.

NOTE: the comparison operators == and != must be fully defined for
      operands of the key type.

func (*Cache) Get

func (cache *Cache) Get(key Key) (value Value, err error)

Get looks up a key's value from the cache. if there is no such item with the key it returns ErrNotFound.

NOTE: the comparison operators == and != must be fully defined for
      operands of the key type.

func (*Cache) Len

func (cache *Cache) Len() (n int)

Len returns the number of items in the cache.

func (*Cache) Purge

func (cache *Cache) Purge()

Purge is used to completely clear the cache

func (*Cache) Remove

func (cache *Cache) Remove(key Key) (err error)

Remove removes the provided key from the cache. if there is no such item with the key it returns ErrNotFound, normally you can ignore this error.

NOTE: the comparison operators == and != must be fully defined for
      operands of the key type.

func (*Cache) Set

func (cache *Cache) Set(key Key, value Value) (err error)

Set sets key-value to cache, unconditional

NOTE: the comparison operators == and != must be fully defined for
      operands of the key type.

func (*Cache) SetSize

func (cache *Cache) SetSize(size int)

SetSize sets a new size for the cache. if size <=0, we do nothing.

func (*Cache) Size

func (cache *Cache) Size() (size int)

Size returns the size of cache.

type Key

type Key interface{} // can changes to explicit type you want

type Value

type Value interface{} // can changes to explicit type you want

Jump to

Keyboard shortcuts

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