working on region clearing cache, if using cachemanager back plance this would clear all servers in cluster

This commit is contained in:
Tom Gardham-Pallister
2017-06-26 19:10:20 +01:00
parent 26e7621798
commit 239dcfb6bd
8 changed files with 155 additions and 12 deletions

View File

@ -1,11 +1,13 @@
using System;
using System.Collections.Generic;
namespace Ocelot.Cache
{
public interface IOcelotCache<T>
{
void Add(string key, T value, TimeSpan ttl);
void AddAndDelete(string key, T value, TimeSpan ttl);
void Add(string key, T value, TimeSpan ttl, string region);
void AddAndDelete(string key, T value, TimeSpan ttl, string region);
T Get(string key);
void ClearRegion(string region);
}
}

View File

@ -63,7 +63,9 @@ namespace Ocelot.Cache.Middleware
var response = HttpResponseMessage;
_outputCache.Add(downstreamUrlKey, response, TimeSpan.FromSeconds(DownstreamRoute.ReRoute.FileCacheOptions.TtlSeconds));
var region = $"{DownstreamRoute.ReRoute.UpstreamHttpMethod}-{DownstreamRoute.ReRoute.UpstreamPathTemplate.Value}";
_outputCache.Add(downstreamUrlKey, response, TimeSpan.FromSeconds(DownstreamRoute.ReRoute.FileCacheOptions.TtlSeconds), region);
_logger.LogDebug("finished response added to cache for {downstreamUrlKey}", downstreamUrlKey);
}

View File

@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using CacheManager.Core;
namespace Ocelot.Cache
@ -6,18 +8,21 @@ 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)
public void Add(string key, T value, TimeSpan ttl, string region)
{
_cacheManager.Add(new CacheItem<T>(key, value, ExpirationMode.Absolute, ttl));
_cacheManager.Add(new CacheItem<T>(key, region, value, ExpirationMode.Absolute, ttl));
_keys.Add(key);
}
public void AddAndDelete(string key, T value, TimeSpan ttl)
public void AddAndDelete(string key, T value, TimeSpan ttl, string region)
{
var exists = _cacheManager.Get(key);
@ -26,12 +31,17 @@ namespace Ocelot.Cache
_cacheManager.Remove(key);
}
_cacheManager.Add(new CacheItem<T>(key, value, ExpirationMode.Absolute, ttl));
Add(key, value, ttl, region);
}
public T Get(string key)
{
return _cacheManager.Get<T>(key);
}
public void ClearRegion(string region)
{
_cacheManager.ClearRegion(region);
}
}
}

View File

@ -68,7 +68,7 @@ namespace Ocelot.Configuration.Repository
if (result.Response)
{
_cache.AddAndDelete(_ocelotConfiguration, ocelotConfiguration, TimeSpan.FromSeconds(3));
_cache.AddAndDelete(_ocelotConfiguration, ocelotConfiguration, TimeSpan.FromSeconds(3), "OcelotConfiguration");
return new OkResponse();
}

View File

@ -0,0 +1,33 @@
using System.Net.Http;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Ocelot.Cache;
namespace Ocelot.Controllers
{
[Authorize]
[Route("cache")]
public class OutputCacheController : Controller
{
private IOcelotCache<HttpResponseMessage> _cache;
public OutputCacheController(IOcelotCache<HttpResponseMessage> cache)
{
_cache = cache;
}
[HttpGet]
public IActionResult Get()
{
return new NotFoundResult();
}
[HttpDelete]
[Route("{region}")]
public IActionResult Delete(string region)
{
_cache.ClearRegion(region);
return new NoContentResult();
}
}
}