Remove leading spaces from README

This commit is contained in:
Patrick Mylund Nielsen 2012-01-29 05:42:07 +01:00
parent eaf2373adf
commit 73233e97c3
1 changed files with 66 additions and 64 deletions

116
README
View File

@ -8,81 +8,83 @@ be saved to and loaded from a file (or any io.Reader/Writer) to recover from
downtime quickly. downtime quickly.
== Installation == Installation
goinstall github.com/pmylund/go-cache
goinstall github.com/pmylund/go-cache
== Usage == Usage
import "github.com/pmylund/go-cache"
// Create a cache with a default expiration time of 5 minutes, and which import "github.com/pmylund/go-cache"
// 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 // Create a cache with a default expiration time of 5 minutes, and which
c.Set("foo", "bar", 0) // purges expired items every 30 seconds
c := cache.New(5*time.Minute, 30*time.Second)
// Set the value of the key "baz" to "yes", with no expiration time // Set the value of the key "foo" to "bar", with the default expiration time
// (the item won't be removed until it is re-set, or removed using c.Set("foo", "bar", 0)
// c.Delete("baz")
c.Set("baz", "yes", -1)
// Get the string associated with the key "foo" from the cache // Set the value of the key "baz" to "yes", with no expiration time
foo, found := c.Get("foo") // (the item won't be removed until it is re-set, or removed using
if found { // c.Delete("baz")
c.Set("baz", "yes", -1)
// Get the string associated with the key "foo" from the cache
foo, found := c.Get("foo")
if found {
fmt.Println(foo) fmt.Println(foo)
} }
// Since Go is statically typed, and cache values can be anything, type // 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 // 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 // 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 // values which will only be used once--e.g. for passing to another
// function--is: // 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. // This gets tedious if the value is used several times in the same function.
// You 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)
... ...
} }
// or // or
var foo string var foo string
if x, found := c.Get("foo"); found { if x, found := c.Get("foo"); found {
foo = x.(string) foo = x.(string)
} }
... ...
// foo can then be passed around freely as a string // foo can then be passed around freely as a string
// Want performance? Store pointers! // Want performance? Store pointers!
c.Set("foo", &MyStruct, 0) c.Set("foo", &MyStruct, 0)
if x, found := c.Get("foo"); found { if x, found := c.Get("foo"); found {
foo := x.(*MyStruct) foo := x.(*MyStruct)
... ...
} }
// If you store a reference type like a pointer, slice, map or channel, you // 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 // 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 // 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 // pointer you've stored in the cache, retrieving that pointer with Get will
// point you to the same data: // point you to the same data:
foo := &MyStruct{Num: 1} foo := &MyStruct{Num: 1}
c.Set("foo", foo, 0) c.Set("foo", foo, 0)
... ...
x, _ := c.Get("foo") x, _ := c.Get("foo")
foo := x.(*MyStruct) foo := x.(*MyStruct)
fmt.Println(foo.Num) fmt.Println(foo.Num)
... ...
foo.Num++ foo.Num++
... ...
x, _ := c.Get("foo") x, _ := c.Get("foo")
foo := x.(*MyStruct) foo := x.(*MyStruct)
foo.Println(foo.Num) foo.Println(foo.Num)
// will print: // will print:
1 1
2 2
== Reference == Reference