-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_metadata.go
More file actions
141 lines (118 loc) · 3.41 KB
/
cache_metadata.go
File metadata and controls
141 lines (118 loc) · 3.41 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
package foon
import (
"fmt"
"google.golang.org/appengine/memcache"
"time"
)
type IURI interface {
URI() string
}
/** クエリ結果などを保持するためのキャッシュ (関連のPut時には全削除される) */
type CacheMetadata struct {
Item *CacheMetadataItem
cache *FirestoreCache
}
type CacheMetadataItem struct {
MemcachePath string
Data []string
}
type MetadataItem struct {
Key IURI
Data interface{}
}
func LoadGroupMetaData(cache *FirestoreCache, key *Key) *CacheMetadata {
return loadMataData(GroupDataCache.CreateCollectionURIByKey(key).URI(), cache)
}
func LoadMetadata(cache *FirestoreCache, key *Key) *CacheMetadata {
path := MetadataCache.CreateURIByKey(key).URI()
return loadMataData(path, cache)
}
func loadMataData(path string, cache *FirestoreCache) *CacheMetadata {
res := &CacheMetadataItem{}
cache.logger.Trace(fmt.Sprintf("load metadata (key: %s)", path))
err := cache.GetCache(path, res)
if err == nil {
cache.logger.Trace(fmt.Sprintf("metadata cache is hit (%+v)", res.Data))
return &CacheMetadata{res, cache}
} else {
if NoSuchDocument.IsNot(err) {
cache.logger.Warning(fmt.Sprintf("failed to load metadata (reason: %+v)", err))
}
}
res.MemcachePath = path
res.Data = []string{}
return &CacheMetadata{res, cache}
}
func (c *CacheMetadata) Save() error {
return c.cache.PutCache(c.Item.MemcachePath, c.Item)
}
func (c *CacheMetadata) DeleteAll() error {
c.cache.logger.Trace(fmt.Sprintf("delete metadata (%s)", c.Item.MemcachePath))
if len(c.Item.Data) == 0 {
return nil
}
keys := []string{}
for _, path := range c.Item.Data {
c.cache.logger.Trace(fmt.Sprintf("delete metadata cache (%s)", path))
keys = append(keys, path)
}
keys = append(keys, c.Item.MemcachePath)
c.Item.Data = []string{}
return memcache.DeleteMulti(c.cache.Context, keys)
}
func (c *CacheMetadata) Has(key IURI) bool {
for _, name := range c.Item.Data {
if name == key.URI() {
return true
}
}
return false
}
func (c *CacheMetadata) Put(uri IURI, src interface{}) error {
return c.PutMulti([]MetadataItem{MetadataItem{uri, src}})
}
func (c *CacheMetadata) Load(key IURI, src interface{}) error {
c.cache.logger.Trace("try to load Cache.")
if !c.Has(key) {
c.cache.logger.Trace("cache is not registy")
return NoSuchDocument
}
return c.cache.GetCache(key.URI(), src)
}
func (c *CacheMetadata) PutMulti(datas []MetadataItem) error {
strs := []string{}
items := []*memcache.Item{}
for _, data := range datas {
if !c.Has(data.Key) {
c.Item.Data = append(c.Item.Data, data.Key.URI())
}
bytes, err := c.cache.asByte(data.Data)
if err != nil {
c.cache.logger.Warning(fmt.Sprintf("failed to save cache. (reason: %v)", err))
return err
}
strs = append(strs, data.Key.URI())
items = append(items, &memcache.Item{
Key: data.Key.URI(),
Value: bytes,
Expiration: time.Hour * 24 * 5,
})
}
bytes, err := c.cache.asByte(c.Item)
if err != nil {
c.cache.logger.Warning(fmt.Sprintf("failed to save metadata. (reason: %v)", err))
return err
}
strs = append(strs, c.Item.MemcachePath)
items = append(items, &memcache.Item{
Key: c.Item.MemcachePath,
Value: bytes,
Expiration: time.Hour * 24 * 5,
})
c.cache.logger.Trace(fmt.Sprintf("metadata save (%+v)", strs))
err = memcache.SetMulti(c.cache.Context, items)
if err != nil {
c.cache.logger.Warning(fmt.Sprintf("failed to save cache (reason: %v)", err))
}
return err
}