now set region in config...or it defaults to something

This commit is contained in:
Tom Gardham-Pallister
2017-06-28 19:02:08 +01:00
parent ab953f28fd
commit 0f60a353ef
19 changed files with 124 additions and 242 deletions

View File

@ -7,7 +7,7 @@ namespace Ocelot.Cache
{
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);
}
}

View File

@ -0,0 +1,9 @@
using Ocelot.Configuration.File;
namespace Ocelot.Cache
{
public interface IRegionCreator
{
string Create(FileReRoute reRoute);
}
}

View File

@ -37,11 +37,11 @@ namespace Ocelot.Cache.Middleware
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)
{
@ -67,9 +67,7 @@ namespace Ocelot.Cache.Middleware
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);
}

View File

@ -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)
@ -34,9 +31,9 @@ namespace Ocelot.Cache
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)

View File

@ -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 region = $"{methods}{reRoute.UpstreamPathTemplate.Value.Replace("/", "")}";
var methods = string.Join("", reRoute.UpstreamHttpMethod.Select(m => m));
var region = $"{methods}{reRoute.UpstreamPathTemplate.Replace("/", "")}";
return region;
}

View File

@ -1,50 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Ocelot.Configuration.Provider;
using Ocelot.Logging;
namespace Ocelot.Cache
{
public interface IRegionsGetter
{
Task<List<string>> Regions();
}
public class RegionsGetter : IRegionsGetter
{
private readonly IOcelotConfigurationProvider _provider;
private readonly IRegionCreator _creator;
private readonly IOcelotLogger _logger;
public RegionsGetter(IOcelotConfigurationProvider provider, IRegionCreator creator, IOcelotLoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<RegionsGetter>();
_provider = provider;
_creator = creator;
}
public async Task<List<string>> Regions()
{
var config = await _provider.Get();
if(config.IsError)
{
_logger.LogError("unable to find regions", new Exception(string.Join(",", config.Errors)));
return new List<string>();
}
var cachedReRoutes = config.Data.ReRoutes.Where(x => x.IsCached);
var regions = new List<string>();
foreach(var reRoute in cachedReRoutes)
{
var region = _creator.Region(reRoute);
regions.Add(region);
}
return regions;
}
}
}

View File

@ -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; }

View File

@ -4,6 +4,7 @@ using System.Text;
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;
@ -36,6 +37,7 @@ namespace Ocelot.Configuration.Creator
private IQoSOptionsCreator _qosOptionsCreator;
private IReRouteOptionsCreator _fileReRouteOptionsCreator;
private IRateLimitOptionsCreator _rateLimitOptionsCreator;
private IRegionCreator _regionCreator;
public FileOcelotConfigurationCreator(
IOptions<FileConfiguration> options,
@ -52,9 +54,11 @@ namespace Ocelot.Configuration.Creator
IServiceProviderConfigurationCreator serviceProviderConfigCreator,
IQoSOptionsCreator qosOptionsCreator,
IReRouteOptionsCreator fileReRouteOptionsCreator,
IRateLimitOptionsCreator rateLimitOptionsCreator
IRateLimitOptionsCreator rateLimitOptionsCreator,
IRegionCreator regionCreator
)
{
_regionCreator = regionCreator;
_rateLimitOptionsCreator = rateLimitOptionsCreator;
_requestIdKeyCreator = requestIdKeyCreator;
_upstreamTemplatePatternCreator = upstreamTemplatePatternCreator;
@ -137,6 +141,8 @@ namespace Ocelot.Configuration.Creator
var rateLimitOption = _rateLimitOptionsCreator.Create(fileReRoute, globalConfiguration, fileReRouteOptions.EnableRateLimiting);
var region = _regionCreator.Create(fileReRoute);
var reRoute = new ReRouteBuilder()
.WithDownstreamPathTemplate(fileReRoute.DownstreamPathTemplate)
.WithUpstreamPathTemplate(fileReRoute.UpstreamPathTemplate)
@ -151,7 +157,7 @@ namespace Ocelot.Configuration.Creator
.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)

View File

@ -3,6 +3,6 @@
public class FileCacheOptions
{
public int TtlSeconds { get; set; }
public string Region {get;private set;}
public string Region {get; set;}
}
}

View File

@ -46,7 +46,7 @@ namespace Ocelot.Configuration
IsAuthorised = isAuthorised;
RequestIdKey = requestIdKey;
IsCached = isCached;
FileCacheOptions = fileCacheOptions;
CacheOptions = fileCacheOptions;
ClaimsToQueries = claimsToQueries
?? new List<ClaimToThing>();
ClaimsToClaims = claimsToClaims
@ -74,7 +74,7 @@ namespace Ocelot.Configuration
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; }

View File

@ -30,7 +30,7 @@ namespace Ocelot.Configuration.Repository
public async Task<Response<IOcelotConfiguration>> Get()
{
var config = _cache.Get(_ocelotConfiguration);
var config = _cache.Get(_ocelotConfiguration, _ocelotConfiguration);
if (config != null)
{
@ -68,7 +68,7 @@ namespace Ocelot.Configuration.Repository
if (result.Response)
{
_cache.AddAndDelete(_ocelotConfiguration, ocelotConfiguration, TimeSpan.FromSeconds(3), "OcelotConfiguration");
_cache.AddAndDelete(_ocelotConfiguration, ocelotConfiguration, TimeSpan.FromSeconds(3), _ocelotConfiguration);
return new OkResponse();
}

View File

@ -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]

View File

@ -145,7 +145,6 @@ namespace Ocelot.DependencyInjection
.AddJsonFormatters();
services.AddLogging();
services.TryAddSingleton<IRegionsGetter, RegionsGetter>();
services.TryAddSingleton<IRegionCreator, RegionCreator>();
services.TryAddSingleton<IFileConfigurationRepository, FileConfigurationRepository>();
services.TryAddSingleton<IFileConfigurationSetter, FileConfigurationSetter>();