diff --git a/src/Ocelot/Cache/IOcelotCache.cs b/src/Ocelot/Cache/IOcelotCache.cs index c2054f6e..04ac2b51 100644 --- a/src/Ocelot/Cache/IOcelotCache.cs +++ b/src/Ocelot/Cache/IOcelotCache.cs @@ -1,11 +1,12 @@ -using System; - -namespace Ocelot.Cache -{ - public interface IOcelotCache - { - void Add(string key, T value, TimeSpan ttl, string region); - T Get(string key, string region); - void ClearRegion(string region); - } -} +using System; + +namespace Ocelot.Cache +{ + public interface IOcelotCache + { + void Add(string key, T value, TimeSpan ttl, string region); + T Get(string key, string region); + void ClearRegion(string region); + void AddAndDelete(string key, T value, TimeSpan ttl, string region); + } +} diff --git a/src/Ocelot/Cache/InMemoryCache.cs b/src/Ocelot/Cache/InMemoryCache.cs index fe1c8791..64c1dbc5 100644 --- a/src/Ocelot/Cache/InMemoryCache.cs +++ b/src/Ocelot/Cache/InMemoryCache.cs @@ -39,6 +39,16 @@ } } + public void AddAndDelete(string key, T value, TimeSpan ttl, string region) + { + if (_cache.ContainsKey(key)) + { + _cache.Remove(key); + } + + Add(key, value, ttl, region); + } + public void ClearRegion(string region) { if (_regions.ContainsKey(region)) diff --git a/test/Ocelot.UnitTests/Cache/InMemoryCacheTests.cs b/test/Ocelot.UnitTests/Cache/InMemoryCacheTests.cs index 8b2e58b9..9c17ba3c 100644 --- a/test/Ocelot.UnitTests/Cache/InMemoryCacheTests.cs +++ b/test/Ocelot.UnitTests/Cache/InMemoryCacheTests.cs @@ -25,6 +25,18 @@ fake.Value.ShouldBe(1); } + [Fact] + public void should_add_and_delete() + { + var fake = new Fake(1); + _cache.Add("1", fake, TimeSpan.FromSeconds(100), "region"); + var newFake = new Fake(1); + _cache.AddAndDelete("1", newFake, TimeSpan.FromSeconds(100), "region"); + var result = _cache.Get("1", "region"); + result.ShouldBe(newFake); + newFake.Value.ShouldBe(1); + } + [Fact] public void should_clear_region() {