Asp.net中全局缓存的几种方式
public class StaticCacheTest
{
private static IDictionary<string, object> _dic;
private static object locker = new object();
private static IDictionary<string, object> CachedDic
{
get
{
if (_dic == null)
{
lock (locker)
{
if (_dic == null)
{
_dic = new Dictionary<string, object>();
}
}
}
return _dic;
}
}
public static object GetObject(string key)
{
return CachedDic[key];
}
public static void SetObject(string key, object obj)
{
lock (locker)
{
CachedDic[key] = obj;
}
}
}
public partial class _Default : System.Web.UI.Page
{
private const string KEY = "CurrentTime";
protected void Page_Load(object sender, EventArgs e)
{
String curTime = System.DateTime.Now.ToString();
if (HttpContext.Current.Application[KEY] == null)
{
if (HttpContext.Current.Cache[KEY] == null)
{
HttpContext.Current.Cache.Insert(KEY, curTime);
}
if (StaticCacheTest.GetObject(KEY) == null) //本质上就是HttpRuntime.Cache的实现方式
{
StaticCacheTest.SetObject(KEY, curTime);
}
if (HttpRuntime.Cache[KEY] == null)
{
HttpRuntime.Cache.Insert(KEY, curTime); //HttpRuntime.Cache与HttpContext.Current.Cache是同一对象,建议使用HttpRuntime.Cache
}
TextBox1.Text = HttpContext.Current.Application[KEY].ToString();
TextBox2.Text=HttpContext.Current.Cache[KEY].ToString();
TextBox3.Text=StaticCacheTest.GetObject(KEY).ToString();
}
}
{
private static IDictionary<string, object> _dic;
private static object locker = new object();
private static IDictionary<string, object> CachedDic
{
get
{
if (_dic == null)
{
lock (locker)
{
if (_dic == null)
{
_dic = new Dictionary<string, object>();
}
}
}
return _dic;
}
}
public static object GetObject(string key)
{
return CachedDic[key];
}
public static void SetObject(string key, object obj)
{
lock (locker)
{
CachedDic[key] = obj;
}
}
}
public partial class _Default : System.Web.UI.Page
{
private const string KEY = "CurrentTime";
protected void Page_Load(object sender, EventArgs e)
{
String curTime = System.DateTime.Now.ToString();
if (HttpContext.Current.Application[KEY] == null)
{
HttpContext.Current.Application.Lock();
HttpContext.Current.Application[KEY] = curTime;
HttpContext.Current.Application.UnLock();
}HttpContext.Current.Application[KEY] = curTime;
HttpContext.Current.Application.UnLock();
if (HttpContext.Current.Cache[KEY] == null)
{
HttpContext.Current.Cache.Insert(KEY, curTime);
}
if (StaticCacheTest.GetObject(KEY) == null) //本质上就是HttpRuntime.Cache的实现方式
{
StaticCacheTest.SetObject(KEY, curTime);
}
if (HttpRuntime.Cache[KEY] == null)
{
HttpRuntime.Cache.Insert(KEY, curTime); //HttpRuntime.Cache与HttpContext.Current.Cache是同一对象,建议使用HttpRuntime.Cache
}
TextBox1.Text = HttpContext.Current.Application[KEY].ToString();
TextBox2.Text=HttpContext.Current.Cache[KEY].ToString();
TextBox3.Text=StaticCacheTest.GetObject(KEY).ToString();
}
}
两年多没有搞Asp.net了,对于全局存储一些东西的机制居然也都搞不清了,今天特意做个测试验证一下,做个记录供自己以后参考,记录如下:
- HttpContext.Current.Application:整个应用程序都可以共享的,当然存储的时候应该加锁的。
- HttpRuntime.Cache与HttpContext.Current.Cache:二者其实是指向的同一对象,区别在于HttpContext与HttpRuntime的实现上。HttpContext必需是Asp.net的上下文中使用,而HttpRuntime则可以在任何上下文中使用,例如可以在控制台程序中利用HttpRuntime作为缓存。
- 静态变量的方式:本质上与HttpRuntime.Cache的实现原理是一样的(Cache的也是静态变量)。
第1与第3只适合存储少量数据(小于1M),否则性能上会有损失。而HttpRuntime.Cache则在内存的使用效率上和功能上(例如缓存依赖、过期策略等)都有所增强,所以应该在实际应用中尽量使用它。
一点说明:为什么在标题中要嵌入英文?原因是为了能够让国外的网友能查询到这篇文章。平常在Google上查资料的时候,经常参考国外网友的博客,帮助我解决了很多问题,所以我也想让他们能够参考我写的内容。当然文中我不可能全部译为英文,所以我尽量把代码粘全,靠代码说话吧。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端