I have a problem that I cache a response from my API. First, my entity does not capitalize but when cached from Redis server it auto-capitalizes my entity. How do I fix that,
Here is picture
The next now with cached from Redis server
Here is my code for cache response
public async Task CacheResponseAsync(string key, object response, TimeSpan timeToLive)
{
if (response == null)
{
return;
}
var serializedResponse = JsonConvert.SerializeObject(response);
await _distributedCache.SetStringAsync(key, serializedResponse, new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = timeToLive
});
}
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
var cacheSetting = context.HttpContext.RequestServices.GetRequiredService<RedisCacheSetting>();
if (!cacheSetting.Enabled)
{
await next();
return;
}
var cacheService = context.HttpContext.RequestServices.GetRequiredService<IResponseCacheService>();
var cacheKey = GenerateKeyFromRequest(context.HttpContext.Request);
var cacheResponse = await cacheService.GetCacheResponseAsync(cacheKey);
if (!string.IsNullOrEmpty(cacheResponse))
{
var rs = new ContentResult
{
Content = cacheResponse,
ContentType = "application/json",
StatusCode = 200,
};
context.Result = rs;
return;
}
var executedContext = await next();
if (executedContext.Result is ObjectResult okObjectResult)
{
await cacheService.CacheResponseAsync(cacheKey, okObjectResult.Value, TimeSpan.FromSeconds(_timeToLiveSeconds));
}
}