Skip to content

Commit

Permalink
now set region in config...or it defaults to something
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Gardham-Pallister committed Jun 28, 2017
1 parent ab953f2 commit 0f60a35
Show file tree
Hide file tree
Showing 19 changed files with 124 additions and 242 deletions.
2 changes: 1 addition & 1 deletion src/Ocelot/Cache/IOcelotCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface IOcelotCache<T>
{
void Add(string key, T value, TimeSpan ttl, string region);
void AddAndDelete(string key, T value, TimeSpan ttl, string region);
T Get(string key);
T Get(string key, string region);
void ClearRegion(string region);
}
}
9 changes: 9 additions & 0 deletions src/Ocelot/Cache/IRegionCreator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Ocelot.Configuration.File;

namespace Ocelot.Cache
{
public interface IRegionCreator
{
string Create(FileReRoute reRoute);
}
}
8 changes: 3 additions & 5 deletions src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public async Task Invoke(HttpContext context)
return;
}

var downstreamUrlKey = DownstreamRequest.RequestUri.OriginalString;
var downstreamUrlKey = $"{DownstreamRequest.Method.Method}-{DownstreamRequest.RequestUri.OriginalString}";

_logger.LogDebug("started checking cache for {downstreamUrlKey}", downstreamUrlKey);

var cached = _outputCache.Get(downstreamUrlKey);
var cached = _outputCache.Get(downstreamUrlKey, DownstreamRoute.ReRoute.CacheOptions.Region);

if (cached != null)
{
Expand All @@ -67,9 +67,7 @@ public async Task Invoke(HttpContext context)

var response = HttpResponseMessage;

var region = _regionCreator.Region(DownstreamRoute.ReRoute);

_outputCache.Add(downstreamUrlKey, response, TimeSpan.FromSeconds(DownstreamRoute.ReRoute.FileCacheOptions.TtlSeconds), region);
_outputCache.Add(downstreamUrlKey, response, TimeSpan.FromSeconds(DownstreamRoute.ReRoute.CacheOptions.TtlSeconds), DownstreamRoute.ReRoute.CacheOptions.Region);

_logger.LogDebug("finished response added to cache for {downstreamUrlKey}", downstreamUrlKey);
}
Expand Down
7 changes: 2 additions & 5 deletions src/Ocelot/Cache/OcelotCacheManagerCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@ namespace Ocelot.Cache
public class OcelotCacheManagerCache<T> : IOcelotCache<T>
{
private readonly ICacheManager<T> _cacheManager;
private HashSet<string> _keys;

public OcelotCacheManagerCache(ICacheManager<T> cacheManager)
{
_cacheManager = cacheManager;
_keys = new HashSet<string>();
}

public void Add(string key, T value, TimeSpan ttl, string region)
{
_cacheManager.Add(new CacheItem<T>(key, region, value, ExpirationMode.Absolute, ttl));
_keys.Add(key);
}

public void AddAndDelete(string key, T value, TimeSpan ttl, string region)
Expand All @@ -34,9 +31,9 @@ public void AddAndDelete(string key, T value, TimeSpan ttl, string region)
Add(key, value, ttl, region);
}

public T Get(string key)
public T Get(string key, string region)
{
return _cacheManager.Get<T>(key);
return _cacheManager.Get<T>(key, region);
}

public void ClearRegion(string region)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
using System.Linq;
using Ocelot.Configuration;
using Ocelot.Configuration.File;

