diff --git a/util/cache.cc b/util/cache.cc index ce46886..97b82ea 100644 --- a/util/cache.cc +++ b/util/cache.cc @@ -288,7 +288,10 @@ Cache::Handle* LRUCache::Insert( LRU_Append(&in_use_, e); usage_ += charge; FinishErase(table_.Insert(e)); - } // else don't cache. (Tests use capacity_==0 to turn off caching.) + } else { + // don't cache. (It is valid to set capacity_==0 to turn off caching.) + e->next = NULL; + } while (usage_ > capacity_ && lru_.next != &lru_) { LRUHandle* old = lru_.next; diff --git a/util/cache_test.cc b/util/cache_test.cc index 468f7a6..246ab8e 100644 --- a/util/cache_test.cc +++ b/util/cache_test.cc @@ -219,6 +219,14 @@ TEST(CacheTest, Prune) { ASSERT_EQ(-1, Lookup(2)); } +TEST(CacheTest, ZeroSizeCache) { + delete cache_; + cache_ = NewLRUCache(0); + + Insert(1, 100); + ASSERT_EQ(-1, Lookup(1)); +} + } // namespace leveldb int main(int argc, char** argv) {