123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Web;
- using System.Web.Caching;
- namespace Cache.WebCache
- {
- /// <summary>
- /// 默认的.net WebCache接口实现
- /// </summary>
- public class WebCache
- {
- protected static volatile System.Web.Caching.Cache webCache = HttpRuntime.Cache;
- /// <summary>
- /// 判断值是否存在..
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public bool IsExist(string key)
- {
- if (string.IsNullOrEmpty(key))
- return false;
- return webCache[key] != null;
- }
- /// <summary>
- /// 写入某个Cache
- /// </summary>
- /// <param name="key">键</param>
- /// <param name="o">对像</param>
- public void AddObject<T>(string key, T o, int t = 0)
- {
- if (String.IsNullOrEmpty(key) || o == null)
- return;
- if (t == 0)
- t = int.Parse(ConfigurationManager.AppSettings["OutTime"]);
- CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);
- webCache.Insert(key, o, null, System.DateTime.Now.AddMinutes(t), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, callBack);
- }
- /// <summary>
- /// 取某个key对应的object值
- /// </summary>
- /// <param name="key">键</param>
- /// <returns></returns>
- public T GetObject<T>(string key)
- {
- if (!String.IsNullOrEmpty(key))
- return (T)webCache.Get(key);
- return default(T);
- }
- /// <summary>
- /// 移出某个Cache
- /// </summary>
- /// <param name="key">键</param>
- public void RemoveObject(string key)
- {
- if (!String.IsNullOrEmpty(key))
- webCache.Remove(key);
- }
- /// <summary>
- /// 批量移出类型key的对像
- /// </summary>
- /// <param name="key"></param>
- public void RemoveObjectByRegex(string pattern)
- {
- if (!string.IsNullOrEmpty(pattern))
- {
- IDictionaryEnumerator enu = webCache.GetEnumerator();
- while (enu.MoveNext())
- {
- string key = enu.Key.ToString();
- if (key.IndexOf(pattern, StringComparison.CurrentCultureIgnoreCase) != -1)
- {
- RemoveObject(key);
- }
- }
- }
- }
- /// <summary>
- /// 移出所有Cache
- /// </summary>
- public void RemoveAll()
- {
- IDictionaryEnumerator enu = webCache.GetEnumerator();
- while (enu.MoveNext())
- {
- RemoveObject(enu.Key.ToString());
- }
- }
- public void onRemove(string key, object val, CacheItemRemovedReason reason)
- {
- switch (reason)
- {
- case CacheItemRemovedReason.DependencyChanged:
- //更新缓存映射表
- break;
- case CacheItemRemovedReason.Expired:
- //更新缓存映射表
- break;
- case CacheItemRemovedReason.Removed:
- break;
- case CacheItemRemovedReason.Underused:
- break;
- default:
- break;
- }
- }
- /// <summary>
- /// 返回所有键值
- /// </summary>
- /// <returns></returns>
- public List<string> GetKeys()
- {
- List<string> list = new List<string>();
- IDictionaryEnumerator enu = webCache.GetEnumerator();
- while (enu.MoveNext())
- {
- list.Add(enu.Key.ToString());
- }
- return list;
- }
- }
- }
|