【发布时间】:2021-11-18 20:12:50
【问题描述】:
我尝试使用异步 getter 方法创建缓存,该方法在 ASP.NET 5 Core MVC 应用程序中使用 EF Core 从数据库读取数据:
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Threading.Tasks;
public sealed class CacheManager
{
readonly IMemoryCache memoryCache;
public CacheManager(IMemoryCache memoryCache)
{
this.memoryCache = memoryCache;
}
public async Task<T> GetAsync<T>(string key, Task<Func<T>> generatorasync)
{
var cacheEntry = await
memoryCache.GetOrCreateAsync<T>(key, async entry =>
{
entry.SlidingExpiration = TimeSpan.FromSeconds(15 * 60);
return await generatorasync();
});
return cacheEntry;
}
}
线
return await generatorasync();
抛出语法错误
CS0149:需要方法名称
锄头来修复它?创建此类缓存的最佳做法是什么?
安德鲁斯。
【问题讨论】:
-
你能显示更多错误信息吗?或者您可以阅读更多关于使用缓存的示例:docs.microsoft.com/en-us/aspnet/core/performance/caching/…
标签: asp.net-mvc asp.net-core .net-core asp.net-core-mvc memorycache