代码语言
.
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
/
777
<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.Text; </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.Data; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Collections; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Text.RegularExpressions; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Web.Caching; </span><span style="color: rgb(0,0,255)">namespace</span><span style="color: rgb(0,0,0)"> DotNet.Common { </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)">///</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)">class</span><span style="color: rgb(0,0,0)"> CacheUtil { </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> CacheUtil() { </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(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)">///</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="strKey"></span><span style="color: rgb(0,128,0)">键</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,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="valueObj"></span><span style="color: rgb(0,128,0)">值</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,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="duration"></span><span style="color: rgb(0,128,0)">以秒为单位</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,128,0)"> </span><span style="color: rgb(128,128,128)"><returns></returns></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)"> Insert(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> strKey, </span><span style="color: rgb(0,0,255)">object</span><span style="color: rgb(0,0,0)"> valueObj, </span><span style="color: rgb(0,0,255)">double</span><span style="color: rgb(0,0,0)"> durationSecond) { </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (strKey </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,0)">&&</span><span style="color: rgb(0,0,0)"> strKey.Length </span><span style="color: rgb(0,0,0)">!=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">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)"> valueObj </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,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)"> CacheItemRemovedCallback callBack </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)"> CacheItemRemovedCallback(onRemove); HttpContext.Current.Cache.Insert(strKey, valueObj, </span><span style="color: rgb(0,0,255)">null</span><span style="color: rgb(0,0,0)">,DateTime.Now.AddSeconds(durationSecond),System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, callBack); </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)">else</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)">; } } </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)">///</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="strKey"></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(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)"> IsExist(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> strKey) { </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> HttpContext.Current.Cache[strKey] </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(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)">///</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="strKey"></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="obj"></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(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)">object</span><span style="color: rgb(0,0,0)"> Read(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> strKey) { </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)">if</span><span style="color: rgb(0,0,0)"> (HttpContext.Current.Cache[strKey] </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)">object</span><span style="color: rgb(0,0,0)"> obj </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> HttpContext.Current.Cache[strKey]; </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (obj </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)">return</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)">else</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)"> obj; } } </span><span style="color: rgb(0,0,255)">else</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)">null</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)"> 删除缓存对象 </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="strKey"></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(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)"> Remove(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> strKey) { </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (HttpContext.Current.Cache[strKey] </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)">) { HttpContext.Current.Cache.Remove(strKey); } } </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)">///</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="pattern"></span><span style="color: rgb(0,128,0)">正则表达式</span><span style="color: rgb(128,128,128)"></param></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)"> RemoveByRegexp(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> pattern) { </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (pattern </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 enu </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> HttpContext.Current.Cache.GetEnumerator(); </span><span style="color: rgb(0,0,255)">while</span><span style="color: rgb(0,0,0)"> (enu.MoveNext()) { </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,0)">=</span><span style="color: rgb(0,0,0)"> enu.Key.ToString(); </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)">(Regex.IsMatch(key,pattern)) { Remove(key); } } } } </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)">///</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)"> Clear() { IDictionaryEnumerator enu </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> HttpContext.Current.Cache.GetEnumerator(); </span><span style="color: rgb(0,0,255)">while</span><span style="color: rgb(0,0,0)"> (enu.MoveNext()) { Remove(enu.Key.ToString()); } } </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)">///</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="strKey"></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="obj"></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="reason"></param></span><span style="color: rgb(128,128,128)"> </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)">void</span><span style="color: rgb(0,0,0)"> onRemove(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> strKey, </span><span style="color: rgb(0,0,255)">object</span><span style="color: rgb(0,0,0)"> obj, CacheItemRemovedReason reason) { } } } </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