程序代码:
namespace ITA.Base.EntLib.Caching
{
/// <summary>
/// 企业库缓存
/// </summary>
public class Cache : IDisposable, ICache
{
private Hashtable inMemoryCache;
DataDelegate _Motheds;
/// <summary>
/// 必填项目 方法名字,表示如果数据不存在,使用该方法填充数据(Motheds(DataForBase))
/// </summary>
public DataDelegate Motheds
{
get { return _Motheds; }
set { _Motheds = value; }
}
/// <summary>
/// 获取缓存
/// </summary>
/// <param name="initialItems"></param>
public Cache(Hashtable initialItems )
{
inMemoryCache = Hashtable.Synchronized(initialItems);
//IManager = Manager;
}
/// <summary>
/// 获取有多少个count
/// </summary>
public int Count
{
get { return inMemoryCache.Count; }
}
public Hashtable CurrentCacheState
{
get { return (Hashtable)inMemoryCache.Clone(); }
}
public void Remove(object key)
{
bool lockWasSuccessful;
CacheItem cacheItemBeforeLock = null;
do{
lock (inMemoryCache.SyncRoot)
{
cacheItemBeforeLock = (CacheItem)inMemoryCache[key];
lockWasSuccessful = Monitor.TryEnter(cacheItemBeforeLock);
}
} while (lockWasSuccessful == false);
//IManager.CacheRemove(key);
inMemoryCache.Remove(key);
Monitor.Exit(cacheItemBeforeLock);
}
public void Add(object key, object value, CacheItemPriority scavengingPriority, DateTime dt)
{
bool lockWasSuccessful;
CacheItem cacheItemBeforeLock = null;
do
{
lock (inMemoryCache.SyncRoot)
{
cacheItemBeforeLock = new CacheItem( false, null, CacheItemPriority.Normal, dt);
inMemoryCache.Add(key, cacheItemBeforeLock);
lockWasSuccessful = Monitor.TryEnter(cacheItemBeforeLock);
}
} while (lockWasSuccessful == false);
// IManager.CacheAdd(key, value, dt, CacheItemPriority.Normal);
cacheItemBeforeLock.IsCacheOk = true;
cacheItemBeforeLock.Data = value;
inMemoryCache[key] = cacheItemBeforeLock;
Monitor.Exit(cacheItemBeforeLock);
}
/// <summary>
/// 根据Key获取相关的数据,如果没有数据,则通过values去获取数据
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public CacheItem GetItem(object key, object values)
{
CacheItem _cacheItem = null;
if (inMemoryCache.ContainsKey(key))
{
_cacheItem = (CacheItem)inMemoryCache[key];
if (_cacheItem.IsCacheOk)
{
//从缓存中获取数据
//_cacheItem.Data = IManager.CacheItemDate(key);
if (_cacheItem.Data == null)
{
Remove(key);
_cacheItem = null;
}
}
else
{
//三秒钟内获取数据
for (int i = 0; i < 150; i++)
{
Thread.Sleep(20);
_cacheItem = (CacheItem)inMemoryCache[key];
if (_cacheItem.Data != null)
{
break;
}
else
{
continue;
}
}
}
}
else
{
//如果没有数据
//object values = methodes(forbase);
ClearExdateCache();
Add(key, values, CacheItemPriority.Low, DateTime.Now.AddSeconds(30));
_cacheItem = (CacheItem)inMemoryCache[key];
}
return _cacheItem;
}
/// <summary>
/// 清除已经过期的缓存
/// </summary>
public void ClearExdateCache()
{
lock (inMemoryCache.SyncRoot)
{
//Hashtable ht = inMemoryCache.Clone()
foreach (object key in CurrentCacheState.Keys)
{
CacheItem item = (CacheItem)inMemoryCache[key];
if (item.LastAccessedTime <= DateTime.Now)
{
inMemoryCache.Remove(key);
//IManager.CacheRemove(key.ToString());
}
}
}
}
/// <summary>
/// 删除最快过期的一个缓存
/// </summary>
public void RemoveCacheLastDateOut()
{
lock (inMemoryCache.SyncRoot)
{
CacheItem ditem=null;
object key_ = null;
foreach (object key in CurrentCacheState.Keys)
{
CacheItem item = (CacheItem)inMemoryCache[key];
if (ditem == null)
{
ditem = item;
key_ = key;
continue;
}
else
{
if (ditem.LastAccessedTime > item.LastAccessedTime)
{
key_ = key;
ditem = item;
}
}
}
inMemoryCache.Remove(key_);
//IManager.CacheRemove(ditem.Key);
}
}
/// <summary>
/// 删除缓存等级最低,最快过期的一个缓存
/// </summary>
public void RemoveCacheByCacheItemPriority()
{
lock (inMemoryCache.SyncRoot)
{
CacheItem ditem = null;
object key_ = null;
foreach (object key in CurrentCacheState.Keys)
{
CacheItem item = (CacheItem)inMemoryCache[key];
if (ditem == null)
{
ditem = item;
key_ = key;
continue;
}
else
{
if (ditem.ScavengingPriority > item.ScavengingPriority)
{
ditem = item;
key_ = key;
}
else
{
if (ditem.ScavengingPriority == item.ScavengingPriority && ditem.LastAccessedTime > item.LastAccessedTime)
{
ditem = item;
key_ = key;
}
}
}
}
inMemoryCache.Remove(key_);
}
}
~Cache()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
inMemoryCache = null;
//backingStore.Dispose();
//backingStore = null;
}
}
public object DataDoing( DataDelegate Mothed,object forbase)
{
return Mothed(forbase);
}
}
}