博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET缓存全解析4:应用程序数据缓存 转自网络原文作者李天平
阅读量:7220 次
发布时间:2019-06-29

本文共 2843 字,大约阅读时间需要 9 分钟。

System.Web.Caching 命名空间提供用于缓存服务器上常用数据的类。此命名空间包括 Cache 类,该类是一个字典,您可以在其中存储任意数据对象,如哈希表和数据集。它还为这些对象提供了失效功能,并为您提供了添加和移除这些对象的方法。您还可以添加依赖于其他文件或缓存项的对象,并在从 Cache 对象中移除对象时执行回调以通知应用程序。

/// /// 获取当前应用程序指定CacheKey的Cache对象值 ///  /// 索引键值 /// 
返回缓存对象
public static object GetCache(string CacheKey){ System.Web.Caching.Cache objCache = HttpRuntime.Cache; return objCache[CacheKey]; } /// /// 设置当前应用程序指定CacheKey的Cache对象值 /// /// 索引键值 /// 缓存对象 public static void SetCache(string CacheKey, object objObject) {   System.Web.Caching.Cache objCache = HttpRuntime.Cache;   objCache.Insert(CacheKey, objObject); } /// /// 设置当前应用程序指定CacheKey的Cache对象值 /// /// 索引键值 /// 缓存对象 /// 绝对过期时间 /// 最后一次访问所插入对象时与该对象过期时之间的时间间隔 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); } protected void Page_Load(object sender, EventArgs e) {   string CacheKey = "cachetest";   object objModel = GetCache(CacheKey);//从缓存中获取   if (objModel == null)//缓存里没有 {     objModel = DateTime.Now;//把当前时间进行缓存 if (objModel != null) { int CacheTime = 30;//缓存时间////// 获取当前应用程序指定CacheKey的Cache对象值 /// ///索引键值 ///
返回缓存对象
public static object GetCache(string CacheKey){ System.Web.Caching.Cache objCache = HttpRuntime.Cache; return objCache[CacheKey]; } /// /// 设置当前应用程序指定CacheKey的Cache对象值 /// ///索引键值 ///缓存对象 public static void SetCache(string CacheKey, object objObject) {   System.Web.Caching.Cache objCache = HttpRuntime.Cache;   objCache.Insert(CacheKey, objObject); } /// /// 设置当前应用程序指定CacheKey的Cache对象值 /// ///索引键值 ///缓存对象 ///绝对过期时间 ///最后一次访问所插入对象时与该对象过期时之间的时间间隔 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); } protected void Page_Load(object sender, EventArgs e) {   string CacheKey = "cachetest";   object objModel = GetCache(CacheKey);//从缓存中获取   if (objModel == null)//缓存里没有 {     objModel = DateTime.Now;//把当前时间进行缓存 if (objModel != null) {  int CacheTime =30;//缓存时间30秒 SetCache(CacheKey, objModel, DateTime.Now.AddSeconds(CacheTime), TimeSpan.Zero);//写入缓存 } } Label1.Text = objModel.ToString(); }

以上几种方法都很好的解决了数据缓存的问题,但由一个最大的问题是当数据发生变化了,而缓存里还是过期的数据,只有等缓存过期后才会重新获取最新的数据,这样的话,很多时候用户获取的数据都是和实际数据不一致的过期数据。这同样给用户造成了比较大的麻烦,怎么办呢?接着往下看。

转载于:https://www.cnblogs.com/caoheyang911016/p/3209575.html

你可能感兴趣的文章
【火炉炼AI】机器学习039-NLP文本分类器
查看>>
java通过JDBC链接SQLServer2012【转载!!!超详细】
查看>>
cmake使用教程(八)-macro和function
查看>>
掘金广告产品介绍
查看>>
小猪的Python学习之旅 —— 7.Python并发之threading模块(1)
查看>>
腾讯冯宇彦:基于大数据与人工智能的智慧交通云
查看>>
eclipse再见,android studio 新手入门教程(二)项目的导入
查看>>
OpenGL ES 2 0 (iOS) 笔记大纲
查看>>
简述HTTP
查看>>
css笔记
查看>>
SpringBoot应用War包形式部署到外部Tomcat
查看>>
3款你必须知道的爬虫工具
查看>>
NS_OPTIONS枚举的用法
查看>>
智能媒体管理产品文档转换/预览功能介绍(4)--快速搭建
查看>>
5 ReCharts
查看>>
Android app 在线更新那点事儿(适配Android6.0、7.0、8.0)
查看>>
使用 OAuth 2 和 JWT 为微服务提供安全保障 - 基本概念
查看>>
PayPal 概述
查看>>
webpack 3 零基础入门教程 #18 - 构建开发和生产环境-分离配置文件
查看>>
前端技术演进:参考文章
查看>>