此类是利用缓存来保存信息的。可以把一些稳定的数据,不会随用户而改变的信息利用Cache保存起来,可以优化网站的速度。
Cache和Session,cookie的区别
Session保存在服务器上,每个用户都有自己的Session,不会冲突,关闭网站后该Session丢失。
Cookie是保存在客户端的,如果不清除流浪器的cookie或者不设置过期时间,cookie都不会丢失。
Cache是分配在服务器上的一个公共的内存片。从Cache中读取数据比较快,有些网站就把一些经常被使用的数据放到Cache里,提高访问速度,优化系统性能。
这里主要介绍Cache里面的三个方法。Insert(),GeT(),Remove() 就是 添加,获取,删除
Insert有重载
????????//Page.Cache和HttpContext.Current.Cache 和一般处理程序 context.Cache ?是不一样的,你用什么存的就要什么取 ???????//insert和get的过程如果装箱和拆箱一样 ???????protected override void OnInit(EventArgs e) ???????{ ???????????//添加缓存, ???????????//你就把参数看做为键值对的形式,只是说键的类型确定了(可以重复,下次还可以设置此键),值可以是任何类型 ???????????Page.Cache.Insert("One", "666"); ?//只要保存了,在后面的页面都可以获取 ???????} ???????protected void Page_Load(object sender, EventArgs e) ???????{ ??????????????????????string aa = ""; ???????????if (Page.Cache.Get("One") != null) ?//使用前最后判断一下,以免项目报错 ???????????{ ???????????????aa =(string)Page.Cache.Get("One"); //获取的时需要转型 ??????????????} ???????????Response.Write(aa + "<br />"); ???????????if (Page.Cache.Get("Ywo") == null) //没有此key就是空 ???????????{ ???????????????Response.Write("无此值<br />"); ???????????} ???????????????}
此方法是可以监视一个文件,如果监视的文件发生了改变,此Cache就清除了。
例如:
①新建一个XML格式的文件用来保存信息,然后做如下准备工作
新建一个类来做对比
利用代码生成xml文件,运行一次就注释了,换成下面的就可以了
???protected override void OnInit(EventArgs e) ???????{ ???????????TestClass xiaoming = new TestClass() { ID = 1, Age = 18, Name = "小明" }; ???????????XmlSerializer xml = new XmlSerializer(xiaoming.GetType()); ???????????string path = Server.MapPath("~/xm.xml"); ???????????using (Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)) ???????????{ ???????????????xml.Serialize(stream, xiaoming); ???????????} ?????????????????????CacheDependency cdd = new CacheDependency(path); ???????????HttpContext.Current.Cache.Insert("xm", xiaoming, cdd);
?????????//这段代码运行一遍就可以删除了,代码换成下面的
???}
protected override void OnInit(EventArgs e) ???????{//读取 ????????????XmlSerializer xml = new XmlSerializer(typeof(TestClass)); ???????????TestClass xiaoming = new TestClass(); ???????????string path = Server.MapPath("~/xm.xml"); ???????????using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) ???????????{ ???????????????xiaoming = (TestClass)xml.Deserialize(stream); ???????????} ???????????CacheDependency cdd = new CacheDependency(path); ???????????HttpContext.Current.Cache.Insert("xm", xiaoming, cdd); ???????}
然后生成xml文件如下
我们页面结构
按钮就是修改我们的XML文件,点击按钮了,我们之前保存的Cache也就被清除了
???????public void ProcessRequest(HttpContext context) ???????{
???????????//此一般处理处理程序用来做显示效果
???????????context.Response.ContentType = "text/plain"; ???????????TestClass xm = null; ???????????if (context.Cache.Get("xm") != null) ???????????{ ???????????????xm =HttpContext.Current.Cache.Get("xm") as TestClass; ???????????} ???????????MemoryStream ms = new MemoryStream(); ???????????XmlWriter xw = new XmlTextWriter(ms, null); ???????????xw.WriteStartDocument(); ???????????xw.WriteStartElement("xm"); ???????????xw.WriteElementString("ID", xm.ID.ToString()); ???????????xw.WriteElementString("Name", xm.Name); ???????????xw.WriteElementString("Age", xm.Age.ToString()); ???????????xw.WriteEndElement(); ???????????xw.Flush(); ???????????ms.Flush(); ???????????// 将流转换成String并返回 ???????????byte[] data = new byte[ms.Length]; ???????????ms.Seek(0, SeekOrigin.Begin); ???????????ms.Read(data, 0, data.Length); ???????????ms.Close(); ???????????string aa = UTF8Encoding.UTF8.GetString(data); ???????????context.Response.ContentType = "text/xml"; ???????????context.Response.Write(aa); ???????}
②打开我们的页面
原理是:一打开我们的页面,我们就把xm.xml里面的信息转成类保存在cache里面,我们点击A标签就会查看到效果,然后我们点击按钮(目的就是改变xm.xml文件),然后在点击A标签就会报错
效果如下:
然后点击按钮,再查看a标签就会报错,证明我们的Cache被清除了。
Cahe里面有两个字段,返回的就是DateTime
例如:两种形式
这个方法手动设置Cache过期
注:Add()和Insert()的区别
Add()和Insert()都可以表示添加一个缓冲,Add()设置了key的值就不能改变了,如同常量一般。而Insert()可以改变key的值。所以一般建议使用Insert()方法
System.Web.Caching.Cache
原文地址:http://www.cnblogs.com/Sea1ee/p/7490952.html