123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- using Common;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Web;
- using System.Web.Caching;
- namespace KC.Cache.WebCache
- {
- public class WebCache:ICache
- {
- protected static volatile System.Web.Caching.Cache webCache = HttpRuntime.Cache;
- private static int timeOut = ConfigHelper.GetValue("OutTime").TryToInt32();
- /// <summary>
- /// 写入缓存,单体
- /// </summary>
- /// <param name="value">对象数据</param>
- /// <param name="cacheKey">键</param>
- /// <param name="seconds">几秒过期</param>
- public void WriteCache<T>(string cacheKey, T value, int seconds = 0) where T : class
- {
- if (String.IsNullOrEmpty(cacheKey) || value == null)
- return;
- if (seconds == 0)
- seconds = timeOut;
- var callBack = new CacheItemRemovedCallback(onRemove);
- webCache.Insert(cacheKey, value, null, System.DateTime.Now.AddSeconds(seconds), System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.High, callBack);
- }
- /// <summary>
- /// 读取缓存,单体
- /// </summary>
- /// <param name="cacheKey">键</param>
- /// <returns></returns>
- public T GetCache<T>(string cacheKey) where T : class
- {
- if (!String.IsNullOrEmpty(cacheKey))
- return (T)webCache.Get(cacheKey);
- return default(T);
- }
- /// <summary>
- /// 获取所有key
- /// </summary>
- /// <returns></returns>
- public List<string> GetAllKeys()
- {
- List<string> list = new List<string>();
- var enu = webCache.GetEnumerator();
- while (enu.MoveNext())
- {
- list.Add(enu.Key.ToString());
- }
- return list;
- }
- /// <summary>
- /// 确定当前Key是否过期
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public bool HasExpire(string cacheKey)
- {
- if (string.IsNullOrEmpty(cacheKey))
- return false;
- return webCache[cacheKey] != null;
- }
- /// <summary>
- /// 移除指定数据缓存
- /// </summary>
- /// <param name="cacheKey">键</param>
- public void RemoveCache(string cacheKey)
- {
- if (!String.IsNullOrEmpty(cacheKey))
- webCache.Remove(cacheKey);
- }
- /// <summary>
- /// 批量删除数据缓存
- /// </summary>
- /// <param name="keys"></param>
- public void RemoveCache(List<string> keys)
- {
- if (keys.Count > 0)
- {
- var enu = webCache.GetEnumerator();
- while (enu.MoveNext())
- {
- string key = enu.Key.ToString();
- if (keys.Where(p => p == key).Count() > 0)
- {
- RemoveCache(key);
- }
- }
- }
- }
- #region MyRegion
- 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;
- }
- }
- #endregion
- }
- }
|