zoukankan      html  css  js  c++  java
  • asp.net 缓存

    缓存分为:输出缓存和数据缓存

    输出缓存(OutputCache):

    页面缓存:

    比较明显的就是缓存HTML输出的页面,当相同的页面再被请求的时候,asp.net就会使用缓存下来的页面。

    <%@ OutputCache Duration = “60” VaryByParam = “None” %>

    上面的列子表面页面将被缓存60秒,用户的请求都会被缓存,60秒后缓存过期将会创建下一个缓存,VaryByParam属性是必须的,当没有参数是设为None。

    <%@ OuputCache Durationj = “60” VaryByParam = “ id ”%>

    上述的列子表示根据不同的ID来缓存数据。

    自定义缓存:

    可以自定义程序来缓存页面。ASP.NET提供了一种很便捷的方式来创建自定义缓存,使用VarByCustom属性指定自定义缓存类型的名字。

    <%@OutputCacheDuration="60"VaryByParam="None"VaryByCustom="browser"%>

    同时还要创建为缓存生成自定义字符串的方法,如下:

    public override string GetVaryByCustomString(HttpContext context,string custom) 

        if(custom == "browser") 

        { 

       Return context.Request.Browser.Browser+ context.Request.Browser.MajorVersion; 

        } 

        else 

       { 

           return base.GetVaryByCustomString(context, custom); 

        } 

    }

    这个方法必须写在global.asax文件中。ASP.NET使用该方法返回的字符串来实现缓存,如果这个方法在不同的请求中返回相同的字符串,ASP.NET就会使用缓存的页面,否则就会生成新的缓存版本。

    上面的例子中GetVaryByCustomString()方法根据浏览器的名字创建缓存字符串,ASP.NET会根据不同的浏览器请求创建不同版本的缓存。

    控件缓存:

    指定缓存控件内容,可以通过VaryByControl属性来实现

    <%@OutputCacheDuration="60"VaryByControl="MyControl"%>

    上述的代码表示asp.net将会把MyControl控件缓存60分钟。

    配置文件设置缓存:

    我们可以通过OutputCache中的CacheProfile属性来指定配置文件中的设置。

    <%@ OutputCache CacheProfile = “ProductCacheProfile” VaryByParam=”None”%>

    <system.web> 

      <caching> 

        <outputCacheSettings> 

          <outputCacheProfiles> 

         <addnameaddname=" ProductCacheProfile "duration="60"/> 

       </outputCacheProfiles> 

    </outputCacheSettings> 

       </caching> 

    </system.web> 

    数据缓存(DataCache):

             对于一些比较耗时的数据我们可以用键值的方式存储到一个对象缓存集合中,如下:

           Cache[“Name”] = data;

             我们可以通过使用Cache.Insert()方法来设置缓存的过期,优先级,依赖项等。

    date1 = DateTime.Now;

    Cache.Insert("Date1", date1, null, DateTime.Now.AddSeconds(20), TimeSpan.Zero);ASP.NET允许你设置一个绝对过期时间或滑动过期时间,但不能同时使用。

    缓存依赖项 Cache dependency:

    缓存依赖项是指,当依赖的其他资源发生改变时,缓存自动过期。

    date2 = DateTime.Now;

    string[] cacheKeys = { "Date1"};

    CacheDependency cacheDepn = newCacheDependency(null, cacheKeys);

    Cache.Insert("Date2", date2, cacheDepn);

    上面的例子“Date2”缓存对象依赖“Date1”缓存条目,当 “Date1” 对象过期后“Date2” 将会自动过期。CacheDependency(null, cacheKeys)中的第一个参数为空是由于我们只监视缓存键的更改情况。

    缓存的优先级:

    public partial class _Default : System.Web.UI.Page

        {

            protected void Page_Load(object sender, EventArgs e)

            {

                Cache cache = HttpContext.Current.Cache;

                  cache.Add("MyData", "缓存重要级别", null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(30), CacheItemPriority.High, null);

            }

        }

     该枚举一共有六级

    优先级

    Low=1

    BelowNormal=2

    Normal=3

    Default=3

    AboveNormal=4

    High=5

    NotRemoveable=6

    SQL数据库缓存依赖:

    当数据库中的某个表发生改变时,数据从表中读取,没有发生改变时候从缓存中读取。

    1. 配置文件

           <caching>

               <sqlCacheDependency>

                  <databases>

    <add name="mySqlCache" connectionStringName="SQLCaheTestConnectionString"/>

                  </databases>

               </sqlCacheDependency>

           </caching>

       

    其中connectionStringName属性的参数是数据库连接字符串;

    1. 在命令行中用aspnet_regsql.exe开始sql缓存

    a).为已经建好的数据库名为NBAData创建成员资格提供程序服务(如果输入的数据库名称不存在或没写,则会创建一个新数据库或创建一个默认的aspnetdb数据库),这里,登陆为sqlserver身份验证,用的是Visual Studio 2008命令提示工具:

    aspnet_regsql.exe -S localhost -U sa -P P@ssw0rd -d NBAData -A m

    b).为数据库NBAData里的一张表Player启用缓存依赖,若数据库未启用缓存依赖,则先要输入命令-ed为数据库启用缓存依赖:

    aspnet_regsql.exe -S localhost -U sa -P P@ssw0rd -d NBAData -ed -et -t Player

    aspnet_regsql.exe -S localhost -E -ssadd -sstype c -d yourDbName

    当缓存依赖添加成功后数据库中则会添加一张名为AspNet_SqlCacheTablesForChangeNotification的表,并且有一行记录存在。

                       3. 在页面文件的头部写入<%@ OutputCache Duration="20" VaryByParam="*" SqlDependency=" SQLCaheTestConnectionString: Player " %>此时会监视数据库的Player表是否改变

  • 相关阅读:
    正则中[A-z]与[A-Za-z]的区别
    .Net Core 缓存方式(二)DistributedSqlServerCache实现(2)
    .Net Core 缓存方式(二)分布式缓存及MemoryDistributedCache 实现(1)
    anaconda安装后spyder打不开的解决方法
    Pandas
    CrawlSpider、分布式、增量式
    Scrapy之数据解析与数据持久化存储
    封装axios库
    vue全国省市选择vue组件
    html+jq实现全国省的单选,弹框输入input
  • 原文地址:https://www.cnblogs.com/makqiq/p/5882487.html
Copyright © 2011-2022 走看看