Fix README line width

master
Patrick Mylund Nielsen 13 years ago
parent ac1a1aeef9
commit a4f5f9088b

148
README

@ -1,22 +1,24 @@
go-cache is an in-memory key:value store/cache similar to memcached that is suitable for go-cache is an in-memory key:value store/cache similar to memcached that is
applications running on a single machine. Any object can be stored, for a given duration suitable for applications running on a single machine. Any object can be stored,
or forever, and the cache can be used safely by multiple goroutines. for a given duration or forever, and the cache can be used safely by multiple
goroutines.
Installation: == Installation
goinstall github.com/pmylund/go-cache goinstall github.com/pmylund/go-cache
Usage: == Usage
import "github.com/pmylund/go-cache" import "github.com/pmylund/go-cache"
// Create a cache with a default expiration time of 5 minutes, and which purges // Create a cache with a default expiration time of 5 minutes, and which
// expired items every 30 seconds // purges expired items every 30 seconds
c := cache.New(5*time.Minute, 30*time.Second) c := cache.New(5*time.Minute, 30*time.Second)
// Set the value of the key "foo" to "bar", with the default expiration time // Set the value of the key "foo" to "bar", with the default expiration time
c.Set("foo", "bar", 0) c.Set("foo", "bar", 0)
// Set the value of the key "baz" to "yes", with no expiration time (the item // Set the value of the key "baz" to "yes", with no expiration time
// won't be removed until it is re-set, or removed using c.Delete("baz") // (the item won't be removed until it is re-set, or removed using
// c.Delete("baz")
c.Set("baz", "yes", -1) c.Set("baz", "yes", -1)
// Get the string associated with the key "foo" from the cache // Get the string associated with the key "foo" from the cache
@ -25,17 +27,18 @@ Usage:
fmt.Println(foo) fmt.Println(foo)
} }
// Since Go is statically typed, and cache values can be anything, type assertion // Since Go is statically typed, and cache values can be anything, type
// is needed when values are being passed to functions that don't take arbitrary types, // assertion is needed when values are being passed to functions that don't
// (i.e. interface{}). The simplest way to do this for values which will only be used // take arbitrary types, (i.e. interface{}). The simplest way to do this for
// once--e.g. for passing to another function--is: // values which will only be used once--e.g. for passing to another
// function--is:
foo, found := c.Get("foo") foo, found := c.Get("foo")
if found { if found {
MyFunction(foo.(string)) MyFunction(foo.(string))
} }
// This gets tedious if the value is used several times in the same function. You // This gets tedious if the value is used several times in the same function.
// might do either of the following instead: // You might do either of the following instead:
if x, found := c.Get("foo"); found { if x, found := c.Get("foo"); found {
foo := x.(string) foo := x.(string)
... ...
@ -55,10 +58,11 @@ Usage:
... ...
} }
// If you store a reference type like a pointer, slice, map or channel, you do not need to // If you store a reference type like a pointer, slice, map or channel, you
// run Set if you modify the underlying data. The cache does not serialize its data, so if // do not need to run Set if you modify the underlying data. The cache does
// you modify a struct whose pointer you've stored in the cache, retrieving that pointer // not serialize its data, so if you modify a struct whose pointer you've
// with Get will point you to the same data: // stored in the cache, retrieving that pointer with Get will point you to
// the same data:
foo := &MyStruct{Num: 1} foo := &MyStruct{Num: 1}
c.Set("foo", foo, 0) c.Set("foo", foo, 0)
... ...
@ -76,54 +80,58 @@ Usage:
1 1
2 2
Reference: == Reference
func New(de, ci time.Duration) *Cache func New(de, ci time.Duration) *Cache
Returns a new cache with a given default expiration duration and default cleanup Returns a new cache with a given default expiration duration and default
interval. If the expiration duration is less than 1, the items in the cache never cleanup interval. If the expiration duration is less than 1, the items in
expire and must be deleted manually. If the cleanup interval is less than one, the cache never expire and must be deleted manually. If the cleanup interval
expired items are not deleted from the cache before their next lookup or before is less than one, expired items are not deleted from the cache before their
calling DeleteExpired. next lookup or before calling DeleteExpired.
func (c *Cache) Set(k string, x interface{}, d time.Duration) func (c *Cache) Set(k string, x interface{}, d time.Duration)
Adds an item to the cache, replacing any existing item. If the duration is 0, the Adds an item to the cache, replacing any existing item. If the duration is 0,
cache's default expiration time is used. If it is -1, the item never expires. 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
Adds an item to the cache only if an item doesn't already exist for the given key, func (c *Cache) Add(k string, x interface{}, d time.Duration) error
or if the existing item has expired. Returns an error if not. Adds 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
Sets a new value for the cache item only if it already exists. Returns an error if func (c *Cache) Replace(k string, x interface{}, d time.Duration) error
it does not. Sets a new value for the cache item only if it already exists. Returns an
error if it does not.
func (c *Cache) Get(k string) (interface{}, bool)
Gets an item from the cache. Returns the item or nil, and a bool indicating whether func (c *Cache) Get(k string) (interface{}, bool)
the given key was found in the cache. Gets an item from the cache. Returns the item or nil, and a bool indicating
whether the given key was found in the cache.
func (c *Cache) Increment(k string, n int64) error
Increment an item of type int, int8, int16, int32, int64, uintptr, uint, uint8, func (c *Cache) Increment(k string, n int64) error
uint32, or uint64, float32 or float64 by n. Returns an error if the item's value Increment an item of type int, int8, int16, int32, int64, uintptr, uint,
is not an integer, if it was not found, or if it is not possible to increment it uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the
by n. Passing a negative number will cause the item to be decremented. item's value is not an integer, if it was not found, or if it is not possible
to increment it by n. Passing a negative number will cause the item to be
func (c *Cache) IncrementFloat(k string, n float64) error decremented.
Increment an item of type int, int8, int16, int32, int64, uintptr, uint, uint8,
uint32, uint64, float32 or float64 by n. Returns an error if the item's value is func (c *Cache) IncrementFloat(k string, n float64) error
not an integer, if it was not found, or if it is not possible to increment it by Increment an item of type int, int8, int16, int32, int64, uintptr, uint,
n. Passing a negative number will cause the item to be decremented. 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
func (c *Cache) Decrement(k string, n int64) error to increment it by n. Passing a negative number will cause the item to be
Decrement an item of type int, int8, int16, int32, int64, uintptr, uint, uint8, decremented.
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 func (c *Cache) Decrement(k string, n int64) error
by n. 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
func (c *Cache) Delete(k string) item's value is not an integer, if it was not found, or if it is not possible
Deletes an item from the cache. Does nothing if the item does not exist in the cache. to decrement it by n.
func (c *Cache) DeleteExpired() func (c *Cache) Delete(k string)
Deletes all expired items from the cache. Deletes an item from the cache. Does nothing if the item does not exist in
the cache.
func (c *Cache) Flush()
Deletes all items from the cache. func (c *Cache) DeleteExpired()
Deletes all expired items from the cache.
func (c *Cache) Flush()
Deletes all items from the cache.

Loading…
Cancel
Save