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

130
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")
fmt.Println(foo) c.Set("baz", "yes", -1)
}
// Since Go is statically typed, and cache values can be anything, type // Get the string associated with the key "foo" from the cache
// assertion is needed when values are being passed to functions that don't foo, found := c.Get("foo")
// take arbitrary types, (i.e. interface{}). The simplest way to do this for if found {
// values which will only be used once--e.g. for passing to another fmt.Println(foo)
// 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. // Since Go is statically typed, and cache values can be anything, type
// You might do either of the following instead: // assertion is needed when values are being passed to functions that don't
if x, found := c.Get("foo"); found { // take arbitrary types, (i.e. interface{}). The simplest way to do this for
foo := x.(string) // values which will only be used once--e.g. for passing to another
... // function--is:
} foo, found := c.Get("foo")
// or if found {
var foo string MyFunction(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! // This gets tedious if the value is used several times in the same function.
c.Set("foo", &MyStruct, 0) // You might do either of the following instead:
if x, found := c.Get("foo"); found { if x, found := c.Get("foo"); found {
foo := x.(*MyStruct) 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
// If you store a reference type like a pointer, slice, map or channel, you // Want performance? Store pointers!
// do not need to run Set if you modify the underlying data. The cached c.Set("foo", &MyStruct, 0)
// reference points to the same memory, so if you modify a struct whose if x, found := c.Get("foo"); found {
// pointer you've stored in the cache, retrieving that pointer with Get will foo := x.(*MyStruct)
// 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: // If you store a reference type like a pointer, slice, map or channel, you
1 // do not need to run Set if you modify the underlying data. The cached
2 // 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 == Reference