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 ...
随机推荐
- JZOJ 5281 钦点
样例输入: 4 4 2 a a b b a a b b c c d d c c d d 1 1 3 3 2 2 3 1 1 3 2 2 样例输出: d d c c d d c c b b a a ...
- 【VBA】全局数组定义
[说明] 全局数组定义(写在Module的最上面) 'Array Public Arr_approver Public Arr_delegator Public Arr_Role
- 使用caffe测试自己的图片
第一种方法是测试批量图片,使用caffe.bin即可,首先要做的是把你的jpg图片转换为LMDB的格式,如何转换呢?用/build/tools/convert_image --resize_width ...
- python请求带cookie
先获得cookie到文件 import cookielib import urllib2 #设置保存cookie的文件,同级目录下的cookie.txt filename = 'cookie.txt' ...
- iOS - 倒计时封装
+(NSString *)countdownStartTime:(NSString *)startTime{ NSString *TIME = [startTime substringToIndex: ...
- Xor路
Xor路 (xor.pas/c/cpp)128MB1s 给定一棵有N个点和N-1条边的树,请你求出树中的最长路径,以及总共有多少条最长路径. 这里路径长度是用xor定义的,即若经过的边的权值为a1, ...
- VBCodeProvider .net compiler service interface or something like that
https://msdn.microsoft.com/zh-cn/library/microsoft.visualbasic.vbcodeprovider%28v=vs.110%29.aspx ref ...
- Linux Mint---安装篇
先前接触过Debian6,埋头折腾已经是好几年前的事情了,后来就放下了,现在工作之余时间比较多,正好debian8.1出来了,于是乎装了个debian用,结果,自己其实还是一个菜蛋而已,连软件源怎么设 ...
- DOS头结构
DOS头结构typedef struct _IMAGE_DOS_HEADER { // DOS .EXE header +0h WORD e_magic; ...
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---3
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下: <Linux命令行与shell脚本 ...