-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
167 lines (145 loc) · 3.83 KB
/
cache.go
File metadata and controls
167 lines (145 loc) · 3.83 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package foon
import (
"bytes"
"context"
"encoding/gob"
"fmt"
"google.golang.org/appengine/memcache"
"time"
)
/** Memcacheを扱う */
type FirestoreCache struct {
context.Context
logger Logger
}
/** キャッシュを取得する際の結果 */
type CacheResult struct {
Key *Key
Src interface{}
HasCache bool
}
func NewCache(ctx context.Context, logger Logger) *FirestoreCache {
return &FirestoreCache{ctx, logger}
}
func (c *FirestoreCache) GetEntity(src interface{}) error {
info, err := KeyError(src)
if err != nil {
return err
}
return c.Get(info, src)
}
func (c *FirestoreCache) Get(info *Key, src interface{}) error {
if info.HasUniqueID() == false {
return InvalidId
}
return c.GetCache(InstanceCache.CreateURIByKey(info).URI(), src)
}
func (c *FirestoreCache) GetCache(path string, src interface{}) error {
c.logger.Trace(fmt.Sprintf("try to get memcache (path: %s)", path))
if cache, err := memcache.Get(c, path); err == nil && cache != nil {
c.logger.Trace(fmt.Sprintf("cache is hit (path: %s)", path))
err := c.asValue(cache.Value, src)
if err != nil {
c.logger.Warning(fmt.Sprintf("failed to get cache (reason: %v)", err))
}
return err
}
return NoSuchDocument
}
func (c *FirestoreCache) GetMulti(results map[string]*CacheResult) error {
c.logger.Trace(fmt.Sprintf("try to get memcaches"))
keys := []string{}
for key, val := range results {
keys = append(keys, key)
val.HasCache = false
}
if caches, err := memcache.GetMulti(c, keys); err == nil {
for _, item := range caches {
if m, ok := results[item.Key]; ok {
c.logger.Trace(fmt.Sprintf("cache is hit (%s)", item.Key))
err := c.asValue(item.Value, m.Src)
if err != nil {
c.logger.Warning(fmt.Sprintf("failed to get cache (reason: %v)", err))
return err
}
m.HasCache = true
}
}
return nil
}
return NoSuchDocument
}
func (c *FirestoreCache) PutEntity(src interface{}) error {
info, err := KeyError(src)
if err != nil {
return err
}
return c.Put(info, src)
}
func (c *FirestoreCache) Put(info *Key, src interface{}) error {
if info.HasUniqueID() == false {
return InvalidId
}
return c.PutCache(InstanceCache.CreateURIByKey(info).URI(), src)
}
func (c *FirestoreCache) PutMulti(results []*KeyAndData) error {
items := []*memcache.Item{}
for _, res := range results {
bytes, err := c.asByte(res.Src)
if err != nil {
return err
}
items = append(items, &memcache.Item{
Key: InstanceCache.CreateURIByKey(res.Key).URI(),
Value: bytes,
Expiration: time.Hour * 24 * 5,
})
}
if len(items) > 0 {
return memcache.SetMulti(c, items)
}
return nil
}
func (c *FirestoreCache) PutCache(path string, src interface{}) error {
bytes, err := c.asByte(src)
if err != nil {
return err
}
tracef(c.logger, "save to memcache (key: %s)", path)
return memcache.Set(c, &memcache.Item{
Key: path,
Value: bytes,
Expiration: time.Hour * 24 * 5,
})
}
func (c *FirestoreCache) Delete(info *Key) error {
if info.HasUniqueID() == false {
return nil
}
url := InstanceCache.CreateURIByKey(info).URI()
c.logger.Trace(fmt.Sprintf("delete cache (key: %s)", url))
return memcache.Delete(c, url)
}
func (c *FirestoreCache) DeleteMulti(keys []*Key) error {
deleteKeys := []string{}
for _, key := range keys {
deleteKeys = append(deleteKeys, InstanceCache.CreateURIByKey(key).URI())
}
return memcache.DeleteMulti(c, deleteKeys)
}
func (c *FirestoreCache) DeleteCache(path string) error {
return memcache.Delete(c, path)
}
func (c *FirestoreCache) asByte(src interface{}) ([]byte, error) {
buf := bytes.NewBuffer(nil)
err := gob.NewEncoder(buf).Encode(src)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (c *FirestoreCache) asValue(data []byte, src interface{}) error {
buf := bytes.NewBuffer(data)
decoder := gob.NewDecoder(buf)
return decoder.Decode(src)
}