Skip to content

Commit 53d7f56

Browse files
committed
Merge branch 'EngRajabi-feature_distributeCache'
2 parents c004c43 + 8b098d2 commit 53d7f56

File tree

5 files changed

+56
-63
lines changed

5 files changed

+56
-63
lines changed

src/Ocelot/Cache/InMemoryCache.cs renamed to src/Ocelot/Cache/AspMemoryCache.cs

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
{
33
using System;
44
using System.Collections.Generic;
5+
using Microsoft.Extensions.Caching.Memory;
56

6-
public class InMemoryCache<T> : IOcelotCache<T>
7+
public class AspMemoryCache<T> : IOcelotCache<T>
78
{
8-
private readonly Dictionary<string, CacheObject<T>> _cache;
9+
private readonly IMemoryCache _memoryCache;
910
private readonly Dictionary<string, List<string>> _regions;
1011

11-
public InMemoryCache()
12+
public AspMemoryCache(IMemoryCache memoryCache)
1213
{
13-
_cache = new Dictionary<string, CacheObject<T>>();
14+
_memoryCache = memoryCache;
1415
_regions = new Dictionary<string, List<string>>();
1516
}
1617

@@ -21,32 +22,19 @@ public void Add(string key, T value, TimeSpan ttl, string region)
2122
return;
2223
}
2324

24-
var expires = DateTime.UtcNow.Add(ttl);
25+
_memoryCache.Set(key, value, ttl);
2526

26-
_cache.Add(key, new CacheObject<T>(value, expires));
27-
28-
if (_regions.ContainsKey(region))
29-
{
30-
var current = _regions[region];
31-
if (!current.Contains(key))
32-
{
33-
current.Add(key);
34-
}
35-
}
36-
else
37-
{
38-
_regions.Add(region, new List<string> { key });
39-
}
27+
SetRegion(region, key);
4028
}
4129

42-
public void AddAndDelete(string key, T value, TimeSpan ttl, string region)
43-
{
44-
if (_cache.ContainsKey(key))
30+
public T Get(string key, string region)
31+
{
32+
if (_memoryCache.TryGetValue(key, out T value))
4533
{
46-
_cache.Remove(key);
34+
return value;
4735
}
4836

49-
Add(key, value, ttl, region);
37+
return default(T);
5038
}
5139

5240
public void ClearRegion(string region)
@@ -56,26 +44,35 @@ public void ClearRegion(string region)
5644
var keys = _regions[region];
5745
foreach (var key in keys)
5846
{
59-
_cache.Remove(key);
47+
_memoryCache.Remove(key);
6048
}
6149
}
6250
}
6351

64-
public T Get(string key, string region)
52+
public void AddAndDelete(string key, T value, TimeSpan ttl, string region)
6553
{
66-
if (_cache.ContainsKey(key))
54+
if (_memoryCache.TryGetValue(key, out T oldValue))
6755
{
68-
var cached = _cache[key];
56+
_memoryCache.Remove(key);
57+
}
58+
59+
Add(key, value, ttl, region);
60+
}
6961

70-
if (cached.Expires > DateTime.UtcNow)
62+
private void SetRegion(string region, string key)
63+
{
64+
if (_regions.ContainsKey(region))
65+
{
66+
var current = _regions[region];
67+
if (!current.Contains(key))
7168
{
72-
return cached.Value;
69+
current.Add(key);
7370
}
74-
75-
_cache.Remove(key);
7671
}
77-
78-
return default(T);
72+
else
73+
{
74+
_regions.Add(region, new List<string> { key });
75+
}
7976
}
8077
}
8178
}

src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,6 @@ private void SetHttpResponseMessageThisRequest(DownstreamContext context,
7777
context.DownstreamResponse = response;
7878
}
7979

