Skip to content

Commit

Permalink
add cache file repository
Browse files Browse the repository at this point in the history
  • Loading branch information
EngRajabi authored and raman-m committed Oct 13, 2023
1 parent 6d64de8 commit 2825067
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ public async Task<Response> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,31 @@ namespace Ocelot.Configuration.Repository
public class DiskFileConfigurationRepository : IFileConfigurationRepository
{
private readonly IOcelotConfigurationChangeTokenSource _changeTokenSource;
private readonly IOcelotCache<FileConfiguration> _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<FileConfiguration> 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<Response<FileConfiguration>> Get()
{
var configuration = _cache.Get(_cacheKey, _cacheKey);

if (configuration != null)
return Task.FromResult<Response<FileConfiguration>>(new OkResponse<FileConfiguration>(configuration));

string jsonConfiguration;

lock (_lock)
Expand Down Expand Up @@ -58,6 +68,9 @@ public Task<Response> Set(FileConfiguration fileConfiguration)
}

_changeTokenSource.Activate();

_cache.AddAndDelete(_cacheKey, fileConfiguration, TimeSpan.FromMinutes(5), _cacheKey);

return Task.FromResult<Response>(new OkResponse());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Ocelot.Configuration.ChangeTracking;
using Ocelot.Configuration.File;
using Ocelot.Configuration.Repository;


namespace Ocelot.UnitTests.Configuration
{
public class DiskFileConfigurationRepositoryTests : IDisposable
Expand Down Expand Up @@ -31,7 +31,8 @@ public DiskFileConfigurationRepositoryTests()
_hostingEnvironment.Setup(he => he.EnvironmentName).Returns(_environmentName);
_changeTokenSource = new Mock<IOcelotConfigurationChangeTokenSource>(MockBehavior.Strict);
_changeTokenSource.Setup(m => m.Activate());
_repo = new DiskFileConfigurationRepository(_hostingEnvironment.Object, _changeTokenSource.Object);
var aspMemoryCache = new AspMemoryCache<FileConfiguration>(new MemoryCache(new MemoryCacheOptions()));
_repo = new DiskFileConfigurationRepository(_hostingEnvironment.Object, _changeTokenSource.Object, aspMemoryCache);
}

[Fact]
Expand Down Expand Up @@ -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<FileConfiguration>(new MemoryCache(new MemoryCacheOptions()));
_repo = new DiskFileConfigurationRepository(_hostingEnvironment.Object, _changeTokenSource.Object, aspMemoryCache);
}

private void GivenIHaveAConfiguration(FileConfiguration fileConfiguration)
Expand Down

0 comments on commit 2825067

Please sign in to comment.