forked from mat/besticon
/
caching.go
93 lines (76 loc) · 1.88 KB
/
caching.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
package besticon
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/golang/groupcache"
)
const contextKeySiteURL = "siteURL"
type result struct {
Icons []Icon
Error string
}
func (b *Besticon) resultFromCache(siteURL string) ([]Icon, error) {
if b.iconCache == nil {
return b.fetchIcons(siteURL)
}
c := context.WithValue(context.Background(), contextKeySiteURL, siteURL)
var data []byte
err := b.iconCache.Get(c, cacheKey(siteURL), groupcache.AllocatingByteSliceSink(&data))
if err != nil {
b.logger.LogError(fmt.Errorf("failed to get icon from cache: %w", err))
return b.fetchIcons(siteURL)
}
res := &result{}
err = json.Unmarshal(data, res)
if err != nil {
panic(err)
}
if res.Error != "" {
return res.Icons, errors.New(res.Error)
}
return res.Icons, nil
}
func cacheKey(siteURL string) string {
// Let results expire after a day
now := time.Now()
return fmt.Sprintf("%d-%02d-%02d-%s", now.Year(), now.Month(), now.Day(), siteURL)
}
func (b *Besticon) generatorFunc(ctx context.Context, key string, sink groupcache.Sink) error {
siteURL := ctx.Value(contextKeySiteURL).(string)
icons, err := b.fetchIcons(siteURL)
if err != nil {
// Don't cache errors
return err
}
res := result{Icons: icons}
if err != nil {
res.Error = err.Error()
}
bytes, err := json.Marshal(res)
if err != nil {
panic(err)
}
sink.SetBytes(bytes)
return nil
}
type cacheOption struct {
size int64
}
func (c *cacheOption) applyOption(b *Besticon) {
b.iconCache = groupcache.NewGroup("icons", c.size<<20, groupcache.GetterFunc(b.generatorFunc))
}
func WithCache(sizeInMB int64) Option {
return &cacheOption{
size: sizeInMB,
}
}
func (b *Besticon) CacheEnabled() bool {
return b.iconCache != nil
}
// GetCacheStats returns cache statistics.
func (b *Besticon) GetCacheStats() groupcache.CacheStats {
return b.iconCache.CacheStats(groupcache.MainCache)
}