Skip to content

Commit dd1ed0b

Browse files
authored
README.md: Remove one level of indentation and increase 'recommended' cleanupInterval
1 parent e7a9def commit dd1ed0b

File tree

1 file changed

+75
-76
lines changed

1 file changed

+75
-76
lines changed

README.md

Lines changed: 75 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -20,86 +20,85 @@ one) to recover from downtime quickly. (See the docs for `NewFrom()` for caveats
2020
### Usage
2121

2222
```go
23-
import (
24-
"fmt"
25-
"github.com/patrickmn/go-cache"
26-
"time"
27-
)
28-
29-
func main() {
30-
31-
// Create a cache with a default expiration time of 5 minutes, and which
32-
// purges expired items every 30 seconds
33-
c := cache.New(5*time.Minute, 30*time.Second)
34-
35-
// Set the value of the key "foo" to "bar", with the default expiration time
36-
c.Set("foo", "bar", cache.DefaultExpiration)
37-
38-
// Set the value of the key "baz" to 42, with no expiration time
39-
// (the item won't be removed until it is re-set, or removed using
40-
// c.Delete("baz")
41-
c.Set("baz", 42, cache.NoExpiration)
42-
43-
// Get the string associated with the key "foo" from the cache
44-
foo, found := c.Get("foo")
45-
if found {
46-
fmt.Println(foo)
47-
}
48-
49-
// Since Go is statically typed, and cache values can be anything, type
50-
// assertion is needed when values are being passed to functions that don't
51-
// take arbitrary types, (i.e. interface{}). The simplest way to do this for
52-
// values which will only be used once--e.g. for passing to another
53-
// function--is:
54-
foo, found := c.Get("foo")
55-
if found {
56-
MyFunction(foo.(string))
57-
}
58-
59-
// This gets tedious if the value is used several times in the same function.
60-
// You might do either of the following instead:
61-
if x, found := c.Get("foo"); found {
62-
foo := x.(string)
63-
// ...
64-
}
65-
// or
66-
var foo string
67-
if x, found := c.Get("foo"); found {
68-
foo = x.(string)
69-
}
70-
// ...
71-
// foo can then be passed around freely as a string
23+
import (
24+
"fmt"
25+
"github.com/patrickmn/go-cache"
26+
"time"
27+
)
28+
29+
func main() {
30+
// Create a cache with a default expiration time of 5 minutes, and which
31+
// purges expired items every 10 minutes
32+
c := cache.New(5*time.Minute, 10*time.Minute)
33+
34+
// Set the value of the key "foo" to "bar", with the default expiration time
35+
c.Set("foo", "bar", cache.DefaultExpiration)
36+
37+
// Set the value of the key "baz" to 42, with no expiration time
38+
// (the item won't be removed until it is re-set, or removed using
39+
// c.Delete("baz")
40+
c.Set("baz", 42, cache.NoExpiration)
41+
42+
// Get the string associated with the key "foo" from the cache
43+
foo, found := c.Get("foo")
44+
if found {
45+
fmt.Println(foo)
46+
}
7247

73-
// Want performance? Store pointers!
74-
c.Set("foo", &MyStruct, cache.DefaultExpiration)
75-
if x, found := c.Get("foo"); found {
76-
foo := x.(*MyStruct)
77-
// ...
78-
}
79-
80-
// If you store a reference type like a pointer, slice, map or channel, you
81-
// do not need to run Set if you modify the underlying data. The cached
82-
// reference points to the same memory, so if you modify a struct whose
83-
// pointer you've stored in the cache, retrieving that pointer with Get will
84-
// point you to the same data:
85-
foo := &MyStruct{Num: 1}
86-
c.Set("foo", foo, cache.DefaultExpiration)
87-
// ...
88-
x, _ := c.Get("foo")
89-
foo := x.(*MyStruct)
90-
fmt.Println(foo.Num)
91-
// ...
92-
foo.Num++
93-
// ...
94-
x, _ := c.Get("foo")
95-
foo := x.(*MyStruct)
96-
foo.Println(foo.Num)
48+
// Since Go is statically typed, and cache values can be anything, type
49+
// assertion is needed when values are being passed to functions that don't
50+
// take arbitrary types, (i.e. interface{}). The simplest way to do this for
51+
// values which will only be used once--e.g. for passing to another
52+
// function--is:
53+
foo, found := c.Get("foo")
54+
if found {
55+
MyFunction(foo.(string))
56+
}
9757

98-
// will print:
99-
// 1
100-
// 2
58+
// This gets tedious if the value is used several times in the same function.
59+
// You might do either of the following instead:
60+
if x, found := c.Get("foo"); found {
61+
foo := x.(string)
62+
// ...
63+
}
64+
// or
65+
var foo string
66+
if x, found := c.Get("foo"); found {
67+
foo = x.(string)
68+
}
69+
// ...
70+
// foo can then be passed around freely as a string
10171

72+
// Want performance? Store pointers!
73+
c.Set("foo", &MyStruct, cache.DefaultExpiration)
74+
if x, found := c.Get("foo"); found {
75+
foo := x.(*MyStruct)
76+
// ...
10277
}
78+
79+
// If you store a reference type like a pointer, slice, map or channel, you
80+
// do not need to run Set if you modify the underlying data. The cached
81+
// reference points to the same memory, so if you modify a struct whose
82+
// pointer you've stored in the cache, retrieving that pointer with Get will
83+
// point you to the same data:
84+
foo := &MyStruct{Num: 1}
85+
c.Set("foo", foo, cache.DefaultExpiration)
86+
// ...
87+
x, _ := c.Get("foo")
88+
foo := x.(*MyStruct)
89+
fmt.Println(foo.Num)
90+
// ...
91+
foo.Num++
92+
// ...
93+
x, _ := c.Get("foo")
94+
foo := x.(*MyStruct)
95+
foo.Println(foo.Num)
96+
97+
// will print:
98+
// 1
99+
// 2
100+
101+
}
103102
```
104103

105104
### Reference

0 commit comments

Comments
 (0)