removed old memory cache and added a few tests for new asp memory cache

This commit is contained in:
TomPallister
2020-04-13 18:37:49 +01:00
parent b4c29fc727
commit 8b098d2336
7 changed files with 36 additions and 203 deletions

View File

@ -1,11 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
namespace Ocelot.Cache
namespace Ocelot.Cache
{
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Caching.Memory;
public class AspMemoryCache<T> : IOcelotCache<T>
{
private readonly IMemoryCache _memoryCache;
@ -20,7 +18,9 @@ namespace Ocelot.Cache
public void Add(string key, T value, TimeSpan ttl, string region)
{
if (ttl.TotalMilliseconds <= 0)
{
return;
}
_memoryCache.Set(key, value, ttl);
@ -28,10 +28,13 @@ namespace Ocelot.Cache
}
public T Get(string key, string region)
{
_memoryCache.TryGetValue<T>(key, out T value);
{
if (_memoryCache.TryGetValue(key, out T value))
{
return value;
}
return value;
return default(T);
}
public void ClearRegion(string region)
@ -48,8 +51,10 @@ namespace Ocelot.Cache
public void AddAndDelete(string key, T value, TimeSpan ttl, string region)
{
if (_memoryCache.TryGetValue(key, out object oldValue))
if (_memoryCache.TryGetValue(key, out T oldValue))
{
_memoryCache.Remove(key);
}
Add(key, value, ttl, region);
}

View File

@ -1,81 +0,0 @@
namespace Ocelot.Cache
{
using System;
using System.Collections.Generic;
public class InMemoryCache<T> : IOcelotCache<T>
{
private readonly Dictionary<string, CacheObject<T>> _cache;
private readonly Dictionary<string, List<string>> _regions;
public InMemoryCache()
{
_cache = new Dictionary<string, CacheObject<T>>();
_regions = new Dictionary<string, List<string>>();
}
public void Add(string key, T value, TimeSpan ttl, string region)
{
if (ttl.TotalMilliseconds <= 0)
{
return;
}
var expires = DateTime.UtcNow.Add(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 });
}
}
public void AddAndDelete(string key, T value, TimeSpan ttl, string region)
{
if (_cache.ContainsKey(key))
{
_cache.Remove(key);
}
Add(key, value, ttl, region);
}
public void ClearRegion(string region)
{
if (_regions.ContainsKey(region))
{
var keys = _regions[region];
foreach (var key in keys)
{
_cache.Remove(key);
}
}
}
public T Get(string key, string region)
{
if (_cache.ContainsKey(key))
{
var cached = _cache[key];
if (cached.Expires > DateTime.UtcNow)
{
return cached.Value;
}
_cache.Remove(key);
}
return default(T);
}
}
}

View File

@ -77,20 +77,6 @@
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)

View File

@ -1,7 +1,3 @@
using Ocelot.ServiceDiscovery.Providers;
using Ocelot.Configuration.ChangeTracking;
namespace Ocelot.DependencyInjection
{
using Microsoft.AspNetCore.Http;
@ -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;
@ -58,12 +56,8 @@ namespace Ocelot.DependencyInjection
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>();