Skip to content

Commit 2c55d1f

Browse files
committed
Plumb in diskcache.
1 parent 1f458e5 commit 2c55d1f

File tree

2 files changed

+58
-16
lines changed

2 files changed

+58
-16
lines changed

pkg/chunk/cache/cache.go

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,42 @@ type Cache interface {
2020
type Config struct {
2121
WriteBackGoroutines int
2222
WriteBackBuffer int
23+
EnableDiskcache bool
2324

2425
memcache MemcachedConfig
2526
memcacheClient MemcachedClientConfig
26-
27-
disk DiskcacheConfig
27+
diskcache DiskcacheConfig
2828
}
2929

30+
// RegisterFlags adds the flags required to config this to the given FlagSet.
3031
func (cfg *Config) RegisterFlags(f *flag.FlagSet) {
32+
f.BoolVar(&cfg.EnableDiskcache, "cache.enable-diskcache", false, "Enable on-disk cache")
3133
f.IntVar(&cfg.WriteBackGoroutines, "memcache.write-back-goroutines", 10, "How many goroutines to use to write back to memcache.")
3234
f.IntVar(&cfg.WriteBackBuffer, "memcache.write-back-buffer", 10000, "How many chunks to buffer for background write back.")
3335

3436
cfg.memcache.RegisterFlags(f)
3537
cfg.memcacheClient.RegisterFlags(f)
36-
cfg.disk.RegisterFlags(f)
38+
cfg.diskcache.RegisterFlags(f)
3739
}
3840

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))
4248
}
4349

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
4659
}
4760

4861
type backgroundCache struct {
@@ -58,8 +71,15 @@ type backgroundWrite struct {
5871
buf []byte
5972
}
6073

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+
6281
c := &backgroundCache{
82+
Cache: cache,
6383
quit: make(chan struct{}),
6484
bgWrites: make(chan backgroundWrite, cfg.WriteBackBuffer),
6585
}
@@ -69,7 +89,7 @@ func NewBackground(cfg Config) Cache {
6989
go c.writeBackLoop()
7090
}
7191

72-
return c
92+
return c, nil
7393
}
7494

7595
// Stop the background flushing goroutines.
@@ -80,7 +100,7 @@ func (c *backgroundCache) Stop() error {
80100
return c.Cache.Stop()
81101
}
82102

83-
// BackgroundWrite writes chunks for the cache in the background
103+
// StoreChunk writes chunks for the cache in the background.
84104
func (c *backgroundCache) StoreChunk(ctx context.Context, key string, buf []byte) error {
85105
bgWrite := backgroundWrite{
86106
key: key,
@@ -110,16 +130,33 @@ func (c *backgroundCache) writeBackLoop() {
110130
}
111131
}
112132

113-
type noopCache struct{}
133+
type multiCache []Cache
114134

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+
}
116141
return nil
117142
}
118143

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+
}
120152
return nil, nil, nil
121153
}
122154

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+
}
124161
return nil
125162
}

pkg/chunk/chunk_store.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,16 @@ func NewStore(cfg StoreConfig, schemaCfg SchemaConfig, storage StorageClient) (*
7676
return nil, err
7777
}
7878

79+
cache, err := cache.NewBackground(cfg.CacheConfig)
80+
if err != nil {
81+
return nil, err
82+
}
83+
7984
return &Store{
8085
cfg: cfg,
8186
storage: storage,
8287
schema: schema,
83-
cache: cache.New(cfg.CacheConfig),
88+
cache: cache,
8489
}, nil
8590
}
8691

0 commit comments

Comments
 (0)