【缓存】.net中Cache管理操作
using System;
using System.Web;
using System.Web.Caching;
using System.Collections;
/// <summary>
/// 设置Cache操作类
/// </summary>
public class SetCache
{
#region 用户自定义变量
private static readonly Cache _cache;//缓存实例
private static readonly int hourfactor;
#endregion
#region 构造函数
static SetCache()
{
hourfactor = 3600;
_cache = HttpRuntime.Cache;
}
private SetCache()
{
}
#endregion
#region 清除所有缓存
/// <summary>
/// 清除所有缓存
/// </summary>
public static void Clear()
{
//要循环访问 Cache 对象的枚举数
IDictionaryEnumerator enumerator = _cache.GetEnumerator();//检索用于循环访问包含在缓存中的键设置及其值的字典枚举数
if (enumerator != null)
{
while (enumerator.MoveNext())
{
_cache.Remove(enumerator.Key.ToString());
}
}
}
#endregion
#region 得到缓存实例
/// <summary>
/// 得到缓存实例
/// </summary>
/// <param name="key">缓存实例名称</param>
/// <returns>返回缓存实例</returns>
public static object GetCache(string key)
{
return _cache[key];
}
#endregion
#region 缓存实例插入
/// <summary>
/// 缓存实例插入(默认缓存20分钟)
/// </summary>
/// <param name="key">缓存实例名称</param>
/// <param name="obj">要缓存的对象</param>
public static void Insert(string key, object obj)
{
CacheDependency dep = null;
Insert(key, obj, dep, 20);
}
/// <summary>
/// 缓存实例插入
/// </summary>
/// <param name="key">缓存实例名称</param>
/// <param name="obj">要缓存的对象</param>
/// <param name="seconds">缓存的时间</param>
public static void Insert(string key, object obj, int seconds)
{
CacheDependency dep = null;
Insert(key, obj, dep, seconds);
}
/// <summary>
/// 缓存实例插入(缓存过期时间是一天)
/// </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="xmlPath">缓存的依赖项xml文件的路径(绝对路径)</param>
public static void Insert(string key, object obj, string xmlPath)
{
CacheDependency dep = new CacheDependency(xmlPath);
Insert(key, obj, dep, hourfactor * 12);
}
/// <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">该项的文件依赖项或缓存键依赖项。当任何依赖项更改时,该对象即无效,并从缓存中移除。如果没有依赖项,则此参数包含空引用(Visual Basic 中为 Nothing)</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="xmlPath">缓存的依赖项xml文件的路径(绝对路径)</param>
/// <param name="seconds">所插入对象将过期并被从缓存中移除的时间。</param>
public static void Insert(string key, object obj, string xmlPath, int seconds)
{
CacheDependency dep = new CacheDependency(xmlPath);
Insert(key, obj, dep, seconds, CacheItemPriority.Normal);
}
/// <summary>
/// 缓存实例插入
/// </summary>
/// <param name="key">用于引用该对象的缓存键</param>
/// <param name="obj">要插入缓存中的对象</param>
/// <param name="dep">该项的文件依赖项或缓存键依赖项。当任何依赖项更改时,该对象即无效,并从缓存中移除。如果没有依赖项,则此参数包含空引用(Visual Basic 中为 Nothing)。</param>
/// <param name="seconds">所插入对象将过期并被从缓存中移除的时间。</param>
/// <param name="priority">该对象相对于缓存中存储的其他项的成本,由 CacheItemPriority 枚举表示。该值由缓存在退出对象时使用;具有较低成本的对象在具有较高成本的对象之前被从缓存移除。 </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((double)seconds), TimeSpan.Zero, priority, null);
}
}
#endregion
#region 移出单个缓存
/// <summary>
/// 移出单个缓存
/// </summary>
/// <param name="key">缓存实例名称</param>
public static void Remove(string key)
{
_cache.Remove(key);
}
#endregion
#region 得到所有使用的Cache键值
/// <summary>
/// 得到所有使用的Cache键值
/// </summary>
/// <returns>返回所有的Cache键值</returns>
public static ArrayList GetAllCacheKey()
{
ArrayList arrList = new ArrayList();
IDictionaryEnumerator enumerator = _cache.GetEnumerator();
if (enumerator != null)
{
while (enumerator.MoveNext())
{
arrList.Add(enumerator.Key);
}
}
return arrList;
}
#endregion
}
.src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px}
.src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px}
.src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden}
.toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;}
.src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif}
.src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important}
.number_show{ padding-left:52px !important; list-style:decimal outside !important}
.number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd}
.number_hide{ padding-left:0px !important; list-style-type:none !important}
.number_hide li{ list-style-type:none !important; border-left:0px}
ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd}
ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}
.linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word}
ol.mainarea li pre.alt{ background-color:#f7f7ff !important}
【缓存】.net中Cache管理操作的更多相关文章
- nodejs 搭建自己的简易缓存cache管理模块
http://www.infoq.com/cn/articles/built-cache-management-module-in-nodejs/ 为什么要搭建自己的缓存管理模块? 这个问题其实也是在 ...
- Redis-基本概念、java操作redis、springboot整合redis,分布式缓存,分布式session管理等
NoSQL的引言 Redis数据库相关指令 Redis持久化相关机制 SpringBoot操作Redis Redis分布式缓存实现 Resis中主从复制架构和哨兵机制 Redis集群搭建 Redis实 ...
- ASP.NET缓存中Cache过期的三种策略
原文:ASP.NET缓存中Cache过期的三种策略 我们在页面上添加三个按钮并双击按钮创建事件处理方法,三个按钮使用不同的过期策略添加ASP.NET缓存. <asp:Button ID=&quo ...
- HTML 5 应用程序缓存(Application Cache)cache manifest 文件使用 html5 中创建manifest缓存以及更新方法 一个manifest文件会创建一份缓存,不同的manifest文件其缓存的内容是互不干扰的
HTML5 离线缓存-manifest简介 HTML 5 应用程序缓存 使用 HTML5,通过创建 cache manifest 文件,可以轻松地创建 web 应用的离线版本. 什么是应用程序缓存(A ...
- SQL Server 查看数据库在数据缓存(data cache)中占用的空间大小
use master go select * from sys.dm_os_buffer_descriptors go --查看数据库在数据缓存(data cache)中占用的空间大小 --由于每个数 ...
- [Cache] C#操作缓存--CacheHelper缓存帮助类 (转载)
点击下载 CacheHelper.zip CacheHelper 缓存帮助类 C#怎么操作缓存 怎么设置和取缓存数据,都在这个类里面呢 下面看一下代码吧 /// <summary> /// ...
- 背水一战 Windows 10 (90) - 文件系统: 获取 Package 中的文件, 可移动存储中的文件操作, “库”管理
[源码下载] 背水一战 Windows 10 (90) - 文件系统: 获取 Package 中的文件, 可移动存储中的文件操作, “库”管理 作者:webabcd 介绍背水一战 Windows 10 ...
- spring对数据库的操作、spring中事务管理的介绍与操作
jdbcTemplate的入门 创建maven工程 此处省略 导入依赖 <!-- https://mvnrepository.com/artifact/org.springframework/s ...
- Spring中使用RedisTemplate操作Redis(spring-data-redis)
RedisTemplate如何检查一个key是否存在? return getRedisTemplate().hasKey(key); 由一个问题,复习了一下redis 抄自: https://www. ...
随机推荐
- CF 518 D. Ilya and Escalator
Ilya got tired of sports programming, left university and got a job in the subway. He was given the ...
- dede织梦列表页如何调用全站子栏目
网站原代码:{dede:channel type='son'} <a href="[field:typelink/]">[field:typename/]</a& ...
- Run Configuration error:broken configuration due to unavailable
希望大家一起来,毕竟大家都不会使用这个Androidstudio,一起扩展这方面的知识量 http://forums.opengamma.com/t/intellij-code-compiles-bu ...
- [ActionScript 3.0] 跨域策略文件crossdomain.xml配置详解
1.简介 flash在跨域时唯一的限制策略就是crossdomain.xml文件,该文件限制了flash是否可以跨域读写数据以及允许从什么地方跨域读写数据. 位于www.a.com域中的SWF文件要访 ...
- 四个排名函数(row_number、rank、dense_rank和ntile)的比较
排名函数是SQL Server2005新加的功能.在SQL Server2005中有如下四个排名函数: 1.row_number 2.rank 3.dense_rank 4.ntile 下面分别介绍一 ...
- log4net 日志写入MongoDB 实现分布式日志
本人在.net framework 4.5下测试成功,首先需要安装log4mongo-net组件,见https://www.nuget.org/packages/log4mongo-net/ vs20 ...
- Arcgis9.3下栅格数据的坐标转换出错
Arcgis9.3下栅格数据的坐标转换出错 在win7系统下的arcgis9.3,使用toolbox里raster project工具总是出错,后来打了sp1补丁就没问题了,想不到arcgis还有这样 ...
- [HDU 2602]Bone Collector ( 0-1背包水题 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 水题啊水题 还给我WA了好多次 因为我在j<w[i]的时候状态没有下传.. #includ ...
- IntelliJ IDEA显示行号方法
File->Settings->Editor->General->Appearence->Show line numbers
- (转)C# SSL-X509使用
X.509 给出的鉴别框架是一种基于公开密钥体制的鉴别业务密钥管理.一个用户有两把密钥:一把是用户的专用密钥(简称为:私钥),另一把是其他用户都可得到和利用的公共密钥(简称为:公钥).该鉴别框架允许用 ...