Skip to content

Commit 7d160ff

Browse files
damzIbrahim Jarif
authored and
Ibrahim Jarif
committed
Support disabling the cache completely. (#1183) (#1185)
The cache can be disabled by setting `opt.MaxCacheSize=0` (cherry picked from commit 7e5a956)
1 parent 119a431 commit 7d160ff

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

db.go

Lines changed: 19 additions & 13 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
@@ -1501,6 +1506,7 @@ func (db *DB) dropAll() (func(), error) {
15011506
db.lc.nextFileID = 1
15021507
db.opt.Infof("Deleted %d value log files. DropAll done.\n", num)
15031508
db.blockCache.Clear()
1509+
15041510
return resume, nil
15051511
}
15061512

db_test.go

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

293300
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)