C# LRUCache类

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
public class LRUCache < TKey, TValue > : where TValue : class
{
private Func < TValue, TKey > getKey;
private int maxCapacity;
private Dictionary < TKey, LinkedListNode < TValue >> buffer;
private LinkedList < TValue > list;
private ReaderWriterLockSlim locker;

public Func < TValue, TKey > GetKey
{
get { return getKey; }
}
public int Capacity
{
set { Interlocked.Exchange( ref maxCapacity, value); }
get { return maxCapacity; }
}
public int Count
{
get
{
locker.EnterReadLock();
try
{
return buffer.Count;
}
finally
{
locker.ExitReadLock();
}
}
}
public TValue LastItem
{
get
{
locker.EnterReadLock();
try
{
return list.Last.Value;
}
finally
{
locker.ExitReadLock();
}
}
}
internal ReaderWriterLockSlim Locker
{
get { return locker; }
}

public TValue this [TKey key]
{
set
{
if (value == null )
{
locker.EnterUpgradeableReadLock();
try
{
LinkedListNode
< TValue > node;
if (buffer.TryGetValue(key, out node))
{
locker.EnterWriteLock();
try
{
buffer.Remove(key);
list.Remove(node);
}
finally
{
locker.ExitWriteLock();
}
}
}
finally
{
locker.ExitUpgradeableReadLock();
}
}
else
{
AddFirst(key, value);
}
}
get
{
locker.EnterReadLock();
try
{
LinkedListNode
< TValue > node;
if (buffer.TryGetValue(key, out node))
{
locker.EnterWriteLock();
try
{
list.Remove(node);
list.AddFirst(node);
}
finally
{
locker.ExitWriteLock();
}
}
return node.Value;
}
finally
{
locker.ExitReadLock();
}
}
}

public LRUCache(Func < TValue, TKey > func)
:
this (func, 500 )
{

}
public LRUCache(Func < TValue, TKey > func, int capacity)
{
this .getKey = func;
this .maxCapacity = capacity * 10 ;
buffer
= new Dictionary < TKey, LinkedListNode < TValue >> (capacity);
list
= new LinkedList < TValue > ();
locker
= new ReaderWriterLockSlim();
}

public bool Contains(TKey key)
{
locker.EnterReadLock();
try
{
return buffer.ContainsKey(key);
}
finally
{
locker.ExitReadLock();
}
}

public void AddFirst(TKey key, TValue value)
{
AddFirst(key, value, Timeout.Infinite);
}
public void AddFirst(TKey key, TValue value, int timeout)
{
locker.EnterUpgradeableReadLock();
int count = buffer.Count;
if (count >= maxCapacity)
{
int needRemoveCount = count / 10 ;
locker.EnterWriteLock();
try
{
for ( int i = 0 ; i < needRemoveCount; i ++ )
{
buffer.Remove(getKey(list.Last.Value));
list.RemoveLast();
}
}
finally
{
locker.ExitWriteLock();
}
}
try
{
LinkedListNode
< TValue > node;
if ( ! buffer.TryGetValue(key, out node))
{
node
= new LinkedListNode < TValue > (value);
if (locker.TryEnterWriteLock(timeout))
{
buffer.Add(key, node);
list.AddFirst(node);
locker.ExitWriteLock();
}
}
}
finally
{
locker.ExitUpgradeableReadLock();
}
}

public void RemoveLast()
{
locker.EnterUpgradeableReadLock();
try
{
if (list.Last != null )
{
TKey key
= getKey(list.Last.Value);
locker.EnterWriteLock();
try
{
buffer.Remove(key);
list.RemoveLast();
}
finally
{
locker.ExitWriteLock();
}
}
}
finally
{
locker.ExitUpgradeableReadLock();
}
}

public void Clear()
{
locker.EnterWriteLock();
try
{
buffer.Clear();
list.Clear();
}
finally
{
locker.ExitWriteLock();
}
}
}

 

 

 

转载于:https://www.cnblogs.com/Googler/archive/2010/06/11/1756551.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值