最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - .NET HybridCache: Disabled Local Cache returns InvalidData warning - Stack Overflow

programmeradmin5浏览0评论

I'm working on a POC for the HybridCache for one of our internal libraries and I want to allow consumers the ability to decide whether they want to enable or disable the local cache. I'm using Couchbase as the L2 cache and I have a pretty basic configuration

builder.Services.AddCouchbase(builder.Configuration.GetSection("Couchbase"));
builder.Services.AddDistributedCouchbaseCache(options =>
{
  options.BucketName = "cachebucket";
});

builder.Services.AddHybridCache(options =>
{
  options.DefaultEntryOptions = new HybridCacheEntryOptions
  {
    Flags = HybridCacheEntryFlags.DisableLocalCache
  };
});
[HttpGet(Name = "GetWeatherForecast")]
public async Task<IEnumerable<WeatherForecast>> Get()
{
    return await _hybridCache.GetOrCreateAsync("weather-forecast", factory: async _ => await GetData());
}

private static async Task<WeatherForecast[]> GetData()
{
    await Task.Delay(1000 * 5);  // wait for for 5 seconds
    var data =  Enumerable.Range(1, 5).Select(index => new WeatherForecast
    {
         Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
         TemperatureC = Random.Shared.Next(-20, 55),
         Summary = Summaries[Random.Shared.Next(Summaries.Length)]
   })
   .ToArray();

    return data;
}

When I call the GetOrCreateAsync() method, the initial call fetches the data from the factory as expected. The data is correctly populated to Couchbase as well

On subsequent calls, I expect the L1 cache to be disabled and the data fetched from the L2 cache, but what is happening is that the data is fetched from the factory all the time and I get these warnings on standard output

warn: Microsoft.Extensions.Caching.HybridCache
      Cache backend data rejected: InvalidData.
发布评论

评论列表(0)

  1. 暂无评论