Tag: asp.net web api2 memorycache

web api中的内存缓存

我在我的web api中寻找缓存 ,我可以使用一个api方法的输出(在12小时内更改一次)进行后续调用,然后我在SO上找到了这个解决方案 ,但是我很难理解并使用下面的代码 private IEnumerable GetFromCache(string key, Func<IEnumerable> valueFactory) where TEntity : class { ObjectCache cache = MemoryCache.Default; var newValue = new Lazy<IEnumerable>(valueFactory); CacheItemPolicy policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(30) }; //The line below returns existing item or adds the new value if it doesn’t exist var value = cache.AddOrGetExisting(key, newValue, policy) […]