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 ...
随机推荐
- ASP.NET MVC Controllers and Actions
MVC应用程序里的URL请求是通过控制器Controller处理的,不管是请求视图页面的GET请求,还是传递数据到服务端处理的Post请求都是通过Controller来处理的,先看一个简单的Contr ...
- 树莓派3B远程VNC的设置(包括开机启动)
可以说,现在很少有自带VNCserver的教程 因为之前 官方系统没有自带VNC 但是 现在 最新版的官方系统已经自带VNCserver 只需要在设置里启用一下,然后设置就可以用啦. 别的教程都是 ...
- JSON学习笔记一 —— 一些与移动端交互产生JSON数据的方法
/** * 测试的返回JSon方法,正式的不会用 * @author MrHandler * @param reqCode * @param joinStr * ...
- UI内侧错题
此题考察的是JQuery UI下的menu插件的使用,ui-menu表示菜单的外层容器,如果菜单包含图标,该元素会另外带一个 ui-menu-icons class.故A B选项错误.ui-menu- ...
- Spring启动后扫描解析注解的过程
对应的类: ComponentScanBeanDefinitionParser.parse() ClassPathBeanDefinitionScanner.doScan() 参考 http://bl ...
- pcl计算样点法向并显示
利用最小二乘法估计样点表面法向,并显示 #include <pcl/point_types.h> #include <pcl/io/pcd_io.h> #include < ...
- 1597: [Usaco2008 Mar]土地购买
1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4023 Solved: 1470[Submit] ...
- ELK日志系统:Filebeat使用及Kibana如何设置登录认证
根据elastic上的说法: Filebeat is a lightweight, open source shipper for log file data. As the next-generat ...
- [LeetCode] Self Crossing 自交
You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to th ...
- html5 自定义标签取值
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...