日期:2014-05-18  浏览次数:20439 次

类库中怎样使用Cache
rt
类库中无法直接引用System.Web.Cacheing,HttpRuntime.Cache等没法使用,不使用哈希表,有什么方法使用cache吗?

------解决方案--------------------
添加System.Web.dll
或者用的别的类库 如memcache等
------解决方案--------------------
天家 System.Web.dll

HttpContext.Current.Cache.Add(...
------解决方案--------------------
using System;
using System.Web;
using System.Collections.Generic;
using System.Web.Caching;
using System.Text;
using System.Collections;

namespace Common
{
public class MyCache
{
public static object ReadCacheValue(string eCacheKey)
{
if (HttpContext.Current != null && HttpContext.Current.Cache != null)
{
return HttpContext.Current.Cache[eCacheKey];
}else
{
return null;
}
}

public static void AddCache(string eCacheKey, object eCacheValue, double eCacheMin)
{
HttpContext.Current.Cache.Insert(eCacheKey, eCacheValue, null, DateTime.Now.AddMinutes(eCacheMin),
Cache.NoSlidingExpiration);
}

public static void ClearCache()
{
System.Web.Caching.Cache _cache = HttpRuntime.Cache;
IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
ArrayList al = new ArrayList();
while (CacheEnum.MoveNext())
{
al.Add(CacheEnum.Key);
}

foreach (string key in al)
{
_cache.Remove(key);
}

}
}
}