@@ -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
@@ -1501,6 +1506,7 @@ func (db *DB) dropAll() (func(), error) {
1501
1506
db .lc .nextFileID = 1
1502
1507
db .opt .Infof ("Deleted %d value log files. DropAll done.\n " , num )
1503
1508
db .blockCache .Clear ()
1509
+
1504
1510
return resume , nil
1505
1511
}
1506
1512
0 commit comments