From 2825067971e753cb0fcb47b5521d5137461618a0 Mon Sep 17 00:00:00 2001 From: Mohsen Rajabi Date: Sat, 30 May 2020 22:25:39 +0430 Subject: [PATCH] add cache file repository --- .../ConsulFileConfigurationRepository.cs | 8 ++++---- .../Middleware/AuthenticationMiddleware.cs | 2 +- .../Repository/DiskFileConfigurationRepository.cs | 15 ++++++++++++++- .../DiskFileConfigurationRepositoryTests.cs | 8 +++++--- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/Ocelot.Provider.Consul/ConsulFileConfigurationRepository.cs b/src/Ocelot.Provider.Consul/ConsulFileConfigurationRepository.cs index 2f95693625..48a68f2e86 100644 --- a/src/Ocelot.Provider.Consul/ConsulFileConfigurationRepository.cs +++ b/src/Ocelot.Provider.Consul/ConsulFileConfigurationRepository.cs @@ -66,10 +66,10 @@ public async Task Set(FileConfiguration ocelotConfiguration) Value = bytes, }; - var result = await _consul.KV.Put(kvPair); - if (result.Response) - { - _cache.AddAndDelete(_configurationKey, ocelotConfiguration, TimeSpan.FromSeconds(3), _configurationKey); + var result = await _consul.KV.Put(kvPair); + if (result.Response) + { + _cache.AddAndDelete(_configurationKey, ocelotConfiguration, TimeSpan.FromSeconds(5), _configurationKey); return new OkResponse(); } diff --git a/src/Ocelot/Authentication/Middleware/AuthenticationMiddleware.cs b/src/Ocelot/Authentication/Middleware/AuthenticationMiddleware.cs index 41007f688e..08a4d7e079 100644 --- a/src/Ocelot/Authentication/Middleware/AuthenticationMiddleware.cs +++ b/src/Ocelot/Authentication/Middleware/AuthenticationMiddleware.cs @@ -41,7 +41,7 @@ await httpContext.AuthenticateAsync(downstreamRoute.AuthenticationOptions userClaim = result.Principal; - _memoryCache.Set(cacheKey, userClaim, TimeSpan.FromMinutes(10)); + _memoryCache.Set(cacheKey, userClaim, TimeSpan.FromMinutes(5)); } httpContext.User = userClaim; diff --git a/src/Ocelot/Configuration/Repository/DiskFileConfigurationRepository.cs b/src/Ocelot/Configuration/Repository/DiskFileConfigurationRepository.cs index 52660cebd3..4800888308 100644 --- a/src/Ocelot/Configuration/Repository/DiskFileConfigurationRepository.cs +++ b/src/Ocelot/Configuration/Repository/DiskFileConfigurationRepository.cs @@ -9,21 +9,31 @@ namespace Ocelot.Configuration.Repository public class DiskFileConfigurationRepository : IFileConfigurationRepository { private readonly IOcelotConfigurationChangeTokenSource _changeTokenSource; + private readonly IOcelotCache _cache; private readonly string _environmentFilePath; private readonly string _ocelotFilePath; + private readonly string _cacheKey; private static readonly object _lock = new(); private const string ConfigurationFileName = "ocelot"; - public DiskFileConfigurationRepository(IWebHostEnvironment hostingEnvironment, IOcelotConfigurationChangeTokenSource changeTokenSource) + public DiskFileConfigurationRepository(IWebHostEnvironment hostingEnvironment, + IOcelotConfigurationChangeTokenSource changeTokenSource, Cache.IOcelotCache cache) { _changeTokenSource = changeTokenSource; + _cache = cache; _environmentFilePath = $"{AppContext.BaseDirectory}{ConfigurationFileName}{(string.IsNullOrEmpty(hostingEnvironment.EnvironmentName) ? string.Empty : ".")}{hostingEnvironment.EnvironmentName}.json"; + _cacheKey = "InternalDiskFileConfigurationRepository"; _ocelotFilePath = $"{AppContext.BaseDirectory}{ConfigurationFileName}.json"; } public Task> Get() { + var configuration = _cache.Get(_cacheKey, _cacheKey); + + if (configuration != null) + return Task.FromResult>(new OkResponse(configuration)); + string jsonConfiguration; lock (_lock) @@ -58,6 +68,9 @@ public Task Set(FileConfiguration fileConfiguration) } _changeTokenSource.Activate(); + + _cache.AddAndDelete(_cacheKey, fileConfiguration, TimeSpan.FromMinutes(5), _cacheKey); + return Task.FromResult(new OkResponse()); } } diff --git a/test/Ocelot.UnitTests/Configuration/DiskFileConfigurationRepositoryTests.cs b/test/Ocelot.UnitTests/Configuration/DiskFileConfigurationRepositoryTests.cs index 1cdad5b020..a9e54c4de6 100644 --- a/test/Ocelot.UnitTests/Configuration/DiskFileConfigurationRepositoryTests.cs +++ b/test/Ocelot.UnitTests/Configuration/DiskFileConfigurationRepositoryTests.cs @@ -3,7 +3,7 @@ using Ocelot.Configuration.ChangeTracking; using Ocelot.Configuration.File; using Ocelot.Configuration.Repository; - + namespace Ocelot.UnitTests.Configuration { public class DiskFileConfigurationRepositoryTests : IDisposable @@ -31,7 +31,8 @@ public DiskFileConfigurationRepositoryTests() _hostingEnvironment.Setup(he => he.EnvironmentName).Returns(_environmentName); _changeTokenSource = new Mock(MockBehavior.Strict); _changeTokenSource.Setup(m => m.Activate()); - _repo = new DiskFileConfigurationRepository(_hostingEnvironment.Object, _changeTokenSource.Object); + var aspMemoryCache = new AspMemoryCache(new MemoryCache(new MemoryCacheOptions())); + _repo = new DiskFileConfigurationRepository(_hostingEnvironment.Object, _changeTokenSource.Object, aspMemoryCache); } [Fact] @@ -114,7 +115,8 @@ private void GivenTheEnvironmentNameIsUnavailable() { _environmentName = null; _hostingEnvironment.Setup(he => he.EnvironmentName).Returns(_environmentName); - _repo = new DiskFileConfigurationRepository(_hostingEnvironment.Object, _changeTokenSource.Object); + var aspMemoryCache = new AspMemoryCache(new MemoryCache(new MemoryCacheOptions())); + _repo = new DiskFileConfigurationRepository(_hostingEnvironment.Object, _changeTokenSource.Object, aspMemoryCache); } private void GivenIHaveAConfiguration(FileConfiguration fileConfiguration)