Skip to content

Commit 8b098d2

Browse files
committed
removed old memory cache and added a few tests for new asp memory cache
1 parent b4c29fc commit 8b098d2

File tree

7 files changed

+36
-203
lines changed

7 files changed

+36
-203
lines changed

src/Ocelot/Cache/AspMemoryCache.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
4-
using Microsoft.Extensions.Caching.Distributed;
5-
using Microsoft.Extensions.Caching.Memory;
6-
7-
namespace Ocelot.Cache
1+
namespace Ocelot.Cache
82
{
3+
using System;
4+
using System.Collections.Generic;
5+
using Microsoft.Extensions.Caching.Memory;
6+
97
public class AspMemoryCache<T> : IOcelotCache<T>
108
{
119
private readonly IMemoryCache _memoryCache;
@@ -20,18 +18,23 @@ public AspMemoryCache(IMemoryCache memoryCache)
2018
public void Add(string key, T value, TimeSpan ttl, string region)
2119
{
2220
if (ttl.TotalMilliseconds <= 0)
21+
{
2322
return;
23+
}
2424

2525
_memoryCache.Set(key, value, ttl);
2626

2727
SetRegion(region, key);
2828
}
2929

3030
public T Get(string key, string region)
31-
{
32-
_memoryCache.TryGetValue<T>(key, out T value);
31+
{
32+
if (_memoryCache.TryGetValue(key, out T value))
33+
{
34+
return value;
35+
}
3336

34-
return value;
37+
return default(T);
3538
}
3639

3740
public void ClearRegion(string region)
@@ -48,8 +51,10 @@ public void ClearRegion(string region)
4851

4952
public void AddAndDelete(string key, T value, TimeSpan ttl, string region)
5053
{
51-
if (_memoryCache.TryGetValue(key, out object oldValue))
54+
if (_memoryCache.TryGetValue(key, out T oldValue))
55+
{
5256
_memoryCache.Remove(key);
57+
}
5358

5459
Add(key, value, ttl, region);
5560
}

src/Ocelot/Cache/InMemoryCache.cs

Lines changed: 0 additions & 81 deletions
This file was deleted.

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: 2 additions & 8 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,12 +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>>();
6359
Services.TryAddSingleton<IOcelotCache<FileConfiguration>, AspMemoryCache<FileConfiguration>>();
6460
Services.TryAddSingleton<IOcelotCache<CachedResponse>, AspMemoryCache<CachedResponse>>();
65-
66-
6761
Services.TryAddSingleton<IHttpResponseHeaderReplacer, HttpResponseHeaderReplacer>();
6862
Services.TryAddSingleton<IHttpContextRequestHeaderReplacer, HttpContextRequestHeaderReplacer>();
6963
Services.TryAddSingleton<IHeaderFindAndReplaceCreator, HeaderFindAndReplaceCreator>();

test/Ocelot.UnitTests/Cache/AspMemoryCacheTests.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
using Microsoft.Extensions.Caching.Memory;
2-
3-
namespace Ocelot.UnitTests.Cache
1+
namespace Ocelot.UnitTests.Cache
42
{
53
using Ocelot.Cache;
64
using Shouldly;
75
using System;
86
using System.Threading;
97
using Xunit;
8+
using Microsoft.Extensions.Caching.Memory;
109

1110
public class AspMemoryCacheTests
1211
{
@@ -27,6 +26,13 @@ public void should_cache()
2726
fake.Value.ShouldBe(1);
2827
}
2928

29+
[Fact]
30+
public void doesnt_exist()
31+
{
32+
var result = _cache.Get("1", "region");
33+
result.ShouldBeNull();
34+
}
35+
3036
[Fact]
3137
public void should_add_and_delete()
3238
{
@@ -42,11 +48,15 @@ public void should_add_and_delete()
4248
[Fact]
4349
public void should_clear_region()
4450
{
45-
var fake = new Fake(1);
46-
_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");
4755
_cache.ClearRegion("region");
48-
var result = _cache.Get("1", "region");
49-
result.ShouldBeNull();
56+
var result1 = _cache.Get("1", "region");
57+
result1.ShouldBeNull();
58+
var result2 = _cache.Get("2", "region");
59+
result2.ShouldBeNull();
5060
}
5161

5262
[Fact]

test/Ocelot.UnitTests/Cache/InMemoryCacheTests.cs

Lines changed: 0 additions & 81 deletions
This file was deleted.

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)