【发布时间】:2022-01-15 14:45:28
【问题描述】:
我有一个问题,我缓存了来自我的 API 的响应。首先,我的实体没有大写,但是当从 Redis 服务器缓存时,它会自动大写我的实体。我该如何解决这个问题,
这是图片
The next now with cached from Redis server
这是我的缓存响应代码
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));
}
}
【问题讨论】:
-
不是redis,是jsonserilizer:看这个stackoverflow.com/questions/34070459/…
标签: .net asp.net-core stackexchange.redis capitalization