LevelDB: Do not create cache if cache size <= 0.

This commit is contained in:
Sascha L. Teichmann 2014-08-08 10:05:03 +02:00
parent 787ea5d0ee
commit 2e47262ce8

View File

@ -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
}