diff --git a/lru_cache/lru_cache.go b/lru_cache/lru_cache.go index f6c4507..b98af6d 100644 --- a/lru_cache/lru_cache.go +++ b/lru_cache/lru_cache.go @@ -126,3 +126,14 @@ func (c *LRUCache) EnlargeCapacity(newCapacity int) error { c.capacity = newCapacity return nil } + +// UpdateValue just update the value of some cache +func (c *LRUCache) UpdateValue (key interface{}, value interface{}) bool { + c.lock.Lock() + defer c.lock.Unlock() + if elem, ok := c.cache[key]; ok { + elem.Value.(*Pair).value = value + return true + } + return false +} \ No newline at end of file diff --git a/lru_cache/lru_cache_test.go b/lru_cache/lru_cache_test.go index 06776c6..739e2c3 100644 --- a/lru_cache/lru_cache_test.go +++ b/lru_cache/lru_cache_test.go @@ -112,3 +112,11 @@ func TestAddAndKeys(t *testing.T) { t.Errorf("Should have same key length") } } + +func TestUpdateValue(t *testing.T) { + cache := prepareCache() + rs := cache.UpdateValue("key6", "") + if !rs { + t.Errorf("'key6' is not in current cache") + } +} \ No newline at end of file