System.Web.Caching.Cache 方法汇总
在做后台的时候,想着把所有栏目放到缓存里,就这里了一个类。必然是有缺陷,暂时没有实现滑动缓存
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 = ;
public const int HourFactor = ;
public const int MinuteFactor = ;
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, );
} 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,);
}
public void UpdateCache(string Datas, string Uid)
{
Cache.Remove(Uid + "-" + SiteSettingsCacheKey);
CreatCache(Datas, Uid);
}
}
}
System.Web.Caching.Cache 方法汇总的更多相关文章
- System.Web.Caching.Cache类 缓存 各种缓存依赖
原文:System.Web.Caching.Cache类 缓存 各种缓存依赖 Cache类,是一个用于缓存常用信息的类.HttpRuntime.Cache以及HttpContext.Current.C ...
- 第一节:从程序集的角度分析System.Web.Caching.Cache ,并完成基本封装。
一. 揭开迷雾 1. 程序集准备 a. 需要给项目添加 System.Web 程序集. b. 需要给使用的地方添加两个引用. 2. 程序集探究 在对应的类中输入关键字 Cache,选中点 ...
- System.Web.Caching.Cache类 缓存 各种缓存依赖(转)
转自:http://www.cnblogs.com/kissdodog/archive/2013/05/07/3064895.html Cache类,是一个用于缓存常用信息的类.HttpRuntime ...
- System.Web.Caching.Cache类 Asp.Net缓存 各种缓存依赖
Cache类,是一个用于缓存常用信息的类.HttpRuntime.Cache以及HttpContext.Current.Cache都是该类的实例. 一.属性 属性 说明 Count 获取存储在缓存中的 ...
- C# System.Web.Caching.Cache类 缓存 各种缓存依赖
原文:https://www.cnblogs.com/kissdodog/archive/2013/05/07/3064895.html Cache类,是一个用于缓存常用信息的类.HttpRuntim ...
- System.Web.Caching.Cache类 缓存
1.文件缓存依赖 public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender ...
- 清除 System.Web.Caching.Cache 以"xxx"开头的缓存
public static void ClearStartCache(string keyStart) { List<string> cacheKeys = new List<str ...
- 缓存-System.Web.Caching.Cache
实现 Web 应用程序的缓存. 每个应用程序域创建一个此类的实例,只要应用程序域将保持活动状态,保持有效. 有关此类的实例的信息,请通过Cache的属性HttpContext对象或Cache属性的Pa ...
- System.Web.Caching.Cache缓存帮助类
/// <summary> /// 缓存帮助类 /// </summary> public class CacheHelper { /// <summary> // ...
随机推荐
- Coloring Trees
Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- 织梦网站底部的Power by DedeCms怎么去掉?
由于织梦DEDECMS程序6月份的漏洞,很多织梦网站都被黑了,所以大家都在抓紧时间更新系统补丁.但是这次的DEDECMS V5.7版本更新后,在前台网页底部会出现织梦版权信息 “powered by ...
- Xcode崩溃问题调试 signal SIGABRT&EXC_BAD_ACCESS
在进行app开发过程中会遇到很多的问题,各种崩溃令人相当头疼.当然,解决bug的能力也体现了一个程序员的水平,现在来说一说开发中经常遇到的崩溃问题吧. 常见崩溃问题: 一是signal SIGABRT ...
- (简单) POJ 3087 Shuffle'm Up,枚举。
Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuff ...
- TCP协议和UDP协议的区别
1. TCP协议面向连接. UDP协议面向非连接 (有无链接)2. TCP协议传输速度慢. UDP协议传输速度快 (传输速度)3. TCP协议保证数据顺序. UDP协议不保证 (数据的有序性. 在IP ...
- Codeforces Education Round 11
A(模拟+数学) 题意:在一个数列当中最少添加多少个数可以使它们两两互质,并打印出添加以后的数列 #include <iostream> #include <cstdio> # ...
- Solr 按照得分score跟指定字段相乘排序
sort=product([you_field],query($q)) desc
- IdTCP的C++Builder2010示例(转)
源:IdTCP的C++Builder2010示例 IdTCP的C++Builder2010示例 这个是服务端的: void __fastcall TTCPDataServer::TCPServerEx ...
- UVa 496 - Simply Subsets
题目大意:给你两个集合,判断两个集合的关系(不相交.相等.真子集和其他).简单判断就可以了,不过STL的set没有交集.并集等操作有点让人觉得不方便... #include <cstdio> ...
- UVa 757 - Gone Fishing
题目大意:John有h的时间可以去钓鱼,有n湖可钓鱼,以5分钟为时间单位,每个湖初始每个单位时间可钓f条鱼,每下一个时间单位数量减少d条.同时,John只能从1号湖往后走进行钓鱼,湖之间的距离需要t个 ...