HttpContext.Cache属性
HttpContext基于HttpApplication的处理管道,由于HttpContext对象贯穿整个处理过程,所以,可以从HttpApplication处理管道的前端将状态数据传递到管道的后端,完成状态的传递任务做个小demo
1.控制器:
public class TestController : Controller
{ string key = "data"; public ActionResult Index()
{
return View();
} /// <summary>
/// 定时器获取缓存数据
/// </summary>
/// <returns></returns>
[HttpPost] public JsonResult GetData()
{
string data = Convert.ToString(HttpContext.Cache.Get(this.key));
if (!string.IsNullOrEmpty(data))
{
return this.Json(new { success = true, data = data });
}
else
{
return this.Json(new { success = false, time = DateTime.Now.ToString("ss"), data = data });
}
} /// <summary>
/// 打开输入缓存值界面
/// </summary>
/// <returns></returns>
[HttpGet]
public ActionResult CreateCacheData()
{
return View();
} /// <summary>
/// 将数据插入缓存
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
[HttpPost]
public void InsertCache(string value)
{
HttpContext.Cache.Insert(this.key, value); } }
2.视图
Index.cshtml:
@{
ViewBag.Title = "Index";
Layout = null;
}
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.timers-1.2.js"></script>
<button id="btnStart">开始定时器</button>
<script>
$(function ()
{
//定时器开始
$("#btnStart").bind("click", function () {
$("body").everyTime("3s", "timer", function () {
$.ajax(
{
type: 'post',
url: '/Test/GetData',
success: function (r) {
if (r.success) {
console.log("获取到数据,json字符串为" + JSON.stringify(r.data));
}
else {
console.log("(" +r.time + ")秒没有获取到数据");
}
}
});
})
});
})
</script>
jquery.timers-1.2.js 是定时器jquery插件
定时器插件下载
CreateCacheData.cshtml:
@{
ViewBag.Title = "CreateCacheData";
}
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<input id="txtData"/>
<button id="btnSave" >
插入服务器缓存
</button>
<script>
$(function () {
$("#btnSave").click(function ()
{
var getDataValue = $("#txtData").val();
$.post("/Test/InsertCache", {value:getDataValue}, function () {
alert("缓存插入成功");
});
})
});
</script>
3.效果

,
HttpContext.Cache属性的更多相关文章
- 第十七篇:使用窗口的cache属性加速SOUI的渲染
内容渲染速度是决定一个UI成败的关键.无论UI做得多华丽,没有速度都没有意义. 在MFC,WTL等开发框架下,每个控件都是一个窗口,窗口只需要画前景,背景.因为窗口之间的内容不需要做混合,一个子窗口的 ...
- HttpContext.Cache 详解
提到HttpContext.Cache必然会想到Application,他们有什么共性和不同点呢,我们一一道来 相同点: 1.两者都是使用键值对来存储对象 2.两者都是应用程序同生命周期(在cache ...
- HttpContext.Cache和Application的区别
原文:HttpContext.Cache和Application的区别 (转载) 应用程序级的Cache和Application用户会话级的Session application的缺点是在读取时最 ...
- ASP.NET中HttpContext.Cache的使用
-------------------------------键 --值-----依赖-----过期时间-------------------------------绝对过期------------- ...
- Asp.Net framework 类库 自带的缓存 HttpRuntime.Cache HttpContext.Cache
两个Cache 在.NET运用中经常用到缓存(Cache)对象.有HttpContext.Current.Cache以及HttpRuntime.Cache,HttpRuntime.Cache是应用程序 ...
- 关于Ajax 的 cache 属性 (Day_34)
最近做项目,在某些页面显示,ajax刷新总是拿不到新内容,时常需要清除缓存,才能到达想要的效果. 经过再次查看文档,最后加了一行属性:cache:false 即可解决问题 我们先看下文档的说明: 可以 ...
- 缓存 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 ...
- HttpContext.Current.Cache 和 HttpRuntime.Cache 区别
原文地址:http://blog.csdn.net/avon520/article/details/4872704 .NET中Cache有两种调用方式:HttpContext.Current.Cach ...
随机推荐
- linux 安装mysql数据库——tar.gz包解压安装法
mysql数据库有多种安装方式,本文只介绍在Linux服务器上的tar.gz包解压安装法, 先通过mysql官网或者网络资源下载 mysql-5.7.3-m13-linux-glibc2.5-x86_ ...
- 【Windows编程】系列第六篇:创建Toolbar与Statusbar
上一篇我们学习了解了如何使用Windows GDI画图,该应用程序都是光光的静态窗口,我们使用Windows应用程序,但凡稍微复杂一点的程序都会有工具栏和状态栏,工具栏主要用于一些快捷功能按钮.比如典 ...
- Struts2 讲解笔记
1.第一个Struts2入门案例 1.找jar包 Struts2-core xwork-core ognl javasist freemarker commons-lang commons-io co ...
- css样式之background详解(格子效果)
background用法详解: 1.background-color 属性设置元素的背景颜色 可能的值 color_name 规定颜色值为颜色名称的背景颜色(比如 red) he ...
- [LeetCode] Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
- Lind.DDD.RedisClient~对StackExchange.Redis调用者的封装及多路复用技术
回到目录 两雄争霸 使用StackExchange.Redis的原因是因为它开源,免费,而对于商业化的ServiceStack.Redis,它将一步步被前者取代,开源将是一种趋势,商业化也值得被我们尊 ...
- WPF简单模拟QQ登录背景动画
介绍 之所以说是简单模拟,是因为我不知道QQ登录背景动画是怎么实现的.这里是通过一些办法把它简化了,做成了类似的效果 效果图 大体思路 首先把背景看成是一个4行8列的点的阵距,X轴Y轴都是距离70.把 ...
- 写出将字符串中的数字转换为整型的方法,如:“as31d2v”->312,并写出相应的单元测试,正则去掉非数值、小数点及正负号外的字符串
写出将字符串中的数字转换为整型的方法,如:"as31d2v"->312,并写出相应的单元测试,输入超过int范围时提示不合法输入. public struct Convert ...
- Keepalived的全局配置
Keepalived的全局配置 默认配置文件如下: ! Configuration File for keepalived global_defs { notification_email { aca ...
- 【数据库】_由2000W多条开房数据引发的思考、实践----给在校生的一个真实【练耙场】,同学们,来开始一次伟大的尝试吧。
× 缘起---闲逛博客园 前几天的时候,在某一QQ群看到一条消息“XXX酒店开房XXXBTXX迅雷BT下载”,当时是一目十行的心态浏览,目光掠过时, 第一反应我想多了~以为是XX种子(你懂的~ ...