-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_for_cache.go
65 lines (60 loc) · 1.3 KB
/
sample_for_cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"fmt"
"go-libcache/cache"
"time"
)
func main() {
defaultExpiration, _ := time.ParseDuration("0.2h")
gcInterval, _ := time.ParseDuration("10s")
cacher, err := cache.NewCache(defaultExpiration, gcInterval)
if err != nil {
fmt.Println("error: ", err)
return
}
key1, err := cacher.SetKey("hello World")
if err != nil {
fmt.Println(err)
return
}
value1 := "Hello world"
expiration, _ := time.ParseDuration("6s")
cacher.Set(key1, value1, expiration)
if val, found, err := cacher.Get(key1); err == nil {
if found {
fmt.Println("Found: ", val)
} else {
fmt.Println("Not found item.")
}
} else {
fmt.Println("Error: ", err)
}
cacher.GetCacheStat()
/*time.Sleep(time.Second * 10)
if val, found := cacher.Get("key1"); found {
fmt.Println("Found: ", val)
} else {
fmt.Println("Not found item.")
}*/
err = cacher.SaveMemToFile("/home/xj/Desktop/mycache.dat")
if err != nil {
fmt.Println("Error: ", err)
return
}
time.Sleep(time.Second * 3)
err = cacher.LoadFileToMem("/home/xj/Desktop/mycache.dat")
if err != nil {
fmt.Println("Error: ", err)
return
}
if val, found, err := cacher.Get(key1); err == nil {
if found {
fmt.Println("Found: ", val)
} else {
fmt.Println("Not found item.")
}
} else {
fmt.Println("Error: ", err)
}
return
}