概述
在做后台的时候,想着把所有栏目放到缓存里,就这里了一个类。必然是有缺陷,暂时没有实现滑动缓存
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Web; using System.Web.Caching; namespace WebCore.UI.Common { public class Cache { private static System.Web.Caching.Cache _cache; public const int DayFactor = 0x4380; private static int Factor = 5; public const int HourFactor = 720; public const int MinuteFactor = 12; public const double SecondFactor = 0.2; static Cache() { HttpContext current = HttpContext.Current; if (current != null) { _cache = current.Cache; } else { _cache = HttpRuntime.Cache; } } private Cache() { } public static void Clear() { IDictionaryEnumerator enumerator = _cache.GetEnumerator(); ArrayList list = new ArrayList(); while (enumerator.MoveNext()) { list.Add(enumerator.Key); } foreach (string str in list) { _cache.Remove(str); } } public static object Get(string key) { return _cache[key]; } public static void Insert(string key, object obj) { Insert(key, obj, null, 1); } public static void Insert(string key, object obj, int seconds) { Insert(key, obj, null, seconds); } public static void Insert(string key, object obj, CacheDependency dep) { Insert(key, obj, dep, 0x21c0); } public static void Insert(string key, object obj, int seconds, CacheItemPriority priority) { Insert(key, obj, null, seconds, priority); } public static void Insert(string key, object obj, CacheDependency dep, int seconds) { Insert(key, obj, dep, seconds, CacheItemPriority.Normal); } public static void Insert(string key, object obj, CacheDependency dep, int seconds, CacheItemPriority priority) { if (obj != null) { _cache.Insert(key, obj, dep, DateTime.Now.AddSeconds((double)(Factor * seconds)), TimeSpan.Zero, priority, null); } } public static void Max(string key, object obj, CacheDependency dep) { if (obj != null) { _cache.Insert(key, obj, dep, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.AboveNormal, null); } } public static void MicroInsert(string key, object obj, int secondFactor) { if (obj != null) { _cache.Insert(key, obj, null, DateTime.Now.AddSeconds((double)(Factor * secondFactor)), TimeSpan.Zero); } } public static void Remove(string key) { _cache.Remove(key); } public static void RemoveByPattern(string pattern) { IDictionaryEnumerator enumerator = _cache.GetEnumerator(); Regex regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase); while (enumerator.MoveNext()) { if (regex.IsMatch(enumerator.Key.ToString())) { _cache.Remove(enumerator.Key.ToString()); } } } public static void ReSetFactor(int cacheFactor) { Factor = cacheFactor; } public static int SecondFactorCalculate(int seconds) { return Convert.ToInt32(Math.Round((double)(seconds * 0.2))); } } }
使用方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Caching; namespace WebCore.UI.Common { public class SetSysMenuCache { private const string SiteSettingsCacheKey = "FileCache-SysLeftMenus"; public string ReadCache(bool cacheable,string Uid,Func<string> GetData) { string str = ""; str = Cache.Get(Uid + "-" + SiteSettingsCacheKey) as string; if (str == null || str == "") { //缓存不存在 if (cacheable) { if (GetData!=null) { str=GetData(); } } } return str; } public void CreatCache(string Datas,string Uid) { Cache.Insert(Uid + "-" + SiteSettingsCacheKey, Datas,3600); } public void UpdateCache(string Datas, string Uid) { Cache.Remove(Uid + "-" + SiteSettingsCacheKey); CreatCache(Datas, Uid); } } }
转载于:https://www.cnblogs.com/CodeHelper/p/6343906.html
最后
以上就是贪玩月光为你收集整理的System.Web.Caching.Cache 方法汇总的全部内容,希望文章能够帮你解决System.Web.Caching.Cache 方法汇总所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复