Skip to content

Commit

Permalink
Support disabling the cache completely. (#1183) (#1185)
Browse files Browse the repository at this point in the history
The cache can be disabled by setting `opt.MaxCacheSize=0`

(cherry picked from commit 7e5a956)
  • Loading branch information
damz authored and Ibrahim Jarif committed Mar 5, 2020
1 parent b70fcaf commit c60978f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
32 changes: 19 additions & 13 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
7 changes: 7 additions & 0 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c60978f

Please sign in to comment.