缓存通用管理类 + 缓存 HttpContext.Current.Cache 和 HttpRuntime.Cache 的区别

简介:   以前写asp.net时用HttpContext.Current.Cache存缓存很好用,今天写了一个windows服务程序,HttpContext.Current.Cache存缓存的时候还好,取的时候一直报错“未将对象引用到实例”很郁闷,查询了一下资料才明白引用程序缓存要用HttpRuntime.Cache...   我们先看MSDN上的解释:         HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象。

  以前写asp.net时用HttpContext.Current.Cache存缓存很好用,今天写了一个windows服务程序,HttpContext.Current.Cache存缓存的时候还好,取的时候一直报错“未将对象引用到实例”很郁闷,查询了一下资料才明白引用程序缓存要用HttpRuntime.Cache...

  我们先看MSDN上的解释:

        HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象。
        HttpRuntime.Cache:获取当前应用程序的Cache。

附带的写了一个操作缓存的通用类,在应用程序中使用,如果要在asp.net中有,只需把HttpRuntime.Cache改为HttpContext.Current.Cache即可,代码如下:

 

代码
 
  
using System;

/// <summary>
/// author:Stone_W
/// date:2010.12.1
/// desc:缓存的管理类
/// 注意:要添加对引用 System.Web
/// </summary>
public class MyCacheTools : System.Web.SessionState.IRequiresSessionState
{
#region 存入Cache
/// <summary>
/// 存入Cache
/// </summary>
/// <param name="key"> 缓存key </param>
/// <param name="value"> 缓存的值 </param>
/// <param name="time_HH"> 存xx小时 </param>
/// <returns> 是否执行成功[bool] </returns>
public static bool SetCache( string key, object value, int time_HH)
{
bool result = false ;
try
{
DateTime dt
= DateTime.Now.AddHours(time_HH);
System.Web.HttpRuntime.Cache.Insert(key, value,
null ,
dt, System.Web.Caching.Cache.NoSlidingExpiration);
result
= true ;
}
catch (Exception ex) { }
return result;
}
#endregion

#region 取得Cache
/// <summary>
/// 取得Cache
/// </summary>
/// <param name="key"> key </param>
/// <returns> object类型 </returns>
public static object GetCache( string key)
{
return System.Web.HttpRuntime.Cache.Get(key);
}
#endregion

#region 查询Cache是否存在
/// <summary>
/// 查询Cache是否存在
/// </summary>
/// <param name="key"> key 值 </param>
/// <returns> bool </returns>
public static bool IsCacheExist( string key)
{
bool result = false ;

object temp = System.Web.HttpRuntime.Cache.Get(key);
if ( null != temp)
{
result
= true ;
}
return result;
}
#endregion

}

 

 

ps:HttpContext.Current.Cache 为null 这个问题搞的我很痛苦,最后还是解决了,希望此篇文章对大家有用!

 

img_fa0be433d68c8212b2b0b3b1a564ccb1.png
如果本文对你有所帮助,请打赏——1元就足够感动我:)
支付宝打赏 微信打赏
联系邮箱:intdb@qq.com
我的GitHub: https://github.com/vipstone
关注公众号: img_9bde0f31ac4a0eca10b1bd7414b78faf.png


作者: 王磊
出处: http://vipstone.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,请标明出处。

相关文章
|
4月前
|
存储 缓存 API
file_cache: 使用文件缓存函数结果
file_cache: 使用文件缓存函数结果
51 15
|
5月前
|
存储 缓存 监控
中间件Read-Through Cache(直读缓存)策略实现方式
【5月更文挑战第11天】中间件Read-Through Cache(直读缓存)策略实现方式
65 4
中间件Read-Through Cache(直读缓存)策略实现方式
|
5月前
|
缓存 中间件 数据库
中间件Write-Through Cache(直写缓存)策略
【5月更文挑战第7天】中间件Write-Through Cache(直写缓存)策略
76 4
中间件Write-Through Cache(直写缓存)策略
|
5月前
|
存储 缓存 中间件
中间件Read-Through Cache(直读缓存)策略
【5月更文挑战第7天】中间件Read-Through Cache(直读缓存)策略
51 4
中间件Read-Through Cache(直读缓存)策略
|
5月前
|
存储 缓存 中间件
中间件Read-Through Cache(直读缓存)策略工作原理
【5月更文挑战第11天】中间件Read-Through Cache(直读缓存)策略工作原理
58 3
|
5月前
|
存储 缓存 监控
中间件Read-Through Cache(直读缓存)策略注意事项
【5月更文挑战第11天】中间件Read-Through Cache(直读缓存)策略注意事项
32 2
|
5月前
|
存储 缓存 监控
中间件Cache-Aside策略读取操作
【5月更文挑战第9天】
30 2
|
12月前
|
存储 缓存 数据库
【什么是Cache Aside机制】
【什么是Cache Aside机制】
218 0
|
内存技术
cache基础
cache
114 0
|
存储 缓存
cache control 里 no-cache 和 no-store 的区别
cache control 里 no-cache 和 no-store 的区别