golanglrutimedcache

package module
v0.0.0-...-d771686 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2015 License: MIT Imports: 4 Imported by: 0

README

Build StatusCoverage StatusGoDoc

golang-lru-timedcache

Simple wrapper for golang-lru which allows time based expiration of keys.

The cache is indexed by both an key and an id.


//Cache of size 20,000 with expiration set to 60,000ms (1 min)
tc, err := NewTimedCache(20000, 60000, nil)

keyid := 5
tc.Set("stringkey",keyid, "value")

//v is "value"
v, ok := tc.GetByName("stringkey")
v, ok = tc.GetByID(5)

//Removing can be done by ID or by name - removing by one automatically
//removes the other, so no need to call both
tc.RemoveID(5)
tc.RemoveName("stringkey")

//Since there is both an id and an index, we can set a value only knowing
//its id (or update only knowing id)
tc.SetID(3,"secondvalue")

//To change a value by ID without changing associated string key, use Update
tc.Update(3,"thirdvalue")

//Clears the cache
tc.Purge()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TimedCache

type TimedCache struct {
	sync.Mutex //Necessary to use the nameMap
	// contains filtered or unexported fields
}

TimedCache is an lru cache with an additional expiration time for the data in each element It is not a very clever cache, and does not factor in time expiration in lru

func NewTimedCache

func NewTimedCache(size int, expireTime int64, err error) (*TimedCache, error)

NewTimedCache returns the TimedCache with given parameters

func (*TimedCache) GetByID

func (tc *TimedCache) GetByID(key int64) (value interface{}, keyName string, ok bool)

GetByID gets an element if it is cached and not yet expired

func (*TimedCache) GetByName

func (tc *TimedCache) GetByName(name string) (value interface{}, ok bool)

GetByName gets an element if it is in the cache and not expired

func (*TimedCache) GetNameID

func (tc *TimedCache) GetNameID(keyName string) (nameID int64, ok bool)

GetNameID gets the ID associated with a name

func (*TimedCache) Purge

func (tc *TimedCache) Purge()

Purge clears all elements in the cache

func (*TimedCache) PurgeNames

func (tc *TimedCache) PurgeNames()

PurgeNames clears all the names from the cache, but leaves the cache elements accessible by ID

func (*TimedCache) RemoveID

func (tc *TimedCache) RemoveID(key int64)

RemoveID the given key from the cache by ID

func (*TimedCache) RemoveName

func (tc *TimedCache) RemoveName(name string)

RemoveName deletes the cache element by its name

func (*TimedCache) Set

func (tc *TimedCache) Set(name string, id int64, value interface{})

Set adds a new element to the cache

func (*TimedCache) SetID

func (tc *TimedCache) SetID(id int64, value interface{})

SetID sets a new element without knowing its name

func (*TimedCache) UnlinkName

func (tc *TimedCache) UnlinkName(keyName string)

UnlinkName unlinks the name fro mthe ID

func (*TimedCache) UnlinkNamePrefix

func (tc *TimedCache) UnlinkNamePrefix(namePrefix string)

UnlinkNamePrefix removes all names with the given prefix. This is not a very efficient way to do it, since it loops through the entire cache unlinking names. It locks queries by name for the entire time.

func (*TimedCache) Update

func (tc *TimedCache) Update(id int64, value interface{})

Update an element without changing the associated name

Jump to

Keyboard shortcuts

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