From 2e47262ce8a4b391029727c7af96c8f3efc392d7 Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Fri, 8 Aug 2014 10:05:03 +0200 Subject: [PATCH] LevelDB: Do not create cache if cache size <= 0. --- leveldb.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/leveldb.go b/leveldb.go index 07a126a..f1780a6 100644 --- a/leveldb.go +++ b/leveldb.go @@ -23,13 +23,21 @@ type LevelDBSession struct { } func NewLeveDBBackend(path string, cacheSize int) (ldb *LevelDBBackend, err error) { - cache := leveldb.NewLRUCache(cacheSize * 1024 * 1024) opts := leveldb.NewOptions() - opts.SetCache(cache) + + var cache *leveldb.Cache + if cacheSize > 0 { + cache = leveldb.NewLRUCache(cacheSize * 1024 * 1024) + opts.SetCache(cache) + } + opts.SetCreateIfMissing(true) + var db *leveldb.DB if db, err = leveldb.Open(path, opts); err != nil { - cache.Close() + if cache != nil { + cache.Close() + } return } ldb = &LevelDBBackend{ @@ -63,7 +71,9 @@ func (ldbs *LevelDBSession) Close() error { func (ldb *LevelDBBackend) Shutdown() error { ldb.db.Close() - ldb.cache.Close() + if ldb.cache != nil { + ldb.cache.Close() + } return nil }