-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
39 lines (30 loc) · 936 Bytes
/
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
package greenyfy
import (
"bytes"
"appengine"
"appengine/memcache"
)
func getCached(c appengine.Context, key string, missing func(appengine.Context, string) (*bytes.Buffer, error)) (*memcache.Item, error) {
item, err := memcache.Get(c, key)
if err == memcache.ErrCacheMiss {
c.Infof("item not in the cache: %v", key)
result, err := missing(c, key)
if err != nil {
return nil, err
}
item = &memcache.Item{
Key: key,
Value: result.Bytes(),
}
if err := memcache.Add(c, item); err == memcache.ErrNotStored {
c.Warningf("item with key %q already exists", item.Key)
} else if err != nil {
return item, err
}
} else if err != nil {
return item, err
} else {
c.Infof("Cache hit: %v", key)
}
return item, nil
}