@@ -20,29 +20,42 @@ type Cache interface {
20
20
type Config struct {
21
21
WriteBackGoroutines int
22
22
WriteBackBuffer int
23
+ EnableDiskcache bool
23
24
24
25
memcache MemcachedConfig
25
26
memcacheClient MemcachedClientConfig
26
-
27
- disk DiskcacheConfig
27
+ diskcache DiskcacheConfig
28
28
}
29
29
30
+ // RegisterFlags adds the flags required to config this to the given FlagSet.
30
31
func (cfg * Config ) RegisterFlags (f * flag.FlagSet ) {
32
+ f .BoolVar (& cfg .EnableDiskcache , "cache.enable-diskcache" , false , "Enable on-disk cache" )
31
33
f .IntVar (& cfg .WriteBackGoroutines , "memcache.write-back-goroutines" , 10 , "How many goroutines to use to write back to memcache." )
32
34
f .IntVar (& cfg .WriteBackBuffer , "memcache.write-back-buffer" , 10000 , "How many chunks to buffer for background write back." )
33
35
34
36
cfg .memcache .RegisterFlags (f )
35
37
cfg .memcacheClient .RegisterFlags (f )
36
- cfg .disk .RegisterFlags (f )
38
+ cfg .diskcache .RegisterFlags (f )
37
39
}
38
40
39
- func New (cfg Config ) Cache {
40
- if cfg .memcacheClient .Host == "" {
41
- return noopCache {}
41
+ // New creates a new Cache using Config.
42
+ func New (cfg Config ) (Cache , error ) {
43
+ caches := []Cache {}
44
+
45
+ if cfg .memcacheClient .Host != "" {
46
+ client := newMemcachedClient (cfg .memcacheClient )
47
+ caches = append (caches , NewMemcached (cfg .memcache , client ))
42
48
}
43
49
44
- client := newMemcachedClient (cfg .memcacheClient )
45
- return NewMemcached (cfg .memcache , client )
50
+ if cfg .EnableDiskcache {
51
+ cache , err := NewDiskcache (cfg .diskcache )
52
+ if err != nil {
53
+ return nil , err
54
+ }
55
+ caches = append (caches , cache )
56
+ }
57
+
58
+ return multiCache (caches ), nil
46
59
}
47
60
48
61
type backgroundCache struct {
@@ -58,8 +71,15 @@ type backgroundWrite struct {
58
71
buf []byte
59
72
}
60
73
61
- func NewBackground (cfg Config ) Cache {
74
+ // NewBackground returns a new Cache that does stores on background goroutines.
75
+ func NewBackground (cfg Config ) (Cache , error ) {
76
+ cache , err := New (cfg )
77
+ if err != nil {
78
+ return nil , err
79
+ }
80
+
62
81
c := & backgroundCache {
82
+ Cache : cache ,
63
83
quit : make (chan struct {}),
64
84
bgWrites : make (chan backgroundWrite , cfg .WriteBackBuffer ),
65
85
}
@@ -69,7 +89,7 @@ func NewBackground(cfg Config) Cache {
69
89
go c .writeBackLoop ()
70
90
}
71
91
72
- return c
92
+ return c , nil
73
93
}
74
94
75
95
// Stop the background flushing goroutines.
@@ -80,7 +100,7 @@ func (c *backgroundCache) Stop() error {
80
100
return c .Cache .Stop ()
81
101
}
82
102
83
- // BackgroundWrite writes chunks for the cache in the background
103
+ // StoreChunk writes chunks for the cache in the background.
84
104
func (c * backgroundCache ) StoreChunk (ctx context.Context , key string , buf []byte ) error {
85
105
bgWrite := backgroundWrite {
86
106
key : key ,
@@ -110,16 +130,33 @@ func (c *backgroundCache) writeBackLoop() {
110
130
}
111
131
}
112
132
113
- type noopCache struct {}
133
+ type multiCache [] Cache
114
134
115
- func (noopCache ) StoreChunk (ctx context.Context , key string , buf []byte ) error {
135
+ func (m multiCache ) StoreChunk (ctx context.Context , key string , buf []byte ) error {
136
+ for _ , c := range []Cache (m ) {
137
+ if err := c .StoreChunk (ctx , key , buf ); err != nil {
138
+ return err
139
+ }
140
+ }
116
141
return nil
117
142
}
118
143
119
- func (noopCache ) FetchChunkData (ctx context.Context , keys []string ) (found []string , bufs [][]byte , err error ) {
144
+ func (m multiCache ) FetchChunkData (ctx context.Context , keys []string ) ([]string , [][]byte , error ) {
145
+ for _ , c := range []Cache (m ) {
146
+ found , bufs , err := c .FetchChunkData (ctx , keys )
147
+ if err != nil {
148
+ return nil , nil , err
149
+ }
150
+ return found , bufs , nil
151
+ }
120
152
return nil , nil , nil
121
153
}
122
154
123
- func (noopCache ) Stop () error {
155
+ func (m multiCache ) Stop () error {
156
+ for _ , c := range []Cache (m ) {
157
+ if err := c .Stop (); err != nil {
158
+ return err
159
+ }
160
+ }
124
161
return nil
125
162
}
0 commit comments