代码语言
.
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
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
Web缓存管理
作者:
Dezai.CN
/ 发布于
2012/8/13
/
560
Web缓存管理
<div>using System; using System.Collections; using System.Text.RegularExpressions; using System.Web; using System.Web.Caching; using System.Collections.Generic;</div> <div>namespace Pub.Class { /// <summary> /// Web缓存管理 /// </summary> public class WebCache { #region 构造器 private WebCache() { } <div> static WebCache() { HttpContext context = HttpContext.Current; if (context != null) { _cache = (Cache)context.Cache; } else { _cache = HttpRuntime.Cache; } } #endregion</div> <div> #region 公有靜态字段 /// <summary> /// 日缓存因子 /// </summary> public static readonly int DayFactor = 17280; /// <summary> /// 小时缓存因子 /// </summary> public static readonly int HourFactor = 720; /// <summary> /// 分缓存因子 /// </summary> public static readonly int MinuteFactor = 12; /// <summary> /// 秒缓存因子 /// </summary> public static readonly double SecondFactor = 0.2; #endregion</div> <div> #region 私有静态字段 private static readonly Cache _cache; /// <summary> /// 缓存因子 /// </summary> private static int Factor = 5; #endregion</div> <div> #region 静态方法 public static IList<CachedItem> GetList(){ IList<CachedItem> list = new List<CachedItem>(); IDictionaryEnumerator CacheEnum = _cache.GetEnumerator(); while (CacheEnum.MoveNext()) { list.Add(new CachedItem(CacheEnum.Key.ToString(),CacheEnum.Value.GetType().ToString())); } return list; } /// <summary> /// 重新设置缓存因子 /// </summary> /// <param name="cacheFactor"></param> public static void ReSetFactor(int cacheFactor) { Factor = cacheFactor; } /// <summary> /// 清空所有缓存项目 /// </summary> public static void Clear() { IDictionaryEnumerator CacheEnum = _cache.GetEnumerator(); ArrayList al = new ArrayList(); while (CacheEnum.MoveNext()) { al.Add(CacheEnum.Key); } <div> foreach (string key in al){ _cache.Remove(key); } } /// <summary> /// 删除缓存 /// </summary> /// <param name="pattern">缓存键正则匹配模式</param> public static void RemoveByPattern(string pattern) { IDictionaryEnumerator CacheEnum = _cache.GetEnumerator(); Regex regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled); while (CacheEnum.MoveNext()) { if (regex.IsMatch(CacheEnum.Key.ToString())) _cache.Remove(CacheEnum.Key.ToString()); } } /// <summary> /// 删除缓存 /// </summary> /// <param name="key">缓存键名</param> public static void Remove(string key) { _cache.Remove(key); } /// <summary> /// 增加缓存项目 /// </summary> /// <param name="key">缓存键名</param> /// <param name="obj">缓存对象</param> public static void Insert(string key, object obj) { Insert(key, obj, null, 1); } /// <summary> /// 增加缓存项目(缓存时间:小时缓存因子*12) /// </summary> /// <param name="key">缓存键名</param> /// <param name="obj">缓存对象</param> /// <param name="dep">缓存依赖荐</param> public static void Insert(string key, object obj, CacheDependency dep) { Insert(key, obj, dep, HourFactor * 12); } /// <summary> /// 增加缓存项目 /// </summary> /// <param name="key">缓存键名</param> /// <param name="obj">缓存对象</param> /// <param name="seconds">缓存秒数</param> public static void Insert(string key, object obj, int seconds) { Insert(key, obj, null, seconds); } /// <summary> /// 增加缓存项目 /// </summary> /// <param name="key">缓存键名</param> /// <param name="obj">缓存对象</param> /// <param name="seconds">缓存秒数</param> /// <param name="priority">缓存优先级</param> public static void Insert(string key, object obj, int seconds, CacheItemPriority priority) { Insert(key, obj, null, seconds, priority); } /// <summary> /// 增加缓存项目 /// </summary> /// <param name="key">缓存键名</param> /// <param name="obj">缓存对象</param> /// <param name="dep">缓存依赖项</param> /// <param name="seconds">缓存秒数</param> public static void Insert(string key, object obj, CacheDependency dep, int seconds) { Insert(key, obj, dep, seconds, CacheItemPriority.Normal); } /// <summary> /// 增加缓存 /// </summary> /// <param name="key">缓存键名</param> /// <param name="obj">缓存对象</param> /// <param name="dep">缓存依赖项</param> /// <param name="seconds">缓存秒数</param> /// <param name="priority">缓存优先级</param> 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(Factor * seconds), Cache.NoSlidingExpiration, priority, null); } } /// <summary> /// 微小缓存 /// </summary> /// <param name="key">缓存键名</param> /// <param name="obj">缓存对象</param> /// <param name="secondFactor">缓存秒因子</param> public static void MicroInsert(string key, object obj, int secondFactor) { if (obj != null) { _cache.Insert(key, obj, null, DateTime.Now.AddSeconds(Factor * secondFactor), Cache.NoSlidingExpiration); } } <div> /// <summary> /// 增加缓存项目 /// </summary> /// <param name="key"></param> /// <param name="obj"></param> public static void Max(string key, object obj) { Max(key, obj, null); } /// <summary> /// 最大缓存对象 /// </summary> /// <param name="key">缓存键名</param> /// <param name="obj">缓存对象</param> /// <param name="dep">缓存依赖项</param> public static void Max(string key, object obj, CacheDependency dep) { if (obj != null) { _cache.Insert(key, obj, dep, DateTime.MaxValue, Cache.NoSlidingExpiration, CacheItemPriority.AboveNormal, null); } } /// <summary> /// 获取缓存对象 /// </summary> /// <param name="key">缓存键名</param> /// <returns>返回缓存对象</returns> public static object Get(string key) { return _cache[key]; } /// <summary> /// 根据秒缓存因子计算秒数 /// </summary> public static int SecondFactorCalculate(int seconds) { return Convert.ToInt32(Math.Round((double)seconds * SecondFactor)); } #endregion } }
试试其它关键字
缓存管理
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
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