0

我正在尝试使用以下方法缓存“FirmDetails”的一组键:


  public async Task<List<Firm>> LoadFirmDetailsAsync()
        {
            List<Firm> items;
            using (var dbContext = new CovestecContext(_configuration))
            {
                var repo = dbContext.GetRepository<Firm>();
                //items = repo.Include(x => x.FirmUsers).ThenInclude(y => y.User).Include(x => x.FirmUsers).ThenInclude(x => x.Role).ToList();
                items = repo.Include(x => x.FirmUsers).ThenInclude(y => y.User).Include(x => x.FirmUsers).ThenInclude(x => x.Role).ToList();
            }

            await _cache.SetAsync("FirmDetails", items, new DistributedCacheEntryOptions { AbsoluteExpiration = DateTimeOffset.MaxValue });

            return items;
        }

构造函数如下所示:

 private static IDistributedCache _cache;
        private static IConfiguration _configuration;
        private static ILogger _logger;

        public DataCache(IDistributedCache cache, IConfiguration configuration, ILogger<DataCache> logger)
        {
            _cache = cache;
            _configuration = configuration;
            _logger = logger;
            
        }

与服务器相关的配置在 appsettings.developement.json 中定义如下

 "enyimMemcached": {
    "Servers": [
      {
        "Address": "localhost",
        "Port": 11211
      }
    ]
  }

并在我的 Startup.cs 类的 ConfigurationService 方法中添加服务,如下所示:

services.AddEnyimMemcached(Configuration)

现在,在尝试使用下面的代码获取“FirmDetail”的数据时,结果为空。

   public async Task<List<Firm>> GetFirmDetailsCache()
        {
            if (_cache.Get("FirmDetails") == null)
            {
                await LoadFirmDetailsAsync();
            }

            var result = await _cache.GetAsync<List<Firm>>("FirmDetails");
            return result;
        }

请让我知道我在这里做错了什么。

4

0 回答 0