cache

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2016 License: MPL-2.0 Imports: 4 Imported by: 2

README

cache: Go LRU cache package

This package is based on https://github.com/hashicorp/golang-lru which is in turn based on the LRU implementation in Brad Fitz's Groupcache package.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {

	// Put an object into the cache and expire after the given ttl
	Put(key string, value interface{}, ttl time.Duration)

	// Get an object from the cache. Stale objects are returned, but marked as stale
	Get(key string) (value interface{}, status Result)

	// Size returns the max items that may be stored in the cache
	Size() int
}

Cache is the abstract interface for a cache that requires an TTL duration for objects

func NOOPCache

func NOOPCache() Cache

func New

func New(maxSize int) Cache

New constructs a new LRU/TTL cache with the given max size (num objects)

func WriteOnly

func WriteOnly(c Cache) Cache

WriteOnly wraps a cache in a write-only wrapper (Get() always returns null)

type LRUCache

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

Cache is a thread-safe fixed size LRU cache.

func NewLRUCache

func NewLRUCache(size int) (*LRUCache, error)

NewLRUCache creates an LRU of the given size

func (*LRUCache) Add

func (c *LRUCache) Add(key, value interface{})

Add adds a value to the cache.

func (*LRUCache) Get

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

Get looks up a key's value from the cache.

func (*LRUCache) Len

func (c *LRUCache) Len() int

Len returns the number of items in the cache.

func (*LRUCache) Purge

func (c *LRUCache) Purge()

Purge is used to completely clear the cache

func (*LRUCache) Remove

func (c *LRUCache) Remove(key interface{})

Remove removes the provided key from the cache.

func (*LRUCache) RemoveOldest

func (c *LRUCache) RemoveOldest()

RemoveOldest removes the oldest item from the cache.

type Result

type Result string

Result is the code of a cache Get operation

const (
	// OK : object for key found and fresh
	OK Result = "ok"
	// NotFound : object for key not found
	NotFound Result = "not found"
	// Stale : object for key found, but stale
	Stale Result = "stale"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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