.NET缓存学习笔记
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <script src="http://libs.baidu.com/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript"> $(function () { $(‘:button‘).eq(0).click(function () { var key = $(":text").eq(0).val(); var value = $(":text").eq(1).val(); if (key != "" && value != "") { var data = { action: "add", value: value, key: key }; $.post("ReturnValue.ashx", data, function (result) { alert(result); }, "json"); } }); $(":button").eq(1).click(function () { var data = { action: "selectCount" }; $.post("ReturnValue.ashx", data, function (result) { alert(result); }, "json"); }); $(":button").eq(2).click(function () { var data = { action: "file" }; $.post("ReturnValue.ashx", data, function (result) { var json = eval(result); alert(json.word); }, "json"); }); }); </script> </head> <body> <form id="form1" runat="server"> <div> <table style="border: 2px solid blue"> <tr> <th>key </th> <td> <input type="text" id="txt_key" /></td> </tr> <tr> <th>value </th> <td> <input type="text" id="txt_value" /></td> </tr> <tr> <td> <input type="button" value="设置" /></td> <td> <input type="button" value="数量" /></td> </tr> </table> <br /> <input type="button" value="修改" /> <div id="div"> </div> </div> </form> </body> </html>
public void ProcessRequest(HttpContext context) { //context.Response.ContentType = "text/plain"; //context.Response.Write("Hello World"); if (context.Request["action"] != null) { switch (context.Request["action"].ToString()) { case "add": add(); break; case "selectCount": selectCount(); break; case "file": file(); break; default: break; } } } private void file() { FileInfo fi = new FileInfo("fuct.txt"); StreamWriter sw = new StreamWriter(fi.FullName); sw.WriteLine(new Random().Next(100)); sw.Flush(); sw.WriteLine(new Random().Next(100)); sw.Flush(); sw.Close(); sw.Dispose(); StreamReader sr = new StreamReader(fi.FullName); string word = sr.ReadLine(); sr.Close(); sr.Dispose(); string json = "{\"word\":\"" + word + "\"}"; HttpContext.Current.Response.Write(json); } private void add() { TimeSpan ts = new TimeSpan(11, 0, 0); string value = HttpContext.Current.Request["value"].ToString(); string key = HttpContext.Current.Request["key"].ToString(); System.Web.Caching.Cache objCache = HttpRuntime.Cache; FileInfo fi = new FileInfo("fuct.txt"); System.Web.Caching.CacheDependency file = new System.Web.Caching.CacheDependency(fi.FullName); objCache.Insert(key, value, file, DateTime.MaxValue, ts, System.Web.Caching.CacheItemPriority.NotRemovable, null); System.Web.Caching.Cache aa = HttpContext.Current.Cache; HttpContext.Current.Response.Write(aa.Count); } private void selectCount() { System.Web.Caching.Cache aa = HttpContext.Current.Cache; HttpContext.Current.Response.Write(aa.Count); } public bool IsReusable { get { return false; } }
最近几次面试都问及到了缓存这个东西,所以抽空简单的学习了下 缓存用来存储一些
然后测试了下缓存中的CacheDependency 也就是缓存依赖项 一旦依赖项改变 缓存也随即清空 这个可以用来储存一些配置信息 一般配置到XML然后缓存这个XML一旦配置修改缓存就需要重新去读取
测试了下用一个txt来作为依赖项 手动在文件夹里修改貌似不影响 但是使用程序去修改这立即见效了 缓存被清除了
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。