我尝试将填充的IReadOnlyList设置为缓存,但它始终为空:
CacheItemPolicy cip = new CacheItemPolicy(); // no expiration
IReadOnlyList<T> dataList = [populate var]
if (dataList != null)
{
MemoryCache.Default.Set(_cacheKey, dataList, cip);
}缓存中未设置任何内容。但如果我将其更改为:
List<T> list = null;
CacheItemPolicy cip = new CacheItemPolicy(); // no expiration
IReadOnlyList<T> dataList = [populate var]
if (dataList != null)
{
list = dataList.ToList();
MemoryCache.Default.Set(_cacheKey, list, cip);
}它起作用了。MemoryCache不支持接口或IReadOnlyList对象吗?
发布于 2016-09-26 07:26:41
MemoryCache正在缓存对第一个示例中列表的引用和第二个示例中列表的副本。
一定是因为您的代码重用了作为dataList传入的列表,所以您需要缓存一个副本。调用ToList()会生成一个副本。
https://stackoverflow.com/questions/39681393
复制相似问题