HttpRuntime.Cache
a.在Web开发中,我们经常能够使用到缓存对象(Cache),在ASP.NET中提供了两种缓存对象,HttpContext.Current.Cache和HttpRuntime.Cache,那么他们有什么区别呢?下面简单描述一下:
(1):HttpContext.Current.Cache 为当前Http请求获取Cache对象,通俗来说就是由于此缓存封装在了HttpContenxt中,而HttpContext只局限于Web中,所以此缓存信息只能够在Web中使用。
(2):HttpRuntime.Cache 获取当前应用程序的Cache,通俗来说就是此缓存信息虽然被放在了System.Web命名空间下,但是非Web程序也可以使用此缓存。
----------------------------------属性介绍-----------------------------------------------------------------------------------
HttpRuntime.Cache.Add(
KeyName,//缓存名 KeyValue,//要缓存的对象 Dependencies,//依赖项 AbsoluteExpiration,//绝对过期时间 SlidingExpiration,//相对过期时间 Priority,//优先级 CacheItemRemovedCallback);//缓存过期回调函数//
// 摘要:
// 向 System.Web.Caching.Cache 中插入具有依赖项和到期策略的对象。
//
// 参数:
// key:
// 用于引用该对象的缓存键。
//
// value:
// 要插入缓存中的对象。
//
// dependencies:
// 所插入对象的文件依赖项或缓存键依赖项。 当任何依赖项更改时,该对象即无效,并从缓存中移除。 如果没有依赖项,则此参数包含 null。
//
// absoluteExpiration:
// 所插入对象将到期并被从缓存中移除的时间。 要避免可能的本地时间问题(例如从标准时间改为夏时制),请使用 System.DateTime.UtcNow
// 而不是 System.DateTime.Now 作为此参数值。 如果使用绝对到期,则 slidingExpiration 参数必须为 System.Web.Caching.Cache.NoSlidingExpiration。
//
// slidingExpiration:
// 最后一次访问所插入对象时与该对象到期时之间的时间间隔。 如果该值等效于 20 分钟,则对象在最后一次被访问 20 分钟之后将到期并被从缓存中移除。
// 如果使用可调到期,则 absoluteExpiration 参数必须为 System.Web.Caching.Cache.NoAbsoluteExpiration。
//
// 异常:
// System.ArgumentNullException:
// key 或 value 参数为 null。
//
// System.ArgumentOutOfRangeException:
// 将 slidingExpiration 参数设置为小于 TimeSpan.Zero 或大于一年的等效值。
//
// System.ArgumentException:
// 为要添加到 Cache 中的项设置 absoluteExpiration 和 slidingExpiration 参数。
public void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration);
---------------------------------------------------------------------------------------------
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,
}
--------------------公共类-------------------------------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// HttpRuntime Cache读取设置缓存信息封装
/// 使用描述:给缓存赋值使用HttpRuntimeCache.Set(key,value....)等参数(第三个参数可以传递文件的路径(HttpContext.Current.Server.MapPath()))
/// 读取缓存中的值使用JObject jObject=HttpRuntimeCache.Get(key) as JObject,读取到值之后就可以进行一系列判断
/// </summary>
public class HttpRuntimeCache
{
/// <summary>
/// 设置缓存过期时间,可从配置文件中读取
/// </summary>
private static double Seconds = string.IsNullOrEmpty(ConfigurationManager.AppSettings["AbsoluteExpiration"]) ? 10 : Double.Parse(ConfigurationManager.AppSettings["AbsoluteExpiration"]);
/// <summary>
/// 缓存指定对象,设置缓存
/// </summary>
public static bool Set(string key, object value)
{
return Set(key, value, null, DateTime.Now.AddSeconds(Seconds), Cache.NoSlidingExpiration, CacheItemPriority.Default, CachedItemRemoveCallBack);
}
/// <summary>
/// 缓存指定对象,设置缓存
/// </summary>
public static bool Set(string key, object value, string path)
{
try
{
var cacheDependency = new CacheDependency(path);
return Set(key, value, cacheDependency);
}
catch
{
return false;
}
}
/// <summary>
/// 缓存指定对象,设置缓存
/// </summary>
public static bool Set(string key, object value, CacheDependency cacheDependency)
{
return Set(key, value, cacheDependency, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
CacheItemPriority.Default, null);
}
/// <summary>
/// 缓存指定对象,设置缓存
/// </summary>
public static bool Set(string key, object value, double seconds, bool isAbsulute)
{
return Set(key, value, null, (isAbsulute ? DateTime.Now.AddSeconds(seconds) : Cache.NoAbsoluteExpiration),
(isAbsulute ? Cache.NoSlidingExpiration : TimeSpan.FromSeconds(seconds)), CacheItemPriority.Default, CachedItemRemoveCallBack);
}
/// <summary>
/// 获取缓存对象
/// </summary>
public static object Get(string key)
{
return GetPrivate(key);
}
/// <summary>
/// 判断缓存中是否含有缓存该键
/// </summary>
public static bool Exists(string key)
{
return (GetPrivate(key) != null);
}
/// <summary>
/// 移除缓存对象
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool Remove(string key)
{
if (string.IsNullOrEmpty(key))
{
return false;
}
HttpRuntime.Cache.Remove(key);
return true;
}
/// <summary>
/// 移除所有缓存
/// </summary>
/// <returns></returns>
public static bool RemoveAll()
{
IDictionaryEnumerator iDictionaryEnumerator = HttpRuntime.Cache.GetEnumerator();
while (iDictionaryEnumerator.MoveNext())
{
HttpRuntime.Cache.Remove(Convert.ToString(iDictionaryEnumerator.Key));
}
return true;
}
//------------------------提供给上面方法进行调用-----------------------------------
/// <summary>
/// 设置缓存
/// </summary>
public static bool Set(string key, object value, CacheDependency cacheDependency, DateTime absoluteExpiration,
TimeSpan slidingExpiration, CacheItemPriority cacheItemPriority, CacheItemRemovedCallback cacheItemRemovedCallback)
{
if (string.IsNullOrEmpty(key) || value == null)
{
return false;
}
HttpRuntime.Cache.Insert(key, value, cacheDependency, absoluteExpiration, slidingExpiration, cacheItemPriority,
cacheItemRemovedCallback);
return true;
}
/// <summary>
/// 获取缓存
/// </summary>
private static object GetPrivate(string key)
{
return string.IsNullOrEmpty(key) ? null : HttpRuntime.Cache.Get(key);
}
/// <summary>
/// 缓存到期后的回调函数,更新缓存
/// </summary>
/// <param name="key">缓存名称</param>
/// <param name="value">缓存的值</param>
/// <param name="reason">缓存到期的原因</param>
private static void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason)
{
if (!Exists(key))
{
//更新缓存
}
}
}
HttpRuntime.Cache的更多相关文章
- 缓存 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 备注:三篇博文结合阅读,简单理解并且使用,如果想深入学习,请多参考文章中给出的博文地 ...
- HttpRuntime.Cache的使用经验
配置文件 <appSettings> <add key="EnableCache" value="true"/> "/ ...
- HttpRuntime.Cache 失效
最近做一个报纸内容类网站,为了提高响应速度,将首页各栏目以及二级栏目中Part文献列表存储在HttpRuntime.Cache缓存中,发布后发现问题,刚插入的缓存很快就失效,本机调试没有问题. 由于H ...
- 服务端缓存HttpRuntime.Cache的使用
HttpRuntime.Cache.Insert("缓存key", "缓存content", null, DateTime.Now.AddMinutes(3), ...
- HttpRuntime.Cache被清空的DataTable
将一个DataTable存到Cache中后,另一个页面新建变量并获取,操作变量,Cache中的数据也被改动了? 页面a.aspx 初始化并赋值,输出当前缓存内DataTable内数据条数 Page_L ...
- HttpContext.Current.Cache 和 HttpRuntime.Cache 区别
原文地址:http://blog.csdn.net/avon520/article/details/4872704 .NET中Cache有两种调用方式:HttpContext.Current.Cach ...
- 分布式缓存HttpRuntime.cache应用到单点登陆中_优化登陆
以前的设计方案,是我们在数据库中放一个表,用作存储验证登陆成功的用户,并且生成用户TOKEN(令牌) 分布式缓存+集群的解决方案图: 相应的代码: DE层中配置文件: receiveTimeout=& ...
- Asp.net缓存技术(HttpRuntime.Cache)
一.缓存: 5个等级的缓存 1级是网络级缓存,缓存在浏览器,CDN以及代理服务器中 (举个例子:每个帮助页面都进行了缓存,访问一个页面的代码非常简单) 2级是由.net框架 HttpRuntime ...
随机推荐
- vue 相邻自定义组件渲染错误正确的打开方式
话不多说看问题: 当封装自定义组件时例如(自定义下拉列表)两个相同的组件在多次v-if变化时偶尔会发生渲染错误,明明赋值正确但是组建中的ajax方法可能返回的数据乱掉,或者其他神逻辑错误. 经过查询发 ...
- 【Swift 】- 闭包
闭包是自包含带函数代码块,可以在代码中被传递和使用.我觉得可以这样理解:闭包相当于C#中的lambda表达式: 全局函数和嵌套函数,实际也是特殊的闭包. 通常闭包是以下三种形式: a,全局函数是一个有 ...
- Java之父职场路
Java之父——詹姆斯·高斯林出生于加拿大,是一位计算机编程天才.在卡内基·梅隆大学攻读计算机博士学位时,他编写了多处理器版本的Unix操作系统,是JAVA编程语言的创始人.1991年,在Sun公司工 ...
- 关于bonecp和QuerRunner
之前一直以为boneCP和QueryRunner是绑定的,但是其实不是,后者来自于commons-dbUtils,BoneCP就是负责连接池. while preparing SQL: UPSERT ...
- Java 程序员容易犯的10个SQL错误
Java程序员编程时需要混合面向对象思维和一般命令式编程的方法,能否完美的将两者结合起来完全得依靠编程人员的水准: 技能(任何人都能容易学会命令式编程) 模式(有些人用“模式-模式”,举个例子,模式可 ...
- c# HttpWebResponse 调用WebApi
public static class WebApiCaller { public static string HttpPost(string url, string body) { try { // ...
- 菜鸟天天不懂,那就天天敲它。。。还不懂。。。JAVA数组比较大小。
package com.aini; import java.util.Scanner; //操...为什么数组的大小比较我硬是搞不懂,比较大小依然放在for循环里... //从键盘输入一组数据,并输出 ...
- 根据当前日期算前一年、前一月、前一天(java基础)
问题的本身没有什么难度,但是要想一下子找到一个现成的方法还真不是那么容易,本来以为java.util.Date中会有方法结果找了半天没找到,最后还是在Calendar中找到了,记下别忘了!! 核心:使 ...
- 比赛安排(穷举法或dfs)
比赛安排 时间限制: 1 Sec 内存限制: 125 MB提交: 11 解决: 10[提交][状态][讨论版][命题人:外部导入] 题目描述 设有2n(n<=6)个球队进行单循环比赛,计划在 ...
- 面向对象银角大王补充2-self就是调用当前方法的对象-静态字段,公有属性-封装的理解-继承的理解,普通方法,静态方法
self是什么,就是一个函数,就是一个形式参数 4.self就是调用当前方法的对象 静态字段,公有属性 静态字段使用场景,每个对象中保存相同的东西时,可以使用静态字段,公有属性 5.封装的理解 类中封 ...