HttpRuntime Cache用法及参数解释
自己用到的: HttpRuntime.Cache.Insert("SchoolBindKcChangci", SchoolBindKcChangci, null, DateTime.MaxValue, TimeSpan.Zero);
DateTime.MaxValue最大有效时间
存Cache方法:
HttpRuntime.Cache.Add(
KeyName,//缓存名
KeyValue,//要缓存的对象
Dependencies,//依赖项
AbsoluteExpiration,//绝对过期时间
SlidingExpiration,//相对过期时间
Priority,//优先级
CacheItemRemovedCallback);//缓存过期引发事件
示例:
HttpRuntime.Cache.Add("CurrencyFundCodeCache", docs, null, DateTime.Now.AddMinutes(2), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
封装一下:
/// <summary>
/// 获得缓存
/// </summary>
/// <param name="cacheName"></param>
/// <returns></returns>
public static object GetRuntimeCache(string cacheName)
{
return HttpRuntime.Cache[cacheName];
}
/// <summary>
/// 更新插入缓存
/// </summary>
/// <param name="cacheName"></param>
/// <param name="value"></param>
/// <param name="expiresAt"></param>
public static void SetRuntimeCache(string cacheName, object value, DateTime expiresAt)
{
HttpRuntime.Cache.Add(cacheName, value, null, expiresAt,Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
}
从Cache中取出数据示例:
MongoCursor<BsonDocument> Data = HttpRuntime.Cache["KeyName"] as MongoCursor<BsonDocument>;
判断Cache中的数据是否已经过期:
if (Data != null)
可以尝试把cache转成字典类
lz还没试过:http://hi.baidu.com/sainaxingxing/item/0620704738289ad6c1a592de
protected void RemoveAllCache()
{ System.Web.Caching.Cache _cache = HttpRuntime.Cache;
IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
ArrayList al = new ArrayList();
while (CacheEnum.MoveNext())
{
al.Add(CacheEnum.Key);
} foreach (string key in al)
{
_cache.Remove(key);
}
show();
}
//显示所有缓存
void show()
{
string str = "";
IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator(); while (CacheEnum.MoveNext())
{
str += "缓存名<b>[" + CacheEnum.Key+"]</b><br />" ;
}
this.Label1.Text = "当前网站总缓存数:" + HttpRuntime.Cache.Count + "<br />"+str;
} 二.详细
System.Web.HttpRuntime.Cache的方法:
Add
Insert
Get
Remove
缓存的操作包括:读、写。 读取缓存内容调用System.Web.HttpRuntime.Cache.Get(Key)方法,插入缓存数据调用Add或Insert方法。
Add与Insert的不同
HttpRuntime.Cache.Add 存在相同的键会异常,返回缓存成功的对象。
HttpRuntime.Cache.Insert存在相同的键会替换原值,无返回值。
如果您希望某个缓存项目一旦放入缓存后,就不要再被修改,那么调用Add确实可以防止后来的修改操作。而调用Insert方法,则永远会覆盖已存在项。
缓存的过期时间
缓存过期时间包括:绝对过期和滑动过期。
绝对过期:到了指定时间以后便会失效。
滑动过期:在指定时间内无访问请求便失效。
实例:
绝对过期:
HttpRuntime.Cache.Insert(key, value, null, DateTime.Now.AddSeconds(seconds),System.Web.Caching.Cache.NoSlidingExpiration);
滑动过期:
HttpRuntime.Cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration , TimeSpan.FromSeconds(seconds));
缓存项移除优先级
// 指定 Cache 对象中存储的项的相对优先级。
public enum CacheItemPriority
{
// 在服务器释放系统内存时,具有该优先级级别的缓存项最有可能被从缓存删除。
Low = 1, // 在服务器释放系统内存时,具有该优先级级别的缓存项比分配了 CacheItemPriority.Normal
// 优先级的项更有可能被从缓存删除。
BelowNormal = 2, // 在服务器释放系统内存时,具有该优先级级别的缓存项很有可能被从缓存删除,
// 其被删除的可能性仅次于具有 CacheItemPriority.Low
// 或 CacheItemPriority.BelowNormal 优先级的那些项。这是默认选项。
Normal = 3, // 缓存项优先级的默认值为 CacheItemPriority.Normal。
Default = 3, // 在服务器释放系统内存时,具有该优先级级别的缓存项被删除的可能性
// 比分配了 CacheItemPriority.Normal 优先级的项要小。
AboveNormal = 4, // 在服务器释放系统内存时,具有该优先级级别的缓存项最不可能被从缓存删除。
High = 5, // 在服务器释放系统内存时,具有该优先级级别的缓存项将不会被自动从缓存删除。
// 但是,具有该优先级级别的项会根据项的绝对到期时间或可调整到期时间与其他项一起被移除。
NotRemovable = 6,
} 三.详细
1、HttpRuntime.Cache 相当于就是一个缓存具体实现类,这个类虽然被放在了 System.Web 命名空间下了。但是非 Web 应用也是可以拿来用的。
2、HttpContext.Cache 是对上述缓存类的封装,由于封装到了 HttpContext ,局限于只能在知道 HttpContext 下使用,即只能用于 Web 应用。
综上所属,在可以的条件,尽量用 HttpRuntime.Cache ,而不是用 HttpContext.Cache 。
有以下几条缓存数据的规则。
第一,数据可能会被频繁的被使用,这种数据可以缓存。 第二,数据的访问频率非常高,或者一个数据的访问频率不高,但是它的生存周期很长,这样的数据最好也缓存起来。 第三是一个常常被忽略的问题,有时候我们缓存了太多数据,通常在一台X86的机子上,如果你要缓存的数据超过800M的话,就会出现内存溢出的错误。所以说缓存是有限的。换名话说,你应该估计缓存集的大小,把缓存集的大小限制在10以内,否则它可能会出问题。在Asp.net中,如果缓存过大的话也会报内存溢出错误,特别是如果缓存大的DataSet对象的时候。
你应该认真分析你的程序。根据实际情况来看哪里该用,哪里不该用。如:cache用得过多也会增大服务器的压力。整页输出缓存,又会影响数据的更新。 如果真的需要缓存很大量的数据,可以考虑静态技术。
下面介绍HttpRuntime.Cache常用方法:
using System;
using System.Web;
using System.Collections;
public class CookiesHelper {
/**//// <summary>
/// 获取数据缓存
/// </summary>
/// <param name="CacheKey">键</param>
public static object GetCache(string CacheKey)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
return objCache[CacheKey];
}
/**//// <summary>
/// 设置数据缓存
/// </summary>
public static void SetCache(string CacheKey, object objObject)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject);
}
/**//// <summary>
/// 设置数据缓存
/// </summary>
public static void SetCache(string CacheKey, object objObject, TimeSpan Timeout)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject, null, DateTime.MaxValue, Timeout, System.Web.Caching.CacheItemPriority.NotRemovable, null); }
/**//// <summary>
/// 设置数据缓存
/// </summary>
public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
}
/**//// <summary>
/// 移除指定数据缓存
/// </summary>
public static void RemoveAllCache(string CacheKey)
{
System.Web.Caching.Cache _cache = HttpRuntime.Cache;
_cache.Remove(CacheKey);
}
/**//// <summary>
/// 移除全部缓存
/// </summary>
public static void RemoveAllCache()
{
System.Web.Caching.Cache _cache = HttpRuntime.Cache;
IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
while (CacheEnum.MoveNext())
{
_cache.Remove(CacheEnum.Key.ToString());
}
}
}
HttpRuntime Cache用法及参数解释的更多相关文章
- Wget用法、参数解释
wget功能的强大就不用多说了,在高手手里,它就像是个无往不利的杀人利器,下面是转载的一篇Wget用法.参数解释的比较好的一个文章,当然最好的老师还是man wget 是一个从网络上自动下载文件的自由 ...
- Wget用法、参数解释的比较好的一个文章
wget是一个从网络上自动下载文件的自由工具.它支持HTTP,HTTPS和FTP协议,可以使用HTTP代理. 所谓的自动下载是指,wget可以在用户退出系统的之后在后台执行.这意味这你可以登录系统,启 ...
- HttpRuntime.Cache .Net自带的缓存类
.Net自带的缓存有两个,一个是Asp.Net的缓存 HttpContext.Cache,一个是.Net应用程序级别的缓存,HttpRuntime.Cache. MSDN上有解释说: HttpCont ...
- Cache及(HttpRuntime.Cache与HttpContext.Current.Cache)
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/avon520/archive/2009/11/25/4872704.aspx .NET中Cache有两种调用方式:Ht ...
- libsvm的安装,数据格式,常见错误,grid.py参数选择,c-SVC过程,libsvm参数解释,svm训练数据,libsvm的使用详解,SVM核函数的选择
直接conda install libsvm安装的不完整,缺几个.py文件. 第一种安装方法: 下载:http://www.csie.ntu.edu.tw/~cjlin/cgi-bin/libsvm. ...
- 缓存 HttpContext.Current.Cache和HttpRuntime.Cache的区别
先看MSDN上的解释: HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象. HttpRuntime.Cache:获取当前应用程序的Cache. 我们再用. ...
- HttpContext.Current.Cache 和HttpRuntime.Cache的区别
先看MSDN上的解释: HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象. HttpRuntime.Cache:获取当前应用程序的Cac ...
- ASP.NET HttpRuntime.Cache缓存类使用总结
1.高性能文件缓存key-value存储—Redis 2.高性能文件缓存key-value存储—Memcached 备注:三篇博文结合阅读,简单理解并且使用,如果想深入学习,请多参考文章中给出的博文地 ...
- HttpContext.Current.Cache 和 HttpRuntime.Cache 区别
原文地址:http://blog.csdn.net/avon520/article/details/4872704 .NET中Cache有两种调用方式:HttpContext.Current.Cach ...
随机推荐
- [ZJOI2011][bzoj2229] 最小割 [最小割树]
题面 传送门 思路 首先我们明确一点:这道题不是让你把$n^2$个最小割跑一遍[废话] 但是最小割过程是必要的,因为最小割并没有别的效率更高的算法(Stoer-Wagner之类的?) 那我们就要尽量找 ...
- Redis集群_主从配置
链接地址http://www.2cto.com/database/201502/377069.html 收藏备用. Redis主从配置(Master-Slave) 一. Redis Replicati ...
- Python 读取 pkl文件
使用python 的cPickle 库中的load函数,可以读取pkl文件的内容 import cPickle as pickle fr = open('mnist.pkl') #open的参数是pk ...
- Codeforces Round #363 (Div. 2) C dp或贪心 两种方法
Description Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasy ...
- iOS 关于请求参数在cookie里面
一.首先了解一下什么cookie cookie是在客户端存储服务器状态的一种机制.web服务器可以通过set-cookie或者set-cookie2 HTTP头部设置cookie. Cookie可以分 ...
- .NET4.5,MVC4preview,VS2011preview.都来了
原文发布时间为:2011-11-06 -- 来源于本人的百度文章 [由搬家工具导入] http://weblogs.asp.net/jgalloway/archive/2011/09/14/get-t ...
- asp.net几种开源上传控件,flash,ajax版,支持多文件
原文发布时间为:2010-03-18 -- 来源于本人的百度文章 [由搬家工具导入] 1、AspnetUpload 地址:http://www.aspnetupload.net/ 最早接触的上传控件。 ...
- 关于apache 虚拟主机配置访问403的问题
<Directory /> Options FollowSymLinks Order allow,deny Allow from all</Directory&g ...
- LeetCode OJ-- Sudoku Solver ***
https://oj.leetcode.com/problems/sudoku-solver/ 九宫格数独问题. 一行上为1 2 3 到9 一列上为1 2 3 到9 每个小的3*3格子为 1 2 3 ...
- DB2数据库报 [SQL0805N Package "NULLID.SQLLD003" was not found.]
解决办法: cd /home/db2inst1/sqllib/bnddb2 bind @db2cli.lst blocking all grant public sqlerror continue C ...