克隆的缓存项目存在内存问题
我们将值存储在 Cache-Enterprise 库缓存块中。缓存项(列表)的访问器修改值。 我们不希望缓存的项目受到影响。
因此,首先我们返回一个新的列表(CachedItem 的 IEnumerator) 这确保访问者添加和删除项目对原始缓存项目几乎没有影响。
但我们发现,我们返回给访问者的列表的所有实例都是活着的!对象关系图显示了该列表与 EnterpriseLibrary.CacheItem 之间的关系。
因此我们将返回更改为新克隆的 List。为此,我们使用了 LINQ 说 (从数据中的项目中选择新的 DataClass(item) ).ToList() 即使您按照上述方式执行操作,ORG 也会显示此列表与 CacheItem 之间存在关系。
我们不能做任何事情来创建企业库缓存中存在的列表项的克隆吗,它与缓存没有任何关系?!
we are having values stored in Cache-Enterprise library Caching Block. The accessors of the cached item, which is a List, modify the values.
We didnt want the Cached Items to get affected.
Hence first we returned a new List(IEnumerator of the CachedItem)
This made sure accessors adding and removing items had little effect on the original Cached item.
But we found, all the instances of the List we returned to accessors were ALIVE! Object relational Graph showed a relationship between this list and the EnterpriseLibrary.CacheItem.
So we changed the return to be a newly cloned List. For this we used a LINQ
say
(from item in Data select new DataClass(item) ).ToList()
even when you do as above, the ORG shows there is a relationship between this list and the CacheItem.
Cant we do anything to create a CLONE of the List item which is present in the Enterprise library cache, which DOESNT have ANY relationship with CACHE?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须对列表进行深度克隆,即添加列表中每个对象的克隆(并克隆它们可能包含的任何对象,我建议使用 Clone() 方法来为您执行此操作),以一个新的清单。正如上面所评论的,无论您复制多少次,参考就是参考。
You would have to make a deep clone of the list, i.e., add a clone of each object in the list (and clone any objects that they may contain, I would suggest having a Clone() method to do this for you), to a new list. As was commented above, a reference is a reference no matter how many times you copy it.
这就是我们从缓存中拉出项目时克隆项目的方式。对象必须标记为可序列化。
This is how we clone items when pulling them out of the cache. The objects have to be marked serializable.