这个问题我已经有好几个月了。我从entlib 4.1升级到5,我的应用程序缓存的项目越来越多。有时(有时每天尝试) CPU挂起100%的使用率,但应用程序保持响应。当发生这种情况时,我使用dotTrace获取快照,并且似乎大部分时间都花在了PriorityDateComparer.Compare上。此比较器仅由System.Collections.SortedList的构造函数使用,并包含以下内容:
public int Compare(object x, object y)
{
CacheItem leftCacheItem = (CacheItem)unsortedItems[(string)x];
CacheItem rightCacheItem = (CacheItem)unsortedItems[(string)y];
lock (rightCacheItem)
{
lock (leftCacheItem)
{
if (rightCacheItem == null && leftCacheItem == null)
{
return 0;
}
if (leftCacheItem == null)
{
return -1;
}
if (rightCacheItem == null)
{
return 1;
}
return leftCacheItem.ScavengingPriority == rightCacheItem.ScavengingPriority
? leftCacheItem.LastAccessedTime.CompareTo(rightCacheItem.LastAccessedTime)
: leftCacheItem.ScavengingPriority - rightCacheItem.ScavengingPriority;
}
}
}问题1:我们能否确保这两个缓存项总是以相同的顺序锁定?如果我检查SortedList的实现,我不这么认为。
问题2:如果我的第一个问题的答案是“否”,那我们如何解决这个问题?我看到了一些可能性:
cacheItems.
倾向于什么?
发布于 2012-07-03 20:08:53
我更改了比较器,以便它不需要查找缓存项:
int IComparer<CacheItem>.Compare(CacheItem leftCacheItem, CacheItem rightCacheItem)
{
lock (rightCacheItem)
{
lock (leftCacheItem)
{
if (rightCacheItem == null && leftCacheItem == null)
{
return 0;
}
if (leftCacheItem == null)
{
return -1;
}
if (rightCacheItem == null)
{
return 1;
}
return leftCacheItem.ScavengingPriority == rightCacheItem.ScavengingPriority
? leftCacheItem.LastAccessedTime.CompareTo(rightCacheItem.LastAccessedTime)
: leftCacheItem.ScavengingPriority - rightCacheItem.ScavengingPriority;
}
}
}在Microsoft.Practices.EnterpriseLibrary.Caching.ScavengerTask中,我相应地从以下位置更改了调用方法:
private static SortedList SortItemsForScavenging(Hashtable unsortedItemsInCache)
{
return new SortedList(unsortedItemsInCache, new PriorityDateComparer(unsortedItemsInCache));
}至
private static List<CacheItem> SortItemsForScavenging(Hashtable unsortedItemsInCache)
{
List<CacheItem> cacheValues = new List<CacheItem>(unsortedItemsInCache.Values.Cast<CacheItem>());
cacheValues.Sort(new PriorityDateComparer());
return cacheValues;
}https://stackoverflow.com/questions/11153477
复制相似问题