HttpContext.Cache属性
生活随笔
收集整理的這篇文章主要介紹了
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.效果
,
轉載于:https://www.cnblogs.com/CallmeYhz/p/5979345.html
總結
以上是生活随笔為你收集整理的HttpContext.Cache属性的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codevs 1519 过路费(Mst
- 下一篇: linux shell if -a 到-