admin 管理员组

文章数量: 1086019

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.

本文标签: cNET HybridCache Disabled Local Cache returns InvalidData warningStack Overflow