This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (executedContext.Result is OkObjectResult okObjectResult) | |
{ | |
await cacheService.CacheResponseAsync(cacheKey, okObjectResult.Value, TimeSpan.FromSeconds(_timeToLiveSeconds)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cacheService = context.HttpContext.RequestServices.GetRequiredService<IResponseCacheService>(); | |
var cacheKey = GenerateCacheKeyFromRequest(context.HttpContext.Request); | |
var cachedResponse = await cacheService.GetCachedResponseAsync(cacheKey); | |
if (!string.IsNullOrEmpty(cachedResponse)) | |
{ | |
var contentResult = new ContentResult | |
{ | |
Content = cachedResponse, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cacheSettings = context.HttpContext.RequestServices.GetRequiredService<RedisCacheSettings>(); | |
if (!cacheSettings.Enabled) | |
{ | |
await next(); | |
return; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task CacheResponseAsync(string cacheKey, object response, TimeSpan timeTimeLive) | |
{ | |
if (response == null) | |
{ | |
return; | |
} | |
var serializedResponse = JsonConvert.SerializeObject(response); | |
await _distributedCache.SetStringAsync(cacheKey, serializedResponse, new DistributedCacheEntryOptions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<string> GetCachedResponseAsync(string cacheKey) | |
{ | |
var cachedResponse = await _distributedCache.GetStringAsync(cacheKey); | |
return string.IsNullOrEmpty(cachedResponse) ? null : cachedResponse; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IResponseCacheService | |
{ | |
Task CacheResponseAsync(string cacheKey, object response, TimeSpan timeTimeLive); | |
Task<string> GetCachedResponseAsync(string cacheKey); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"RedisCacheSettings": { | |
"Enabled": true, | |
"ConnectionString": "localhost" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RedisCacheSettings | |
{ | |
public bool Enabled { get; set; } | |
public string ConnectionString { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var redisCacheSettings = new RedisCacheSettings(); | |
configuration.GetSection(nameof(RedisCacheSettings)).Bind(redisCacheSettings); | |
services.AddSingleton(redisCacheSettings); | |
if (!redisCacheSettings.Enabled) | |
{ | |
return; | |
} | |
services.AddStackExchangeRedisCache(options => options.Configuration = redisCacheSettings.ConnectionString); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] | |
public class ApiKeyAuthAttribute : Attribute, IAsyncActionFilter | |
{ | |
private const string ApiKeyHeaderName = "ApiKey"; | |
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) | |
{ | |
if (!context.HttpContext.Request.Headers.TryGetValue(ApiKeyHeaderName, out var potentialApiKey)) | |
{ | |
context.Result = new UnauthorizedResult(); |