zoukankan      html  css  js  c++  java
  • 【Asp.Net从零开始】:使用缓存来提高网站性能 Caching(二)

       一.Application Caching   

       1.使用Page.Cache对象  

    //类似Session的键值使用方式
     protected void Page_Load(object sender, EventArgs e)
        {
            lb_Time.Text = "Hello world!";
          
        }
        protected void btn_submit_Click(object sender, EventArgs e)
        {
            Cache["greeting"] = "Hello Cache!";
            if (Cache["greeting"] != null)
            {
                lb_Time.Text = Cache["greeting"] as string;
            }
            else
                lb_Time.Text = "Hello world!";
    
            
        }
    

      2.向Cache中添加元素System.Web.Caching.Cache.Insert  

    Cache.Insert(string, Object, CacheDependency, DateTime, TimeSpan)

    public void Insert (
    	string key,
    	Object value,
    	CacheDependency dependencies,
    	DateTime absoluteExpiration,
    	TimeSpan slidingExpiration
    )
    key

    用于引用该对象的缓存键。

    value

    要插入缓存中的对象。

    dependencies

    所插入对象的文件依赖项或缓存键依赖项。当任何依赖项更改时,该对象即无效,并从缓存中移除。如果没有依赖项,则此参数包含 空引用(在 Visual Basic 中为 Nothing)。

    absoluteExpiration

    所插入对象将过期并被从缓存中移除的时间。如果使用绝对过期,则 slidingExpiration 参数必须为 NoSlidingExpiration

    slidingExpiration

    最后一次访问所插入对象时与该对象过期时之间的时间间隔。如果该值等效于 20 分钟,则对象在最后一次被访问 20 分钟之后将过期并被从缓存中移除。如果使用可调过期,则 absoluteExpiration 参数必须为 NoAbsoluteExpiration.

    Cache.Insert("FileCache", File.ReadAllText("File.txt"), new Caching.CacheDependency(Server.MapPath("File.txt"),Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0,10,0));
    

      

      3.定义Cache的依赖项System.Web.Caching.Dependency  

    A.构造函数

     名称说明
    受保护的方法 CacheDependency() 初始化 CacheDependency 类的新实例。
    公共方法 CacheDependency(String) 初始化 CacheDependency 类的新实例,它监视文件或目录的更改情况。
    公共方法 CacheDependency(String[]) 初始化 CacheDependency 类的新实例,它监视一组(到文件或目录的)路径的更改情况。
    公共方法 CacheDependency(String, DateTime) 初始化 CacheDependency 类的新实例,它监视文件或目录的更改情况。
    公共方法 CacheDependency(String[], DateTime) 初始化 CacheDependency 类的新实例,它监视一组(到文件或目录的)路径的更改情况并指定更改监视开始的时间。
    公共方法 CacheDependency(String[], String[]) 初始化 CacheDependency 类的新实例,它监视一组(到文件或目录的)路径、缓存键的更改情况或同时监视二者的更改情况。
    公共方法 CacheDependency(String[], String[], DateTime) 初始化 CacheDependency 类的新实例,它监视一组(到文件或目录的)路径、缓存键的更改情况或同时监视二者的更改情况。
    公共方法 CacheDependency(String[], String[], CacheDependency) 初始化 CacheDependency 类的新实例,它监视一组(到文件或目录的)路径、缓存键的更改情况或同时监视二者的更改情况。 它还使自己依赖于 CacheDependency 类的一个单独的实例。
    公共方法 CacheDependency(String[], String[], CacheDependency, DateTime) 初始化 CacheDependency 类的新实例,它监视一组(到文件或目录的)路径、缓存键的更改情况或同时监视二者的更改情况。 它还使自己依赖于 CacheDependency 类的另一个实例以及更改监视开始的时间。

    B.属性

     名称说明
    公共属性 HasChanged 获取一个值,该值指示 CacheDependency 对象是否已更改。
    公共属性 UtcLastModified 获取依赖项的上次更改时间。

    添加多个依赖项,利用System.Web.Caching.AggregateCacheDependency

    Caching.CacheDependency dep1 = 
                new Caching.CacheDependency(Server.MapPathw("file1.txt"));
            string[] keyDependencies2 = { "CacheItem1" };
    
    Caching.CacheDependency dep2 = 
                new Caching.CacheDependency(null, keyDependencies2);
            
    Caching.AggregateCacheDependency aggDep =
                new Caching.AggregateCacheDependency();
            aggDep.Add(dep1);
            aggDep.Add(dep2);
            
    Cache.Insert("FileCache", File.ReadAllText(Server.MapPath("File.txt"), aggDep);

      4.设定抛弃重置缓存的绝对时间和相对时间(2者互斥)  

    Cache.Insert("File","C);acheContents", null, DateTime.Now.AddMinutes(10), Cache.NoAbsoluteExpiration);
            
    Cache.Insert("File","CacheContents", null, Cache.NoAbsoluteExpiration, new TimeSpan(0,10,0));
    

      

       二. Page Output Caching   

      1.在前台代码@Page下面添加  

    <%@ OutputCache Duration="15" VaryByParam="none" %>
    
    <%@ OutputCache Duration="15" VaryByParam="search;category" %>

      2.需要以控件作为区分值时,需要获得Asp.Net为控件所生成的id:  

    A.Create your web form and add the control you want to plan to vary by;

    B.Run the page and view the page source code in your web browser, within the HTML source you will find the complex control id;

    C.Copy this id to the VaryByParam property;

    <%@ OutputCache Duration="15" VaryByParam="ct100$MainContent$ChoiceDropDownList" %>
    

      3.局部页面的Caching  

    找到需要缓存的控件.ascx文件,然后再其中加入@ OutputCache 即可

      4.编程动态地为单个页面配置Caching,使用如下3个方法  

    Response.Cache.SetExpires

    Response.Cache.SetCacheablity

    Response.Cache.SetValidUntilExpires

      5.使用 Substitution 来 更新缓存  

    (不能使用在已经被outputcache标注过的控件上)

      A.使用 Response.WriteSubsititution 方法;

          B.使用Substitution控件

      通过对MethodName属性的设定,可以使特定的方法免于被缓存;

     protected void Page_Load(object sender, EventArgs e)
        {
            Substitution1.MethodName = "GetCurrentDateTime";
          
        }
        public static string GetCurrentDateTime()
        {
            return DateTime.Now.ToString();
        }
    

      6.检查(自定义)缓存页的有效性  

    http://msdn.microsoft.com/zh-cn/library/a5e5hdyz(v=VS.80).aspx

     7.从Cache collections中提取项:

    A.确定不指向null;

    B.将其转化为正确的类型;

    C.如果为null,从原始资源来获取

          

          

    给梦想一点时间
  • 相关阅读:
    Atitit attilax要工作研究的要素 纪要 方案 趋势 方向 概念 理论
    Atitit 常见每日流程日程日常工作.docx v7 r8f
    Atitit it 互联网 软件牛人的博客列表
    Atitit 信息链(Information Chain)的概念理解 attilax总结
    Atitit 知识点的体系化 框架与方法 如何了解 看待xxx
    Atitit 聚合搜索多个微博 attilax总结
    Atitit 企业知识管理PKM与PIM
    Atitit 项目沟通管理 Atitit 沟通之道 attilax著.docx
    Atitit 项目管理软件 在线服务 attilax总结 1. 项目管理协作的历史 1 1.1. Worktile 406k 1 1.2. Teambition  584k in baidu
    Atitit.每周末总结 于每周一计划日程表 流程表 v8 import 上周遗漏日志补充 检查话费 检查流量情况 Crm问候 Crm表total and 问候
  • 原文地址:https://www.cnblogs.com/VortexPiggy/p/2628818.html
Copyright © 2011-2022 走看看