cache

package module
v0.0.0-...-0a0f458 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2013 License: MIT Imports: 9 Imported by: 0

README

go-cache is an in-memory key:value store/cache similar to memcached that is
suitable for applications running on a single machine. Its major advantage is
that, being essentially a thread-safe map[string]interface{} with expiration
times, it doesn't need to serialize or transmit its contents over the network.

Any object can be stored, for a given duration or forever, and the cache can be
safely used by multiple goroutines.

Although go-cache isn't meant to be used as a persistent datastore, the entire
cache may be saved to and loaded from a file (or any io.Reader/Writer) to
recover from downtime quickly.

== Installation

go get github.com/pmylund/go-cache

== Usage

import "github.com/pmylund/go-cache"

// Create a cache with a default expiration time of 5 minutes, and which
// purges expired items every 30 seconds
c := cache.New(5*time.Minute, 30*time.Second)

// Set the value of the key "foo" to "bar", with the default expiration time
c.Set("foo", "bar", 0)

// Set the value of the key "baz" to 42, with no expiration time
// (the item won't be removed until it is re-set, or removed using
// c.Delete("baz")
c.Set("baz", 42, -1)

// Get the string associated with the key "foo" from the cache
foo, found := c.Get("foo")
if found {
        fmt.Println(foo)
}

// Since Go is statically typed, and cache values can be anything, type
// assertion is needed when values are being passed to functions that don't
// take arbitrary types, (i.e. interface{}). The simplest way to do this for
// values which will only be used once--e.g. for passing to another
// function--is:
foo, found := c.Get("foo")
if found {
        MyFunction(foo.(string))
}

// This gets tedious if the value is used several times in the same function.
// You might do either of the following instead:
if x, found := c.Get("foo"); found {
        foo := x.(string)
        ...
}
// or
var foo string
if x, found := c.Get("foo"); found {
        foo = x.(string)
}
...
// foo can then be passed around freely as a string

// Want performance? Store pointers!
c.Set("foo", &MyStruct, 0)
if x, found := c.Get("foo"); found {
        foo := x.(*MyStruct)
        ...
}

// If you store a reference type like a pointer, slice, map or channel, you
// do not need to run Set if you modify the underlying data. The cached
// reference points to the same memory, so if you modify a struct whose
// pointer you've stored in the cache, retrieving that pointer with Get will
// point you to the same data:
foo := &MyStruct{Num: 1}
c.Set("foo", foo, 0)
...
x, _ := c.Get("foo")
foo := x.(*MyStruct)
fmt.Println(foo.Num)
...
foo.Num++
...
x, _ := c.Get("foo")
foo := x.(*MyStruct)
foo.Println(foo.Num)

// will print:
1
2

== Reference

func New(de, ci time.Duration) *Cache
    Return a new cache with a given default expiration duration and cleanup
    interval. If the expiration duration is less than 1, the items in the cache
    never expire (by default), and must be deleted manually. If the cleanup
    interval is less than one, expired items are not deleted from the cache
    before their next lookup or before calling DeleteExpired.

func (c *Cache) Set(k string, x interface{}, d time.Duration)
    Add an item to the cache, replacing any existing item. If the duration is
    0, the cache's default expiration time is used. If it is -1, the item never
    expires.

func (c *Cache) Add(k string, x interface{}, d time.Duration) error
    Add an item to the cache only if an item doesn't already exist for the
    given key, or if the existing item has expired. Returns an error if not.

func (c *Cache) Replace(k string, x interface{}, d time.Duration) error
    Set a new value for the cache key only if it already exists. Returns an
    error if it does not.

func (c *Cache) Get(k string) (interface{}, bool)
    Get an item from the cache. Returns the item or nil, and a bool indicating
    whether the key was found.

func (c *Cache) Increment(k string, n int64) error
    Increment an item of type int, int8, int16, int32, int64, uintptr, uint,
    uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the
    item's value is not an integer, if it was not found, or if it is not
    possible to increment it by n. To retrieve the incremented value, use one
    of the specialized methods, e.g. IncrementInt64.

