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 ...
随机推荐
- PhpStorm 相关激活方式
点击进入下面网站: http://idea.lanyus.com/
- OC中加载html5调用html方法和修改HTML5内容
1.利用webView控件加载本地html5或者网络上html5 2.设置控制器为webView的代理,遵守协议 3.实现代理方法webViewDidFinishLoad: 4.在代理方法中进行操作H ...
- Don't repeat yourself
今天工作上要测试不同版本mysql在不同系统的速度,学习了怎么源码安装mysql后,就开始敲命令安装和编译执行测试程序. 这么走了两遍后,想起一句话——当一件事重复做三遍后,要考虑把它工具化,自动化. ...
- kettle中含有参数传递的定时任务
(1)新建一个作业(新建->作业),并在控制面板右键: (2)设置一个命令参数: (3)把作业的参数传递给转换: (4)在转换中右键设置转换属性: (5)接收作业中设置的传递参数: (6)参数的 ...
- js form 表达关于onpress 的一个问题
<form id="search-form" method="get" action="/search"> <fields ...
- codevs 3288 积木大赛
题目描述 Description 春春幼儿园举办了一年一度的"积木大赛".今年比赛的内容是搭建一座宽度为 n 的大厦,大厦可以看成由 n 块宽度为1的积木组成,第i块积木的最终高度 ...
- xwalk_core_library-15.44.384 .13.aar 百度云分享
xwalk_core_library-15.44.384.13.aar 这玩意下载很慢 特意放到百度,需要的可以通过百度下载 http://pan.baidu.com/s/1nt0Cmbn 下完后放到 ...
- keras 中如何自定义损失函数
http://lazycoderx.com/2016/10/09/keras%E4%BF%9D%E5%AD%98%E6%A8%A1%E5%9E%8B%E6%97%B6%E4%BD%BF%E7%94%A ...
- HTTP服务器(2)
导语 重定向器是特殊用途的HTTP服务器的另一个简单而有用的应用程序.它的作用就是将用户从一个WEB网站重定向到另外一个网站.下面是一个简单的例子,程序沿用上一个SingleHttpServer代码. ...
- go database/sql sql-driver/mysql 操作
这里使用的是github.com/Go-SQL-Driver/MySQL, 所以需要下载一个github.com/Go-SQL-Driver/MySQL 引入 database/sql 和 githu ...