-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
118 lines (89 loc) · 2.61 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"bytes"
"fmt"
"reflect"
"time"
"github.com/adelowo/onecache"
"github.com/adelowo/onecache/filesystem"
"github.com/adelowo/onecache/memcached"
"github.com/adelowo/onecache/memory"
"github.com/adelowo/onecache/redis"
"github.com/bradfitz/gomemcache/memcache"
r "github.com/go-redis/redis"
)
//A custom type
//Might want to register this type with encoding/gob (Not really necessary though)
type user struct {
Name string
}
func main() {
marshal := onecache.NewCacheSerializer()
i := &onecache.Item{time.Now().Add(time.Minute * 10), []byte("Lanre")}
fileSystemCache(marshal)
redisStore(marshal, i)
memcachedStore()
inMemoryStore()
}
func inMemoryStore() {
//Setting a larger interval for gc would be better.
//Something like time.Minute * 10
//In other to prevent doing the same thing over and over again
// if nothing really changed
mem := memory.NewInMemoryStore()
fmt.Println(mem.Set("name", []byte("Lanre"), time.Minute*10))
fmt.Println(mem.Get("name"))
fmt.Println(mem.Set("occupation", []byte("What ?"), time.Second))
fmt.Println(mem.Has("occupation"))
fmt.Println(mem.Flush())
fmt.Println(mem.Set("n", []byte("42"), time.Minute*1))
fmt.Println(mem.Get("n"))
fmt.Println(mem.Get("n"))
}
func memcachedStore() {
m := memcached.NewMemcachedStore(
memcache.New("127.0.0.1:11211"),
"",
)
fmt.Println(m.Set("name", []byte("Rob Pike"), onecache.EXPIRES_DEFAULT))
fmt.Println(m.Get("name"))
fmt.Println(m.Flush())
}
func redisStore(marshal *onecache.CacheSerializer, i *onecache.Item) {
opt := &r.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
}
redisStore := redis.NewRedisStore(opt, "")
byt, _ := marshal.Serialize(i)
fmt.Println(redisStore.Set("name", byt, time.Second*10))
val, _ := redisStore.Get("name")
fmt.Println(marshal.Serialize(val))
fmt.Println(redisStore.Flush())
fmt.Println(redisStore.Get("name"))
}
func fileSystemCache(marshal *onecache.CacheSerializer) {
var store onecache.Store
store = filesystem.MustNewFSStore("./onecache_tmp")
err := store.Set("profile", []byte("Lanre"), time.Second*60)
fmt.Println(err)
fmt.Println(store.Get("profile"))
u := &user{"Lanre"}
b, _ := marshal.Serialize(u)
//Handle error
fmt.Println(store.Set("user", b, time.Minute*1))
newB, _ := store.Get("user")
if !bytes.Equal(b, newB) {
panic("OOPS")
}
//Convert back to a user struct
newUser := new(user)
marshal.DeSerialize(newB, newUser)
//Check if the conversion was right
if !reflect.DeepEqual(u, newUser) {
panic("OOPS")
}
fmt.Println(store.Flush())
fmt.Println(store.Get("unkownKey"))
}