如何在Web Api中使用Redis缓存和存储库

时间:2018-01-31 06:56:45

标签: asp.net-web-api redis

我是Redis缓存实现的新手,并从Redis Cache获得了getstring的简单示例,该工作正常。但同时我没有通过Redis了解Set和获取列表值。

我有使用Provider和Repository。请帮我进一步了解......

 public List<Article> GetArticleListBySectionName(string sectionName, int RegionId, int Count, int CacheTime, string cacheKey, bool cacheEnable = false, string expiryInMinutes = "NoExpiry")
    {
        {
            cacheKey = GetCacheKey(cacheKey, Convert.ToString(sectionName));
            List<Article> _article = _cacheProxy.Get<List<Article>>(cacheKey);
            if (_article != null)
            {
                _article = _articleRepositary.GetArticleListBySectionName(sectionName, RegionId, Count, CacheTime);
                _cacheProxy.Store<List<Article>>(_article, cacheKey, expiryInMinutes);
            }
            return _article;
        }
    }

此处_cacheProxy.Get_cacheProxy.Store获取null值。执行此操作时,我会收到错误页面

1 个答案:

答案 0 :(得分:0)

  1. 注入依赖项
  2. 在WebApiConfig中添加以下行

    config.DependencyResolver = new UnityResolver(Bootstrapper.Initialise(DIContainer.Initialise()));

  3. 添加127.0.0.1:6379
  4. 的Redis端点