diff --git a/src/Ocelot/Cache/InMemoryCache.cs b/src/Ocelot/Cache/AspMemoryCache.cs similarity index 66% rename from src/Ocelot/Cache/InMemoryCache.cs rename to src/Ocelot/Cache/AspMemoryCache.cs index c0c892ad..6a28dbbf 100644 --- a/src/Ocelot/Cache/InMemoryCache.cs +++ b/src/Ocelot/Cache/AspMemoryCache.cs @@ -2,15 +2,16 @@ { using System; using System.Collections.Generic; + using Microsoft.Extensions.Caching.Memory; - public class InMemoryCache : IOcelotCache + public class AspMemoryCache : IOcelotCache { - private readonly Dictionary> _cache; + private readonly IMemoryCache _memoryCache; private readonly Dictionary> _regions; - public InMemoryCache() + public AspMemoryCache(IMemoryCache memoryCache) { - _cache = new Dictionary>(); + _memoryCache = memoryCache; _regions = new Dictionary>(); } @@ -21,10 +22,45 @@ return; } - var expires = DateTime.UtcNow.Add(ttl); + _memoryCache.Set(key, value, ttl); - _cache.Add(key, new CacheObject(value, expires)); + SetRegion(region, key); + } + public T Get(string key, string region) + { + if (_memoryCache.TryGetValue(key, out T value)) + { + return value; + } + + return default(T); + } + + public void ClearRegion(string region) + { + if (_regions.ContainsKey(region)) + { + var keys = _regions[region]; + foreach (var key in keys) + { + _memoryCache.Remove(key); + } + } + } + + public void AddAndDelete(string key, T value, TimeSpan ttl, string region) + { + if (_memoryCache.TryGetValue(key, out T oldValue)) + { + _memoryCache.Remove(key); + } + + Add(key, value, ttl, region); + } + + private void SetRegion(string region, string key) + { if (_regions.ContainsKey(region)) { var current = _regions[region]; @@ -38,44 +74,5 @@ _regions.Add(region, new List { key }); } } - - 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)) - { - var keys = _regions[region]; - foreach (var key in keys) - { - _cache.Remove(key); - } - } - } - - public T Get(string key, string region) - { - if (_cache.ContainsKey(key)) - { - var cached = _cache[key]; - - if (cached.Expires > DateTime.UtcNow) - { - return cached.Value; - } - - _cache.Remove(key); - } - - return default(T); - } } } diff --git a/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs b/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs index 3d37febc..615332eb 100644 --- a/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs +++ b/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs @@ -77,20 +77,6 @@ context.DownstreamResponse = response; } - private string GenerateRequestCacheKey(DownstreamContext context) - { - string hashedContent = null; - StringBuilder downStreamUrlKeyBuilder = new StringBuilder($"{context.DownstreamRequest.Method}-{context.DownstreamRequest.OriginalString}"); - if (context.DownstreamRequest.Content != null) - { - string requestContentString = Task.Run(async () => await context.DownstreamRequest.Content?.ReadAsStringAsync()).Result; - downStreamUrlKeyBuilder.Append(requestContentString); - } - - hashedContent = MD5Helper.GenerateMd5(downStreamUrlKeyBuilder.ToString()); - return hashedContent; - } - internal DownstreamResponse CreateHttpResponseMessage(CachedResponse cached) { if (cached == null) diff --git a/src/Ocelot/DependencyInjection/OcelotBuilder.cs b/src/Ocelot/DependencyInjection/OcelotBuilder.cs index 9e348183..131fff8f 100644 --- a/src/Ocelot/DependencyInjection/OcelotBuilder.cs +++ b/src/Ocelot/DependencyInjection/OcelotBuilder.cs @@ -1,7 +1,3 @@ -using Ocelot.ServiceDiscovery.Providers; - -using Ocelot.Configuration.ChangeTracking; - namespace Ocelot.DependencyInjection { using Microsoft.AspNetCore.Http; @@ -13,6 +9,8 @@ namespace Ocelot.DependencyInjection using Ocelot.Cache; using Ocelot.Claims; using Ocelot.Configuration; + using Ocelot.ServiceDiscovery.Providers; + using Ocelot.Configuration.ChangeTracking; using Ocelot.Configuration.Creator; using Ocelot.Configuration.File; using Ocelot.Configuration.Parser; @@ -58,8 +56,8 @@ namespace Ocelot.DependencyInjection Services = services; Services.Configure(configurationRoot); - Services.TryAddSingleton, InMemoryCache>(); - Services.TryAddSingleton, InMemoryCache>(); + Services.TryAddSingleton, AspMemoryCache>(); + Services.TryAddSingleton, AspMemoryCache>(); Services.TryAddSingleton(); Services.TryAddSingleton(); Services.TryAddSingleton(); diff --git a/test/Ocelot.UnitTests/Cache/InMemoryCacheTests.cs b/test/Ocelot.UnitTests/Cache/AspMemoryCacheTests.cs similarity index 72% rename from test/Ocelot.UnitTests/Cache/InMemoryCacheTests.cs rename to test/Ocelot.UnitTests/Cache/AspMemoryCacheTests.cs index b9f5b7c0..ae9e1163 100644 --- a/test/Ocelot.UnitTests/Cache/InMemoryCacheTests.cs +++ b/test/Ocelot.UnitTests/Cache/AspMemoryCacheTests.cs @@ -5,14 +5,15 @@ using System; using System.Threading; using Xunit; + using Microsoft.Extensions.Caching.Memory; - public class InMemoryCacheTests + public class AspMemoryCacheTests { - private readonly InMemoryCache _cache; + private readonly AspMemoryCache _cache; - public InMemoryCacheTests() + public AspMemoryCacheTests() { - _cache = new InMemoryCache(); + _cache = new AspMemoryCache(new MemoryCache(new MemoryCacheOptions())); } [Fact] @@ -25,6 +26,13 @@ fake.Value.ShouldBe(1); } + [Fact] + public void doesnt_exist() + { + var result = _cache.Get("1", "region"); + result.ShouldBeNull(); + } + [Fact] public void should_add_and_delete() { @@ -40,11 +48,15 @@ [Fact] public void should_clear_region() { - var fake = new Fake(1); - _cache.Add("1", fake, TimeSpan.FromSeconds(100), "region"); + var fake1 = new Fake(1); + var fake2 = new Fake(2); + _cache.Add("1", fake1, TimeSpan.FromSeconds(100), "region"); + _cache.Add("2", fake2, TimeSpan.FromSeconds(100), "region"); _cache.ClearRegion("region"); - var result = _cache.Get("1", "region"); - result.ShouldBeNull(); + var result1 = _cache.Get("1", "region"); + result1.ShouldBeNull(); + var result2 = _cache.Get("2", "region"); + result2.ShouldBeNull(); } [Fact] diff --git a/test/Ocelot.UnitTests/CacheManager/OcelotCacheManagerCache.cs b/test/Ocelot.UnitTests/CacheManager/OcelotCacheManagerCache.cs index 26432b32..08ec42a8 100644 --- a/test/Ocelot.UnitTests/CacheManager/OcelotCacheManagerCache.cs +++ b/test/Ocelot.UnitTests/CacheManager/OcelotCacheManagerCache.cs @@ -3,8 +3,8 @@ using global::CacheManager.Core; using Moq; using Ocelot.Cache.CacheManager; - using Shouldly; using System; + using Shouldly; using TestStack.BDDfy; using Xunit;