我有这样的缓存服务:
public interface ICacheService {
T Get<T>(string cacheID, Func<T> getItemCallback, int cacheMinutes = 5) where T : class;
}
public class MemoryCacheService : ICacheService {
public T Get<T>(string cacheId, Func<T> getItemCallback, int cacheMinutes = 5) where T : class {
T item = MemoryCache.Default.Get(cacheId) as T;
if (item == null) {
item = getItemCallback();
MemoryCache.Default.Add(cacheId, item,
new CacheItemPolicy {AbsoluteExpiration = DateTime.Now.AddMinutes(cacheMinutes)});
}
return item;
}
}
并像这样检索:
var result = _cache.Get("mylist", () => _database.Fetch<MyList>().AsQueryable(), 600);
该列表很大,并且在每个击键提前下拉列表中经常访问。而且查询条件也是动态的,例如
if (this) result = result.Where(x=> this ...)
if (that) result = result.Where(x=> that ...)
finally result.ToList()
我想知道,每次从缓存访问列表时,系统是否在开始构建linq查询之前创建数据的副本?如果是这样,那就像按击键复制一样,不是很有效。还是因为我正在检索AsQueryable并构建linq而推迟了查询?
还有更好的选择吗?谢谢
不,MemoryCache不会复制。基本上,您将对某个对象实例的引用存储在缓存中,这就是您访问缓存中的项时所获得的。
我没有正式的文档链接,但是在实践中发现了“困难的方式”,在此我仅使用我获得的引用(未复制)意外地修改了缓存的对象。
另外,研究参考源(http://referencesource.microsoft.com)显示,没有自动复制发生。
根据您的应用程序和需求,您可能想要确保缓存的类型实际上是设计不变的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句