diff --git a/db.go b/db.go index 4cffe9cea..03e9e05b4 100644 --- a/db.go +++ b/db.go @@ -278,17 +278,6 @@ func Open(opt Options) (db *DB, err error) { elog = trace.NewEventLog("Badger", "DB") } - config := ristretto.Config{ - // Use 5% of cache memory for storing counters. - NumCounters: int64(float64(opt.MaxCacheSize) * 0.05 * 2), - MaxCost: int64(float64(opt.MaxCacheSize) * 0.95), - BufferItems: 64, - Metrics: true, - } - cache, err := ristretto.NewCache(&config) - if err != nil { - return nil, errors.Wrap(err, "failed to create cache") - } db = &DB{ imm: make([]*skl.Skiplist, 0, opt.NumMemtables), flushChan: make(chan flushTask, opt.NumMemtables), @@ -300,7 +289,20 @@ func Open(opt Options) (db *DB, err error) { valueDirGuard: valueDirLockGuard, orc: newOracle(opt), pub: newPublisher(), - blockCache: cache, + } + + if opt.MaxCacheSize > 0 { + config := ristretto.Config{ + // Use 5% of cache memory for storing counters. + NumCounters: int64(float64(opt.MaxCacheSize) * 0.05 * 2), + MaxCost: int64(float64(opt.MaxCacheSize) * 0.95), + BufferItems: 64, + Metrics: true, + } + db.blockCache, err = ristretto.NewCache(&config) + if err != nil { + return nil, errors.Wrap(err, "failed to create cache") + } } if db.opt.InMemory { @@ -386,7 +388,10 @@ func Open(opt Options) (db *DB, err error) { // CacheMetrics returns the metrics for the underlying cache. func (db *DB) CacheMetrics() *ristretto.Metrics { - return db.blockCache.Metrics + if db.blockCache != nil { + return db.blockCache.Metrics + } + return nil } // 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) { db.lc.nextFileID = 1 db.opt.Infof("Deleted %d value log files. DropAll done.\n", num) db.blockCache.Clear() + return resume, nil } diff --git a/db_test.go b/db_test.go index 56c95de98..41631dab8 100644 --- a/db_test.go +++ b/db_test.go @@ -288,6 +288,13 @@ func TestGet(t *testing.T) { test(t, db) require.NoError(t, db.Close()) }) + t.Run("cache disabled", func(t *testing.T) { + opts := DefaultOptions("").WithInMemory(true).WithMaxCacheSize(0) + db, err := Open(opts) + require.NoError(t, err) + test(t, db) + require.NoError(t, db.Close()) + }) } func TestGetAfterDelete(t *testing.T) { diff --git a/options.go b/options.go index 4374fc39d..f5d795d69 100644 --- a/options.go +++ b/options.go @@ -539,7 +539,8 @@ func (opt Options) WithChecksumVerificationMode(cvMode options.ChecksumVerificat // WithMaxCacheSize returns a new Options value with MaxCacheSize set to the given value. // // This value specifies how much data cache should hold in memory. A small size of cache means lower -// memory consumption and lookups/iterations would take longer. +// memory consumption and lookups/iterations would take longer. Setting size to zero disables the +// cache altogether. func (opt Options) WithMaxCacheSize(size int64) Options { opt.MaxCacheSize = size return opt