func (c *Cache) IncrementFloat(k string, n float64) error
    Increment an item of type float32 or float64 by n. Returns an error if the
    item's value is not floating point, if it was not found, or if it is not
    possible to increment it by n.

func (c *Cache) IncrementInt(k string, n int) (int, error)
func (c *Cache) IncrementInt8(k string, n int8) (int8, error)
func (c *Cache) IncrementInt16(k string, n int16) (int16, error)
func (c *Cache) IncrementInt32(k string, n int32) (int32, error)
func (c *Cache) IncrementInt64(k string, n int64) (int64, error)
func (c *Cache) IncrementUint(k string, n uint) (uint, error)
func (c *Cache) IncrementUint8(k string, n uint8) (uint8, error)
func (c *Cache) IncrementUint16(k string, n uint16) (uint16, error)
func (c *Cache) IncrementUint32(k string, n uint32) (uint32, error)
func (c *Cache) IncrementUint64(k string, n uint64) (uint64, error)
func (c *Cache) IncrementFloat32(k string, n float32) (float32, error)
func (c *Cache) IncrementFloat64(k string, n float64) (float64, error)
    Increment an item of the specified type by n. Returns an error if the item's
    value is not of that type, or if it was not found. If there is no error, the
    incremented value is returned.

func (c *Cache) Decrement(k string, n int64) error
    Decrement an item of type int, int8, int16, int32, int64, uintptr, uint,
    uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the
    item's value is not an integer, if it was not found, or if it is not
    possible to decrement it by n.

func (c *Cache) DecrementFloat(k string, n float64) error
    Decrement an item of type float32 or float64 by n. Returns an error if the
    item's value is floating point, if it was not found, or if it is not
    possible to decrement it by n.

func (c *Cache) DecrementInt(k string, n int) (int, error)
func (c *Cache) DecrementInt8(k string, n int8) (int8, error)
func (c *Cache) DecrementInt16(k string, n int16) (int16, error)
func (c *Cache) DecrementInt32(k string, n int32) (int32, error)
func (c *Cache) DecrementInt64(k string, n int64) (int64, error)
func (c *Cache) DecrementUint(k string, n uint) (uint, error)
func (c *Cache) DecrementUintptr(k string, n uintptr) (uintptr, error)
func (c *Cache) DecrementUint8(k string, n uint8) (uint8, error)
func (c *Cache) DecrementUint16(k string, n uint16) (uint16, error)
func (c *Cache) DecrementUint32(k string, n uint32) (uint32, error)
func (c *Cache) DecrementUint64(k string, n uint64) (uint64, error)
func (c *Cache) DecrementFloat32(k string, n float32) (float32, error)
func (c *Cache) DecrementFloat64(k string, n float64) (float64, error)
    Decrement an item of the specified type by n. Returns an error if the item's
    value is not of that type, or if it was not found. If there is no error, the
    decremented value is returned.

func (c *Cache) Delete(k string)
    Delete an item from the cache. Does nothing if the key does not exist in the
    cache.

func (c *Cache) DeleteExpired()
    Delete all expired items from the cache.

func (c *Cache) ItemCount()
    Returns the number of items in the cache. This may include items that have
    expired, but have not yet been cleaned up.

func (c *Cache) Flush()
    Delete all items from the cache.

func (c *Cache) Save(w io.Writer) error
    Write the cache's items (using Gob) to an io.Writer. Returns an error if
    the serialization fails, e.g. because there are unserializable objects like
    channels in the cache.

func (c *Cache) SaveFile(fname string) error
    Save the cache's items to the given filename, creating the file if it
    doesn't exist, and overwriting it if it does.

func (c *Cache) Load(r io.Reader) error
    Add (Gob-serialized) cache items from an io.Reader, excluding any items
    with keys that already exist in the current cache.

func (c *Cache) LoadFile(fname string) error
    Loads and adds cache items from the given filename, excluding any items
    with keys that already exist in the current cache.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

func New

func New(defaultExpiration, cleanupInterval time.Duration) *Cache

Return a new cache with a given default expiration duration and cleanup interval. If the expiration duration is less than 1, the items in the cache never expire (by default), and must be deleted manually. If the cleanup interval is less than one, expired items are not deleted from the cache before calling DeleteExpired.

