gpt4 book ai didi

c# - 如何在 MVC Core 中使用缓存管理器?

转载 作者:行者123 更新时间:2023-11-30 17:27:02 25 4
gpt4 key购买 nike

我需要在 EF Core 中激活二级缓存,它将缓存查询结果直到下一次,而不是数据库。

最佳答案

这是我的 CacheManager 类

public class CacheManagerAdapter : ICacheService
{
private readonly YYYApi _api;
private ICacheManager<object> _cacheManager;
private static readonly string _cacheName;

static CacheManagerAdapter()
{
_cacheName = ConfigurationManager.AppSettings["cacheName"] ?? "CancelBookingCache";
}

public CacheManagerAdapter(IOptions<YYYApi> options)
{
_api = options.Value;

_cacheManager = CacheFactory.Build("cacheName", settings => settings
.WithUpdateMode(CacheUpdateMode.Up)
.WithRedisCacheHandle("redisCache")
.And.WithRedisConfiguration("redisCache", _api.cacheHost)
.WithJsonSerializer()
);
}

public void Clear()
{
_cacheManager.ClearRegion(_cacheName);
}

public bool Contains(string key)
{
return _cacheManager.GetCacheItem(key, _cacheName) == null;
}

public object Get(string key)
{
try
{
return _cacheManager.GetCacheItem(key, _cacheName).Value;
}
catch (Exception)
{
return null;
}

}

public T Get<T>(string key, Func<T> getItemCallback) where T : class
{
return _cacheManager.Get<T>(key, _cacheName);
}

public void Invalidate(Regex pattern)
{

throw new NotImplementedException();
}

public void Invalidate(string key)
{
_cacheManager.Remove(key, _cacheName);
}

public bool IsSet(string key)
{
throw new NotImplementedException();
}

public void Set(string key, object data, int cacheTime)
{
try
{
_cacheManager.AddOrUpdate(key, _cacheName, data, x => data);
if (cacheTime > 0)
{
_cacheManager.Expire(key, _cacheName, ExpirationMode.Absolute, new TimeSpan(0, cacheTime, 0));
}
}
catch (Exception ex)
{

}
}
}

界面

public interface ICacheService
{
void Clear();
bool Contains(string key);
T Get<T>(string key, Func<T> getItemCallback) where T : class;
object Get(string key);
void Invalidate(string key);
void Invalidate(Regex pattern);
bool IsSet(string key);
void Set(string key, object data, int cacheTime);
}

所以这是启动注入(inject)等

services.AddSingleton<ICacheService, CacheManagerAdapter>();

services.AddSingleton<IMMMAdapterService>(new MMMAdapterService(
XXX: new XXXController(
services.BuildServiceProvider().GetRequiredService<IOptions<YYYApi>>(),
_XXXLogger,
_diagnosticContext,
new CacheManagerAdapter(services.BuildServiceProvider().GetRequiredService<IOptions<YYYApi>>())
), _XXXLogger, _diagnosticContext));
services.AddSoapExceptionTransformer((ex) => ex.Message);

最后,注入(inject) Controller

private readonly YYYApi _api;
readonly ILogger<XXXController> _logger;
readonly IDiagnosticContext _diagnosticContext;
readonly ICacheService _cache;

public XXXController(IOptions<YYYApi> options, ILogger<XXXController> logger, IDiagnosticContext diagnosticContext, ICacheService cache)
: base(options, logger)
{
_api = options.Value;
_logger = logger;
_diagnosticContext = diagnosticContext ?? throw new ArgumentNullException(nameof(diagnosticContext));
_cache = cache;
}

关于c# - 如何在 MVC Core 中使用缓存管理器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55585566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com