[英]How to use SignIn Manager and User Manager on C# pages ASP.NET Core MVC
[英]How to use cache manager in MVC Core?
我需要在 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);
}
所以這是啟動注入等。
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);
最后,注入控制器
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.