zoukankan      html  css  js  c++  java
  • 缓存的使用

    ASP.net缓存主要分为:页面缓存(中庸)、数据源缓存(最不灵活的)、数据缓存(灵活)这三种主要类型。
    ①页面缓存:给页面添加<%@ OutputCache Duration=“15”  VaryByParam=“none”%>标签就可以启用页面缓存,这样整个页面的内容都会被缓存,页面中的ASP.Net代码、数据源在缓存期间都不会被运行,而是直接输出缓存的页面内容。 也就是不会执行C#和HTML代码,直接到缓存空间中拿已经存在的页面。

    对于看新闻页面来讲,如果如上设置的话,则会缓存在第一个看到的新闻,因为?id=2、?id=3只是页面的不同参数而已,为了能让不同的新闻各自缓存,因此可以设置VaryByParam=“id”,表示对于相同页面的不同的id参数进行单独缓存。如果有多个确定缓存的参数,则将参数名用分号隔开即可,比如VaryByParam=“id;number”。如果想让任何不同的查询字符串都创建不同的缓存,则设置VaryByParam="*",一般情况下设置“*”就足够。

    一般网站的首页设置页面缓存。

    ②数据源缓存
    设定ObjectDataSource的CacheDuration(缓存时间:秒),EnableCaching=true。这样每隔CacheDuration指定的时间段才调用SelectMethod指定的方法来执行数据库查询,其他时候都是直接返回缓存的数据。取数据的过程缓存,在缓存期间,绑定控件向ObjectDataSource要数据, ObjectDataSource直接将缓存的数据返回给控件,不再去向TypeName指向的类要数据。
    CacheExpirationPolicy="Sliding"。缓存实效策略。

    ③数据缓存

    var data = HttpRuntime.Cache["hotwords"];
    if (data == null)
    {
        IEnumerable<SearchSum> hotwords = new KeywordLogServices().GetHotWords();
        //将数据放入缓存,key为"hotwords",缓存30秒
        //第一个参数为缓存项的key,第二个参数为值
        //第四个参数为超时时间
        HttpRuntime.Cache.Insert("hotwords", hotwords, null, DateTime.Now.AddSeconds(30), TimeSpan.Zero);
        return hotwords;
    }
    else
    {
        return (IEnumerable<SearchSum>)data;
    }

     

    if (Cache["list"] != null)
    {
        TextBox1.Text=Cache["list"].ToString();
    }
    else
    {
        List<MODEL.Aticle> list = new BLL.Aticle().GetAll()[0];
        Cache.Insert("list", list.ATitle, null, DateTime.Now.AddHours(1), TimeSpan.Zero);
    }

    分类: ASP.NET
  • 相关阅读:
    PAT (Advanced Level) 1060. Are They Equal (25)
    PAT (Advanced Level) 1059. Prime Factors (25)
    PAT (Advanced Level) 1058. A+B in Hogwarts (20)
    PAT (Advanced Level) 1057. Stack (30)
    PAT (Advanced Level) 1056. Mice and Rice (25)
    PAT (Advanced Level) 1055. The World's Richest (25)
    PAT (Advanced Level) 1054. The Dominant Color (20)
    PAT (Advanced Level) 1053. Path of Equal Weight (30)
    PAT (Advanced Level) 1052. Linked List Sorting (25)
    PAT (Advanced Level) 1051. Pop Sequence (25)
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/2458601.html
Copyright © 2011-2022 走看看