80-
private string GenerateRequestCacheKey(DownstreamContext context)
81-
{
82-
string hashedContent = null;
83-
StringBuilder downStreamUrlKeyBuilder = new StringBuilder($"{context.DownstreamRequest.Method}-{context.DownstreamRequest.OriginalString}");
84-
if (context.DownstreamRequest.Content != null)
85-
{
86-
string requestContentString = Task.Run(async () => await context.DownstreamRequest.Content?.ReadAsStringAsync()).Result;
87-
downStreamUrlKeyBuilder.Append(requestContentString);
88-
}
89-
90-
hashedContent = MD5Helper.GenerateMd5(downStreamUrlKeyBuilder.ToString());
91-
return hashedContent;
92-
}
93-
9480
internal DownstreamResponse CreateHttpResponseMessage(CachedResponse cached)
9581
{
9682
if (cached == null)

src/Ocelot/DependencyInjection/OcelotBuilder.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
using Ocelot.ServiceDiscovery.Providers;
2-
3-
using Ocelot.Configuration.ChangeTracking;
4-
51
namespace Ocelot.DependencyInjection
62
{
73
using Microsoft.AspNetCore.Http;
@@ -13,6 +9,8 @@ namespace Ocelot.DependencyInjection
139
using Ocelot.Cache;
1410
using Ocelot.Claims;
1511
using Ocelot.Configuration;
12+
using Ocelot.ServiceDiscovery.Providers;
13+
using Ocelot.Configuration.ChangeTracking;
1614
using Ocelot.Configuration.Creator;
1715
using Ocelot.Configuration.File;
1816
using Ocelot.Configuration.Parser;
@@ -58,8 +56,8 @@ public OcelotBuilder(IServiceCollection services, IConfiguration configurationRo
5856
Services = services;
5957
Services.Configure<FileConfiguration>(configurationRoot);
6058

61-
Services.TryAddSingleton<IOcelotCache<FileConfiguration>, InMemoryCache<FileConfiguration>>();
62-
Services.TryAddSingleton<IOcelotCache<CachedResponse>, InMemoryCache<CachedResponse>>();
59+
Services.TryAddSingleton<IOcelotCache<FileConfiguration>, AspMemoryCache<FileConfiguration>>();
60+
Services.TryAddSingleton<IOcelotCache<CachedResponse>, AspMemoryCache<CachedResponse>>();
6361
Services.TryAddSingleton<IHttpResponseHeaderReplacer, HttpResponseHeaderReplacer>();
6462
Services.TryAddSingleton<IHttpContextRequestHeaderReplacer, HttpContextRequestHeaderReplacer>();
6563
Services.TryAddSingleton<IHeaderFindAndReplaceCreator, HeaderFindAndReplaceCreator>();

test/Ocelot.UnitTests/Cache/InMemoryCacheTests.cs renamed to test/Ocelot.UnitTests/Cache/AspMemoryCacheTests.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
using System;
66
using System.Threading;
77
using Xunit;
8+
using Microsoft.Extensions.Caching.Memory;
89

9-
public class InMemoryCacheTests
10+
public class AspMemoryCacheTests
1011
{
11-
private readonly InMemoryCache<Fake> _cache;
12+
private readonly AspMemoryCache<Fake> _cache;
1213

13-
public InMemoryCacheTests()
14+
public AspMemoryCacheTests()
1415
{
15-
_cache = new InMemoryCache<Fake>();
16+
_cache = new AspMemoryCache<Fake>(new MemoryCache(new MemoryCacheOptions()));
1617
}
1718

1819
[Fact]
@@ -25,6 +26,13 @@ public void should_cache()
2526
fake.Value.ShouldBe(1);
2627
}
2728

29+
[Fact]
30+
public void doesnt_exist()
31+
{
32+
var result = _cache.Get("1", "region");
33+
result.ShouldBeNull();
34+
}
35+
2836
[Fact]
2937
public void should_add_and_delete()
3038
{
@@ -40,11 +48,15 @@ public void should_add_and_delete()
4048
[Fact]
4149
public void should_clear_region()
4250
{
43-
var fake = new Fake(1);
44-
_cache.Add("1", fake, TimeSpan.FromSeconds(100), "region");
51+
var fake1 = new Fake(1);
52+
var fake2 = new Fake(2);
53+
_cache.Add("1", fake1, TimeSpan.FromSeconds(100), "region");
54+
_cache.Add("2", fake2, TimeSpan.FromSeconds(100), "region");
4555
_cache.ClearRegion("region");
46-
var result = _cache.Get("1", "region");
47-
result.ShouldBeNull();
56+
var result1 = _cache.Get("1", "region");
57+
result1.ShouldBeNull();
58+
var result2 = _cache.Get("2", "region");
59+
result2.ShouldBeNull();
4860
}
4961

5062
[Fact]

test/Ocelot.UnitTests/CacheManager/OcelotCacheManagerCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
using global::CacheManager.Core;
44
using Moq;
55
using Ocelot.Cache.CacheManager;
6-
using Shouldly;
76
using System;
7+
using Shouldly;
88
using TestStack.BDDfy;
99
using Xunit;
1010

0 commit comments

Comments
 (0)