@@ -278,17 +278,6 @@ func Open(opt Options) (db *DB, err error) {
278
278
elog = trace .NewEventLog ("Badger" , "DB" )
279
279
}
280
280
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
- }
292
281
db = & DB {
293
282
imm : make ([]* skl.Skiplist , 0 , opt .NumMemtables ),
294
283
flushChan : make (chan flushTask , opt .NumMemtables ),
@@ -300,7 +289,20 @@ func Open(opt Options) (db *DB, err error) {
300
289
valueDirGuard : valueDirLockGuard ,
301
290
orc : newOracle (opt ),
302
291
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
+ }
304
306
}
305
307
306
308
if db .opt .InMemory {
@@ -386,7 +388,10 @@ func Open(opt Options) (db *DB, err error) {
386
388
387
389
// CacheMetrics returns the metrics for the underlying cache.
388
390
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
390
395
}
391
396
392
397
// 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) {
477
482
db .elog .Printf ("Waiting for closer" )
478
483
db .closers .updateSize .SignalAndWait ()
479
484
db .orc .Stop ()
480
- db .blockCache .Close ()
485
+ if db .blockCache != nil {
486
+ db .blockCache .Close ()
487
+ }
481
488
482
489
db .elog .Finish ()
483
490
if db .opt .InMemory {
@@ -1500,7 +1507,9 @@ func (db *DB) dropAll() (func(), error) {
1500
1507
db .vhead = valuePointer {} // Zero it out.
1501
1508
db .lc .nextFileID = 1
1502
1509
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
+ }
1504
1513
return resume , nil
1505
1514
}
1506
1515
0 commit comments