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

I'm working on a POC for the HybridCache for one of our internal libraries and I want to allow con

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744092833a4557399.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信