func (Cache) Add

func (c Cache) Add(k string, x interface{}, d time.Duration) error

Add an item to the cache only if an item doesn't already exist for the given key, or if the existing item has expired. Returns an error otherwise.

func (*Cache) Clone

func (c *Cache) Clone() map[string]interface{}

func (Cache) Decrement

func (c Cache) Decrement(k string, n int64) error

Decrement an item of type int, int8, int16, int32, int64, uintptr, uint, uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the item's value is not an integer, if it was not found, or if it is not possible to decrement it by n. To retrieve the decremented value, use one of the specialized methods, e.g. DecrementInt64.

func (Cache) DecrementFloat

func (c Cache) DecrementFloat(k string, n float64) error

Decrement an item of type float32 or float64 by n. Returns an error if the item's value is not floating point, if it was not found, or if it is not possible to decrement it by n. Pass a negative number to decrement the value. To retrieve the decremented value, use one of the specialized methods, e.g. DecrementFloat64.

func (Cache) DecrementFloat32

func (c Cache) DecrementFloat32(k string, n float32) (float32, error)

Decrement an item of type float32 by n. Returns an error if the item's value is not an float32, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementFloat64

func (c Cache) DecrementFloat64(k string, n float64) (float64, error)

Decrement an item of type float64 by n. Returns an error if the item's value is not an float64, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementInt

func (c Cache) DecrementInt(k string, n int) (int, error)

Decrement an item of type int by n. Returns an error if the item's value is not an int, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementInt16

func (c Cache) DecrementInt16(k string, n int16) (int16, error)

Decrement an item of type int16 by n. Returns an error if the item's value is not an int16, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementInt32

func (c Cache) DecrementInt32(k string, n int32) (int32, error)

Decrement an item of type int32 by n. Returns an error if the item's value is not an int32, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementInt64

func (c Cache) DecrementInt64(k string, n int64) (int64, error)

Decrement an item of type int64 by n. Returns an error if the item's value is not an int64, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementInt8

func (c Cache) DecrementInt8(k string, n int8) (int8, error)

Decrement an item of type int8 by n. Returns an error if the item's value is not an int8, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementUint

func (c Cache) DecrementUint(k string, n uint) (uint, error)

Decrement an item of type uint by n. Returns an error if the item's value is not an uint, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementUint16

func (c Cache) DecrementUint16(k string, n uint16) (uint16, error)

Decrement an item of type uint16 by n. Returns an error if the item's value is not an uint16, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementUint32

func (c Cache) DecrementUint32(k string, n uint32) (uint32, error)

Decrement an item of type uint32 by n. Returns an error if the item's value is not an uint32, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementUint64

func (c Cache) DecrementUint64(k string, n uint64) (uint64, error)

Decrement an item of type uint64 by n. Returns an error if the item's value is not an uint64, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementUint8

func (c Cache) DecrementUint8(k string, n uint8) (uint8, error)

Decrement an item of type uint8 by n. Returns an error if the item's value is not an uint8, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementUintptr

func (c Cache) DecrementUintptr(k string, n uintptr) (uintptr, error)

Decrement an item of type uintptr by n. Returns an error if the item's value is not an uintptr, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) Delete

func (c Cache) Delete(k string)

Delete an item from the cache. Does nothing if the key is not in the cache.

func (Cache) DeleteExpired

func (c Cache) DeleteExpired()

Delete all expired items from the cache.

func (Cache) Flush

func (c Cache) Flush()

Delete all items from the cache.

func (Cache) Get

func (c Cache) Get(k string) (interface{}, bool)

Get an item from the cache. Returns the item or nil, and a bool indicating whether the key was found.

func (Cache) GetDelete

func (c Cache) GetDelete(k string) (interface{}, bool)

func (Cache) GetSet

func (c Cache) GetSet(k string, x interface{}, d time.Duration) (interface{}, bool)

func (Cache) Increment

func (c Cache) Increment(k string, n int64) error

