cache

package module
v0.0.0-...-7ffbc07 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2015 License: BSD-2-Clause Imports: 4 Imported by: 1

README

***************************************************************************
** This package is deprecated in favor of https://github.com/dchest/lru ***
***************************************************************************

Go package cache implements LRU (Least Recently Used) cache algorithm.

Cache capacity can be optionally limited by both size in bytes and a number
of items. Items can keep track of their modification and access time.

INSTALLATION

	$ go get github.com/dchest/cache

DOCUMENTATION

	See http://godoc.org/github.com/dchest/cache


ACKNOWLEDGEMENTS

Some code structure ideas are inspired by YouTube's Vitess implementation of
LRU cache, but no code was taken from it:
https://github.com/youtube/vitess/blob/master/go/cache/lru_cache.go

Thanks to Chaker Nakhli (@nakhli)  for the article:
http://www.sinbadsoft.com/blog/a-lru-cache-implementation/

Documentation

Overview

Package cache implements LRU (Least Recently Used) cache algorithm.

Cache capacity can be optionally limited by both size in bytes and a number of items. Items can keep track of their modification and access time.

Example:

// Create a 1 MB cache.
c := cache.New(cache.Config{ MaxBytes: 1024*1024 })
...
// Query cache and insert item if it's not there.
var x someType
v, ok := c.Get("key")
if !ok {
	// Item is not in cache, fetch it from main storage...
	x = ...
	// ...then add it to cache.
	c.Set("key", x, x.Size())
} else {
	x = v.(someType)
}
// x now contains the value.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func New

func New(config Config) *Cache

New returns a cache instance configured with the given Config.

func (*Cache) Config

func (c *Cache) Config() Config

Config returns a copy of cache configuration.

func (*Cache) Get

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

Get returns a value of item cached under the given key. If there is no such key in the cache, it returns nil, false.

func (*Cache) GetBytes

func (c *Cache) GetBytes(key string) (value []byte, ok bool)

GetBytes is like Get, but returns a bytes slice value. It panics if value is not a byte slice.

func (*Cache) GetItem

func (c *Cache) GetItem(key string) (it Item, ok bool)

GetItem returns a copy of item cached under the given key.

func (*Cache) Items

func (c *Cache) Items() []Item

Items returns a slice of copies of all items in cache.

func (*Cache) Len

func (c *Cache) Len() int

Len returns the number of items in cache.

func (*Cache) OldestItem

func (c *Cache) OldestItem() (it Item, ok bool)

OldestItem returns a copy of least recently used item. If cache contains no items, the second return value is false. Accessing oldest item via this function doesn't change its access time or the order of items in cache.

func (*Cache) Reconfigure

func (c *Cache) Reconfigure(newConfig Config)

Reconfigure sets a new cache configuration.

func (*Cache) Remove

func (c *Cache) Remove(key string) bool

Remove deletes an item with the given key from cache.

func (*Cache) Reset

func (c *Cache) Reset()

Reset clears the cache.

func (*Cache) Set

func (c *Cache) Set(key string, value interface{}, size int64)

Set sets or updates a cache item for the given key to the given value, and the given value size in bytes.

func (*Cache) SetBytes

func (c *Cache) SetBytes(key string, value []byte)

SetBytes is like Set, but accepts a slice of bytes for value, and sets the size to its length.

func (*Cache) Size

func (c *Cache) Size() int64

Size returns the size of all values in cache.

type Config

type Config struct {
	// Maximum number of items the cache can contain (unlimited by default).
	MaxItems int

	// Maximum byte capacity of cache (unlimited by default).
	MaxBytes int64

	// Track item access time (false by default).
	TrackAccessTime bool

	// Item expiration duration.
	//
	// An item is removed from cache when trying to get it if the given
	// time passed since its modification time.
	//
	// Set to zero for no expiration (default).
	Expires time.Duration

	// Function to call when an item is dropped or removed from cache
	// (nil by default).
	//
	// The handler function is given a copy of the removed item as an argument.
	// During its execution the cache is locked, and must not be accessed or
	// modified from the handler function.
	RemoveHandler func(Item)
}

type Item

type Item struct {
	Key        string
	Value      interface{}
	Size       int64     // byte size of value
	ModTime    time.Time // when this item was added to cache
	AccessTime time.Time // when this item was last accessed
}

Item represents a cached item with additional information.

Jump to

Keyboard shortcuts

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