Skip to content

Commit

Permalink
Merge branch 'EngRajabi-feature_distributeCache'
Browse files Browse the repository at this point in the history
  • Loading branch information
TomPallister committed Apr 13, 2020
2 parents c004c43 + 8b098d2 commit 53d7f56
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
{
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Caching.Memory;

public class InMemoryCache<T> : IOcelotCache<T>
public class AspMemoryCache<T> : IOcelotCache<T>
{
private readonly Dictionary<string, CacheObject<T>> _cache;
private readonly IMemoryCache _memoryCache;
private readonly Dictionary<string, List<string>> _regions;

public InMemoryCache()
public AspMemoryCache(IMemoryCache memoryCache)
{
_cache = new Dictionary<string, CacheObject<T>>();
_memoryCache = memoryCache;
_regions = new Dictionary<string, List<string>>();
}

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

var expires = DateTime.UtcNow.Add(ttl);
_memoryCache.Set(key, value, ttl);

_cache.Add(key, new CacheObject<T>(value, expires));

if (_regions.ContainsKey(region))
{
var current = _regions[region];
if (!current.Contains(key))
{
current.Add(key);
}
}
else
{
_regions.Add(region, new List<string> { key });
}
SetRegion(region, key);
}

public void AddAndDelete(string key, T value, TimeSpan ttl, string region)
{
if (_cache.ContainsKey(key))
public T Get(string key, string region)
{
if (_memoryCache.TryGetValue(key, out T value))
{
_cache.Remove(key);
return value;
}

Add(key, value, ttl, region);
return default(T);
}

public void ClearRegion(string region)
Expand All @@ -56,26 +44,35 @@ public void ClearRegion(string region)
var keys = _regions[region];
foreach (var key in keys)
{
_cache.Remove(key);
_memoryCache.Remove(key);
}
}
}

public T Get(string key, string region)
public void AddAndDelete(string key, T value, TimeSpan ttl, string region)
{
if (_cache.ContainsKey(key))
if (_memoryCache.TryGetValue(key, out T oldValue))
{
var cached = _cache[key];
_memoryCache.Remove(key);
}

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

if (cached.Expires > DateTime.UtcNow)
private void SetRegion(string region, string key)
{
if (_regions.ContainsKey(region))
{
var current = _regions[region];
if (!current.Contains(key))
{
return cached.Value;
current.Add(key);
}

_cache.Remove(key);
}

return default(T);
else
{
_regions.Add(region, new List<string> { key });
}
}
}
}
14 changes: 0 additions & 14 deletions src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,6 @@ private void SetHttpResponseMessageThisRequest(DownstreamContext context,
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)
Expand Down
10 changes: 4 additions & 6 deletions src/Ocelot/DependencyInjection/OcelotBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using Ocelot.ServiceDiscovery.Providers;

using Ocelot.Configuration.ChangeTracking;

namespace Ocelot.DependencyInjection
{
using Microsoft.AspNetCore.Http;
Expand All @@ -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;
Expand Down Expand Up @@ -58,8 +56,8 @@ public OcelotBuilder(IServiceCollection services, IConfiguration configurationRo
Services = services;
Services.Configure<FileConfiguration>(configurationRoot);

Services.TryAddSingleton<IOcelotCache<FileConfiguration>, InMemoryCache<FileConfiguration>>();
Services.TryAddSingleton<IOcelotCache<CachedResponse>, InMemoryCache<CachedResponse>>();
Services.TryAddSingleton<IOcelotCache<FileConfiguration>, AspMemoryCache<FileConfiguration>>();
Services.TryAddSingleton<IOcelotCache<CachedResponse>, AspMemoryCache<CachedResponse>>();
Services.TryAddSingleton<IHttpResponseHeaderReplacer, HttpResponseHeaderReplacer>();
Services.TryAddSingleton<IHttpContextRequestHeaderReplacer, HttpContextRequestHeaderReplacer>();
Services.TryAddSingleton<IHeaderFindAndReplaceCreator, HeaderFindAndReplaceCreator>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Fake> _cache;
private readonly AspMemoryCache<Fake> _cache;

public InMemoryCacheTests()
public AspMemoryCacheTests()
{
_cache = new InMemoryCache<Fake>();
_cache = new AspMemoryCache<Fake>(new MemoryCache(new MemoryCacheOptions()));
}

[Fact]
Expand All @@ -25,6 +26,13 @@ public void should_cache()
fake.Value.ShouldBe(1);
}

[Fact]
public void doesnt_exist()
{
var result = _cache.Get("1", "region");
result.ShouldBeNull();
}

[Fact]
public void should_add_and_delete()
{
Expand All @@ -40,11 +48,15 @@ public void should_add_and_delete()
[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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 53d7f56

Please sign in to comment.