Increment an item of type int, int8, int16, int32, int64, uintptr, uint, uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the item's value is not an integer, if it was not found, or if it is not possible to increment it by n. To retrieve the incremented value, use one of the specialized methods, e.g. IncrementInt64.

func (Cache) IncrementFloat

func (c Cache) IncrementFloat(k string, n float64) error

Increment an item of type float32 or float64 by n. Returns an error if the item's value is not floating point, if it was not found, or if it is not possible to increment it by n. Pass a negative number to decrement the value. To retrieve the incremented value, use one of the specialized methods, e.g. IncrementFloat64.

func (Cache) IncrementFloat32

func (c Cache) IncrementFloat32(k string, n float32) (float32, error)

Increment an item of type float32 by n. Returns an error if the item's value is not an float32, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementFloat64

func (c Cache) IncrementFloat64(k string, n float64) (float64, error)

Increment an item of type float64 by n. Returns an error if the item's value is not an float64, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementInt

func (c Cache) IncrementInt(k string, n int) (int, error)

Increment an item of type int by n. Returns an error if the item's value is not an int, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementInt16

func (c Cache) IncrementInt16(k string, n int16) (int16, error)

Increment an item of type int16 by n. Returns an error if the item's value is not an int16, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementInt32

func (c Cache) IncrementInt32(k string, n int32) (int32, error)

Increment an item of type int32 by n. Returns an error if the item's value is not an int32, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementInt64

func (c Cache) IncrementInt64(k string, n int64) (int64, error)

Increment an item of type int64 by n. Returns an error if the item's value is not an int64, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementInt8

func (c Cache) IncrementInt8(k string, n int8) (int8, error)

Increment an item of type int8 by n. Returns an error if the item's value is not an int8, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementUint

func (c Cache) IncrementUint(k string, n uint) (uint, error)

Increment an item of type uint by n. Returns an error if the item's value is not an uint, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementUint16

func (c Cache) IncrementUint16(k string, n uint16) (uint16, error)

Increment an item of type uint16 by n. Returns an error if the item's value is not an uint16, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementUint32

func (c Cache) IncrementUint32(k string, n uint32) (uint32, error)

Increment an item of type uint32 by n. Returns an error if the item's value is not an uint32, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementUint64

func (c Cache) IncrementUint64(k string, n uint64) (uint64, error)

Increment an item of type uint64 by n. Returns an error if the item's value is not an uint64, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementUint8

func (c Cache) IncrementUint8(k string, n uint8) (uint8, error)

Increment an item of type uint8 by n. Returns an error if the item's value is not an uint8, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementUintptr

func (c Cache) IncrementUintptr(k string, n uintptr) (uintptr, error)

Increment an item of type uintptr by n. Returns an error if the item's value is not an uintptr, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) ItemCount

func (c Cache) ItemCount() int

Returns the number of items in the cache. This may include items that have expired, but have not yet been cleaned up.

func (Cache) Keys

func (c Cache) Keys() []string

func (Cache) Load

func (c Cache) Load(r io.Reader) error

Add (Gob-serialized) cache items from an io.Reader, excluding any items with keys that already exist (and haven't expired) in the current cache.

func (Cache) LoadFile

func (c Cache) LoadFile(fname string) error

Load and add cache items from the given filename, excluding any items with keys that already exist in the current cache.

func (Cache) Replace

func (c Cache) Replace(k string, x interface{}, d time.Duration) error

Set a new value for the cache key only if it already exists, and the existing item hasn't expired. Returns an error otherwise.

func (Cache) Save

func (c Cache) Save(w io.Writer) (err error)

Write the cache's items (using Gob) to an io.Writer.

func (Cache) SaveFile

func (c Cache) SaveFile(fname string) error

Save the cache's items to the given filename, creating the file if it doesn't exist, and overwriting it if it does.

func (Cache) Set

func (c Cache) Set(k string, x interface{}, d time.Duration)

Add an item to the cache, replacing any existing item. If the duration is 0, the cache's default expiration time is used. If it is -1, the item never expires.

Jump to

Keyboard shortcuts

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