123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- using System;
- using System.Collections;
- using System.Web;
- namespace Cache
- {
- /// <summary>
- /// 缓存操作
- /// </summary>
- public class WebCache : ICache
- {
- private static readonly System.Web.Caching.Cache cache = HttpRuntime.Cache;
- /// <summary>
- /// 读取缓存
- /// </summary>
- /// <param name="cacheKey">键</param>
- /// <returns></returns>
- public T GetCache<T>(string cacheKey) where T : class
- {
- if (cache[cacheKey] != null)
- {
- return (T)cache[cacheKey];
- }
- return default(T);
- }
- /// <summary>
- /// 写入缓存
- /// </summary>
- /// <param name="value">对象数据</param>
- /// <param name="cacheKey">键</param>
- public void WriteCache<T>(T value, string cacheKey) where T : class
- {
- cache.Insert(cacheKey, value, null, DateTime.Now.AddMinutes(10), System.Web.Caching.Cache.NoSlidingExpiration);
- }
- /// <summary>
- /// 写入缓存
- /// </summary>
- /// <param name="value">对象数据</param>
- /// <param name="cacheKey">键</param>
- /// <param name="expireTime">到期时间</param>
- public void WriteCache<T>(T value, string cacheKey, DateTime expireTime) where T : class
- {
- cache.Insert(cacheKey, value, null, expireTime, System.Web.Caching.Cache.NoSlidingExpiration);
- }
- /// <summary>
- /// 移除指定数据缓存
- /// </summary>
- /// <param name="cacheKey">键</param>
- public void RemoveCache(string cacheKey)
- {
- if (cacheKey.Equals("All"))
- {
- this.RemoveCache();
- }
- else
- {
- cache.Remove(cacheKey);
- }
- }
- /// <summary>
- /// 移除全部缓存
- /// </summary>
- public void RemoveCache()
- {
- IDictionaryEnumerator cacheEnum = cache.GetEnumerator();
- while (cacheEnum.MoveNext())
- {
- if (cacheEnum.Key != null) cache.Remove(cacheEnum.Key.ToString());
- }
- }
- }
- }
|