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条)