namespace Ocelot.Cache
{
public interface IRegionCreator
{
string Region(ReRoute reRoute);
}

public class RegionCreator : IRegionCreator
{
public string Region(ReRoute reRoute)
public string Create(FileReRoute reRoute)
{
var methods = string.Join("", reRoute.UpstreamHttpMethod.Select(m => m.Method));
if(!string.IsNullOrEmpty(reRoute?.FileCacheOptions?.Region))
{
return reRoute?.FileCacheOptions?.Region;
}

var methods = string.Join("", reRoute.UpstreamHttpMethod.Select(m => m));

var region = $"{methods}{reRoute.UpstreamPathTemplate.Value.Replace("/", "")}";
var region = $"{methods}{reRoute.UpstreamPathTemplate.Replace("/", "")}";

return region;
}
Expand Down
50 changes: 0 additions & 50 deletions src/Ocelot/Cache/RegionsGetter.cs

This file was deleted.

3 changes: 2 additions & 1 deletion src/Ocelot/Configuration/CacheOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
{
public class CacheOptions
{
public CacheOptions(int ttlSeconds)
public CacheOptions(int ttlSeconds, string region)
{
TtlSeconds = ttlSeconds;
Region = region;
}

public int TtlSeconds { get; private set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Ocelot.Cache;
using Ocelot.Configuration.Builder;
using Ocelot.Configuration.File;
using Ocelot.Configuration.Parser;
Expand Down Expand Up @@ -36,6 +37,7 @@ public class FileOcelotConfigurationCreator : IOcelotConfigurationCreator
private IQoSOptionsCreator _qosOptionsCreator;
private IReRouteOptionsCreator _fileReRouteOptionsCreator;
private IRateLimitOptionsCreator _rateLimitOptionsCreator;
private IRegionCreator _regionCreator;

public FileOcelotConfigurationCreator(
IOptions<FileConfiguration> options,
Expand All @@ -52,9 +54,11 @@ public FileOcelotConfigurationCreator(
IServiceProviderConfigurationCreator serviceProviderConfigCreator,
IQoSOptionsCreator qosOptionsCreator,
IReRouteOptionsCreator fileReRouteOptionsCreator,
IRateLimitOptionsCreator rateLimitOptionsCreator
IRateLimitOptionsCreator rateLimitOptionsCreator,
IRegionCreator regionCreator
)
{
_regionCreator = regionCreator;
_rateLimitOptionsCreator = rateLimitOptionsCreator;
_requestIdKeyCreator = requestIdKeyCreator;
_upstreamTemplatePatternCreator = upstreamTemplatePatternCreator;
Expand Down Expand Up @@ -137,6 +141,8 @@ private async Task<ReRoute> SetUpReRoute(FileReRoute fileReRoute, FileGlobalConf

var rateLimitOption = _rateLimitOptionsCreator.Create(fileReRoute, globalConfiguration, fileReRouteOptions.EnableRateLimiting);

var region = _regionCreator.Create(fileReRoute);

var reRoute = new ReRouteBuilder()
.WithDownstreamPathTemplate(fileReRoute.DownstreamPathTemplate)
.WithUpstreamPathTemplate(fileReRoute.UpstreamPathTemplate)
Expand All @@ -151,7 +157,7 @@ private async Task<ReRoute> SetUpReRoute(FileReRoute fileReRoute, FileGlobalConf
.WithClaimsToQueries(claimsToQueries)
.WithRequestIdKey(requestIdKey)
.WithIsCached(fileReRouteOptions.IsCached)
.WithCacheOptions(new CacheOptions(fileReRoute.FileCacheOptions.TtlSeconds))
.WithCacheOptions(new CacheOptions(fileReRoute.FileCacheOptions.TtlSeconds, region))
.WithDownstreamScheme(fileReRoute.DownstreamScheme)
.WithLoadBalancer(fileReRoute.LoadBalancer)
.WithDownstreamHost(fileReRoute.DownstreamHost)
Expand Down
2 changes: 1 addition & 1 deletion src/Ocelot/Configuration/File/FileCacheOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
public class FileCacheOptions
{
public int TtlSeconds { get; set; }
public string Region {get;private set;}
public string Region {get; set;}
}
}
4 changes: 2 additions & 2 deletions src/Ocelot/Configuration/ReRoute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public ReRoute(PathTemplate downstreamPathTemplate,
IsAuthorised = isAuthorised;
RequestIdKey = requestIdKey;
IsCached = isCached;
FileCacheOptions = fileCacheOptions;
CacheOptions = fileCacheOptions;
ClaimsToQueries = claimsToQueries
?? new List<ClaimToThing>();
ClaimsToClaims = claimsToClaims
Expand Down Expand Up @@ -74,7 +74,7 @@ public ReRoute(PathTemplate downstreamPathTemplate,
public Dictionary<string, string> RouteClaimsRequirement { get; private set; }
public string RequestIdKey { get; private set; }
public bool IsCached { get; private set; }
public CacheOptions FileCacheOptions { get; private set; }
public CacheOptions CacheOptions { get; private set; }
public string DownstreamScheme {get;private set;}
public bool IsQos { get; private set; }
public QoSOptions QosOptionsOptions { get; private set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ConsulOcelotConfigurationRepository(ConsulRegistryConfiguration consulReg

public async Task<Response<IOcelotConfiguration>> Get()
{
var config = _cache.Get(_ocelotConfiguration);
var config = _cache.Get(_ocelotConfiguration, _ocelotConfiguration);

if (config != null)
{
Expand Down Expand Up @@ -68,7 +68,7 @@ public async Task<Response> AddOrReplace(IOcelotConfiguration ocelotConfiguratio

if (result.Response)
{
_cache.AddAndDelete(_ocelotConfiguration, ocelotConfiguration, TimeSpan.FromSeconds(3), "OcelotConfiguration");
_cache.AddAndDelete(_ocelotConfiguration, ocelotConfiguration, TimeSpan.FromSeconds(3), _ocelotConfiguration);

return new OkResponse();
}
Expand Down
11 changes: 1 addition & 10 deletions src/Ocelot/Controllers/OutputCacheController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,10 @@ namespace Ocelot.Controllers
public class OutputCacheController : Controller
{
private IOcelotCache<HttpResponseMessage> _cache;
private IRegionsGetter _regionsGetter;

public OutputCacheController(IOcelotCache<HttpResponseMessage> cache, IRegionsGetter regionsGetter)
public OutputCacheController(IOcelotCache<HttpResponseMessage> cache)
{
_cache = cache;
_regionsGetter = regionsGetter;
}

[HttpGet]
public async Task<IActionResult> Get()
{
var regions = await _regionsGetter.Regions();
return new OkObjectResult(new Regions(regions));
}

[HttpDelete]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ public static IServiceCollection AddOcelot(this IServiceCollection services, ICo
.AddJsonFormatters();

services.AddLogging();
services.TryAddSingleton<IRegionsGetter, RegionsGetter>();
services.TryAddSingleton<IRegionCreator, RegionCreator>();
services.TryAddSingleton<IFileConfigurationRepository, FileConfigurationRepository>();
services.TryAddSingleton<IFileConfigurationSetter, FileConfigurationSetter>();
Expand Down
10 changes: 6 additions & 4 deletions test/Ocelot.UnitTests/Cache/CacheManagerCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class CacheManagerCacheTests
private string _resultGet;
private TimeSpan _ttlSeconds;
private List<string> _resultKeys;
private string _region;

public CacheManagerCacheTests()
{
Expand All @@ -28,7 +29,7 @@ public CacheManagerCacheTests()
[Fact]
public void should_get_from_cache()
{
this.Given(x => x.GivenTheFollowingIsCached("someKey", "someValue"))
this.Given(x => x.GivenTheFollowingIsCached("someKey", "someRegion", "someValue"))
.When(x => x.WhenIGetFromTheCache())
.Then(x => x.ThenTheResultIs("someValue"))
.BDDfy();
Expand Down Expand Up @@ -88,15 +89,16 @@ private void ThenTheResultIs(string expected)

private void WhenIGetFromTheCache()
{
_resultGet = _ocelotOcelotCacheManager.Get(_key);
_resultGet = _ocelotOcelotCacheManager.Get(_key, _region);
}

private void GivenTheFollowingIsCached(string key, string value)
private void GivenTheFollowingIsCached(string key, string region, string value)
{
_key = key;
_value = value;
_region = region;
_mockCacheManager
.Setup(x => x.Get<string>(It.IsAny<string>()))
.Setup(x => x.Get<string>(It.IsAny<string>(), It.IsAny<string>()))
.Returns(value);
}
}
Expand Down
6 changes: 3 additions & 3 deletions test/Ocelot.UnitTests/Cache/OutputCacheMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private void GivenTheDownstreamRouteIs()
{
var reRoute = new ReRouteBuilder()
.WithIsCached(true)
.WithCacheOptions(new CacheOptions(100))
.WithCacheOptions(new CacheOptions(100, "kanken"))
.WithUpstreamHttpMethod(new List<string> { "Get" })
.Build();

Expand Down Expand Up @@ -120,7 +120,7 @@ private void GivenThereIsADownstreamUrl()
private void ThenTheCacheGetIsCalledCorrectly()
{
_cacheManager
.Verify(x => x.Get(It.IsAny<string>()), Times.Once);
.Verify(x => x.Get(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}

private void ThenTheCacheAddIsCalledCorrectly()
Expand All @@ -140,7 +140,7 @@ private void GivenThereIsACachedResponse(HttpResponseMessage response)
{
_response = response;
_cacheManager
.Setup(x => x.Get(It.IsAny<string>()))
.Setup(x => x.Get(It.IsAny<string>(), It.IsAny<string>()))
.Returns(_response);
}

Expand Down
Loading

0 comments on commit 0f60a35

Please sign in to comment.