代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
缓存类
作者:
Dezai.CN
/ 发布于
2012/1/11
/
695
<div><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Collections.Generic; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Web; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Configuration; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Collections; </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)">CacheHelper 的摘要说明 </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">class</span><span style="color: rgb(0,0,0)"> CacheHelper { </span><span style="color: rgb(0,0,255)">#region</span><span style="color: rgb(0,0,0)"> 从配置文件中读取缓存时间</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">缓存时间</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">private</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> _CacheTime </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)">.Empty; </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> CacheTime { </span><span style="color: rgb(0,0,255)">get</span><span style="color: rgb(0,0,0)"> { </span><span style="color: rgb(0,0,255)">try</span><span style="color: rgb(0,0,0)"> { _CacheTime </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> ConfigurationManager.AppSettings[</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">CacheTime</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">].ToString(); } </span><span style="color: rgb(0,0,255)">catch</span><span style="color: rgb(0,0,0)"> (Exception) { _CacheTime </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">0</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">; } </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> _CacheTime; } </span><span style="color: rgb(0,0,255)">set</span><span style="color: rgb(0,0,0)"> { _CacheTime </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> value; } } </span><span style="color: rgb(0,0,255)">#endregion</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> CacheHelper() { </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">TODO: 在此处添加构造函数逻辑 </span><span style="color: rgb(0,128,0)">// </span><span style="color: rgb(0,0,0)"> } </span><span style="color: rgb(0,0,255)">#region</span><span style="color: rgb(0,0,0)"> 插入Cache</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 插入Cache </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><typeparam name="T"></typeparam></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="o"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="key"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> Add</span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(0,0,0)">T</span><span style="color: rgb(0,0,0)">></span><span style="color: rgb(0,0,0)">(T o, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> key) { HttpContext.Current.Cache.Insert( key, o, </span><span style="color: rgb(0,0,255)">null</span><span style="color: rgb(0,0,0)">, DateTime.Now.AddMinutes(Convert.ToDouble(CacheTime)), </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> Cache的缓存时间,通常建议配置在Config文件中 </span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> System.Web.Caching.Cache.NoSlidingExpiration); } </span><span style="color: rgb(0,0,255)">#endregion</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">#region</span><span style="color: rgb(0,0,0)"> 删除指定的Cache</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 删除指定的Cache </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="key"></span><span style="color: rgb(0,128,0)">Cache的key</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> Clear(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> key) { HttpContext.Current.Cache.Remove(key); } </span><span style="color: rgb(0,0,255)">#endregion</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">#region</span><span style="color: rgb(0,0,0)"> 判断Cache是否存在</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 判断Cache是否存在 </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="key"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><returns></returns></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">bool</span><span style="color: rgb(0,0,0)"> Exists(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> key) { </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> HttpContext.Current.Cache[key] </span><span style="color: rgb(0,0,0)">!=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">null</span><span style="color: rgb(0,0,0)">; } </span><span style="color: rgb(0,0,255)">#endregion</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">#region</span><span style="color: rgb(0,0,0)"> 取得Cache值,带类型 T</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 取得Cache值,带类型 T </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><typeparam name="T"></typeparam></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="key"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="value"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><returns></returns></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">bool</span><span style="color: rgb(0,0,0)"> Get</span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(0,0,0)">T</span><span style="color: rgb(0,0,0)">></span><span style="color: rgb(0,0,0)">(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> key, </span><span style="color: rgb(0,0,255)">out</span><span style="color: rgb(0,0,0)"> T value) { </span><span style="color: rgb(0,0,255)">try</span><span style="color: rgb(0,0,0)"> { </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,0)">!</span><span style="color: rgb(0,0,0)">Exists(key)) { value </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">default</span><span style="color: rgb(0,0,0)">(T); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">; } value </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> (T)HttpContext.Current.Cache[key]; } </span><span style="color: rgb(0,0,255)">catch</span><span style="color: rgb(0,0,0)"> { value </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">default</span><span style="color: rgb(0,0,0)">(T); </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">; } </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">; } </span><span style="color: rgb(0,0,255)">#endregion</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">#region</span><span style="color: rgb(0,0,0)"> 清除所有缓存</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 有时可能需要立即更新,这里就必须手工清除一下Cache </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)">Cache类有一个Remove方法,但该方法需要提供一个CacheKey,但整个网站的CacheKey我们是无法得知的 </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)">只能经过遍历 </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> RemoveAllCache() { System.Web.Caching.Cache _cache </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> HttpRuntime.Cache; IDictionaryEnumerator CacheEnum </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> _cache.GetEnumerator(); ArrayList al </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> ArrayList(); </span><span style="color: rgb(0,0,255)">while</span><span style="color: rgb(0,0,0)"> (CacheEnum.MoveNext()) { al.Add(CacheEnum.Key); } </span><span style="color: rgb(0,0,255)">foreach</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> key </span><span style="color: rgb(0,0,255)">in</span><span style="color: rgb(0,0,0)"> al) { _cache.Remove(key); } } </span><span style="color: rgb(0,0,255)">#endregion</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">#region</span><span style="color: rgb(0,0,0)"> 显示所有缓存</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">显示所有缓存 </span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> show() { </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> str </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">""</span><span style="color: rgb(0,0,0)">; IDictionaryEnumerator CacheEnum </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> HttpRuntime.Cache.GetEnumerator(); </span><span style="color: rgb(0,0,255)">while</span><span style="color: rgb(0,0,0)"> (CacheEnum.MoveNext()) { str </span><span style="color: rgb(0,0,0)">+=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">缓存名<b>[</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> CacheEnum.Key </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">]</b></span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">; } </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">当前网站总缓存数:</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> HttpRuntime.Cache.Count </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)"></span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> str; } </span><span style="color: rgb(0,0,255)">#endregion</span><span style="color: rgb(0,0,0)"> } </span></div>
试试其它关键字
缓存类
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3