Skip to content

Commit bfe482d

Browse files
committed
Support disabling the cache completely. (#1183)
1 parent 0f2e629 commit bfe482d

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

db.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -278,17 +278,6 @@ func Open(opt Options) (db *DB, err error) {
278278
elog = trace.NewEventLog("Badger", "DB")
279279
}
280280

281-
config := ristretto.Config{
282-
// Use 5% of cache memory for storing counters.
283-
NumCounters: int64(float64(opt.MaxCacheSize) * 0.05 * 2),
284-
MaxCost: int64(float64(opt.MaxCacheSize) * 0.95),
285-
BufferItems: 64,
286-
Metrics: true,
287-
}
288-
cache, err := ristretto.NewCache(&config)
289-
if err != nil {
290-
return nil, errors.Wrap(err, "failed to create cache")
291-
}
292281
db = &DB{
293282
imm: make([]*skl.Skiplist, 0, opt.NumMemtables),
294283
flushChan: make(chan flushTask, opt.NumMemtables),
@@ -300,7 +289,20 @@ func Open(opt Options) (db *DB, err error) {
300289
valueDirGuard: valueDirLockGuard,
301290
orc: newOracle(opt),
302291
pub: newPublisher(),
303-
blockCache: cache,
292+
}
293+
294+
if opt.MaxCacheSize > 0 {
295+
config := ristretto.Config{
296+
// Use 5% of cache memory for storing counters.
297+
NumCounters: int64(float64(opt.MaxCacheSize) * 0.05 * 2),
298+
MaxCost: int64(float64(opt.MaxCacheSize) * 0.95),
299+
BufferItems: 64,
300+
Metrics: true,
301+
}
302+
db.blockCache, err = ristretto.NewCache(&config)
303+
if err != nil {
304+
return nil, errors.Wrap(err, "failed to create cache")
305+
}
304306
}
305307

306308
if db.opt.InMemory {
@@ -386,7 +388,10 @@ func Open(opt Options) (db *DB, err error) {
386388

387389
// CacheMetrics returns the metrics for the underlying cache.
388390
func (db *DB) CacheMetrics() *ristretto.Metrics {
389-
return db.blockCache.Metrics
391+
if db.blockCache != nil {
392+
return db.blockCache.Metrics
393+
}
394+
return nil
390395
}
391396

392397
// Close closes a DB. It's crucial to call it to ensure all the pending updates make their way to
@@ -477,7 +482,9 @@ func (db *DB) close() (err error) {
477482
db.elog.Printf("Waiting for closer")
478483
db.closers.updateSize.SignalAndWait()
479484
db.orc.Stop()
480-
db.blockCache.Close()
485+
if db.blockCache != nil {
486+
db.blockCache.Close()
487+
}
481488

482489
db.elog.Finish()
483490
if db.opt.InMemory {
@@ -1500,7 +1507,9 @@ func (db *DB) dropAll() (func(), error) {
15001507
db.vhead = valuePointer{} // Zero it out.
15011508
db.lc.nextFileID = 1
15021509
db.opt.Infof("Deleted %d value log files. DropAll done.\n", num)
1503-
db.blockCache.Clear()
1510+
if db.blockCache != nil {
1511+
db.blockCache.Clear()
1512+
}
15041513
return resume, nil
15051514
}
15061515

db_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,13 @@ func TestGet(t *testing.T) {
289289
test(t, db)
290290
require.NoError(t, db.Close())
291291
})
292+
t.Run("cache disabled", func(t *testing.T) {
293+
opts := DefaultOptions("").WithInMemory(true).WithMaxCacheSize(0)
294+
db, err := Open(opts)
295+
require.NoError(t, err)
296+
test(t, db)
297+
require.NoError(t, db.Close())
298+
})
292299
}
293300

294301
func TestGetAfterDelete(t *testing.T) {

options.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,8 @@ func (opt Options) WithChecksumVerificationMode(cvMode options.ChecksumVerificat
539539
// WithMaxCacheSize returns a new Options value with MaxCacheSize set to the given value.
540540
//
541541
// This value specifies how much data cache should hold in memory. A small size of cache means lower
542-
// memory consumption and lookups/iterations would take longer.
542+
// memory consumption and lookups/iterations would take longer. Setting size to zero disables the
543+
// cache altogether.
543544
func (opt Options) WithMaxCacheSize(size int64) Options {
544545
opt.MaxCacheSize = size
545546
return opt

0 commit comments

Comments
 (0)