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

时间:2019-04-09 05:21:33

标签: c# asp.net model-view-controller core

我需要激活二级缓存EF Core,这样我才能将查询结果缓存到下一次,而不是数据库,它将被缓存。

2 个答案:

答案 0 :(得分:1)

取决于您的项目。您可以通过多种方式缓存数据:

  • 存储库模式中的实现缓存
  • 在单独的类中编写您自己的缓存管理器
  • 使用类似Redis的系统进行缓存
  • ASP.NET Core中的响应缓存:Details here

答案 1 :(得分:0)

这是我的CacheManager类

find_in_batches

界面

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);