From a43c8a9c367a5dbd0535e77568d6885b781306ba Mon Sep 17 00:00:00 2001 From: Anton Evangelatov Date: Tue, 3 Apr 2018 14:52:58 +0200 Subject: [PATCH] swarm/storage: fix setCapacity for 0 --- swarm/storage/memstore_lrucache.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/swarm/storage/memstore_lrucache.go b/swarm/storage/memstore_lrucache.go index 74b6376571..0a6834a3f9 100644 --- a/swarm/storage/memstore_lrucache.go +++ b/swarm/storage/memstore_lrucache.go @@ -31,10 +31,17 @@ const ( type MemStore struct { //cache *lru.ARCCache - cache *lru.Cache + cache *lru.Cache + disabled bool } func NewMemStore(_ *LDBStore, capacity uint) (m *MemStore) { + if capacity == 0 { + return &MemStore{ + disabled: true, + } + } + onEvicted := func(key interface{}, value interface{}) { v := value.(*Chunk) <-v.dbStoredC @@ -51,6 +58,10 @@ func NewMemStore(_ *LDBStore, capacity uint) (m *MemStore) { } func (m *MemStore) Get(key Key) (*Chunk, error) { + if m.disabled { + return nil, ErrChunkNotFound + } + c, ok := m.cache.Get(string(key)) if !ok { return nil, ErrChunkNotFound @@ -63,11 +74,18 @@ func (m *MemStore) Get(key Key) (*Chunk, error) { } func (m *MemStore) Put(c *Chunk) { + if m.disabled { + return + } m.cache.Add(string(c.Key), c) } func (m *MemStore) setCapacity(n int) { - //no-op + if n <= 0 { + m.disabled = true + } else { + m = NewMemStore(nil, uint(n)) + } } // Close memstore