• 缓存技术


    一、缓存概述

      缓存是在内存中保存创建代价高的信息副本的一种技术,它可以同时提高性能和扩展性。

    二、缓存的分类

      1、客服端缓存(客服端浏览器的硬盘上保留的静态文件)

      2、服务端缓存

        (1)静态文件缓存(静态页面)

        (2)动态缓存

        1)传统缓存方式

        2)页面输出缓存

        写法一、将整个ASP.NET页面内容保存在服务器内存中。用法:在aspx页面的顶部加<%@ OutputCache Duration="60" VaryByParam="none" %>,其中Duration表示缓存的时间,VaryByParam表示页面根据什么方式发送的参数来更新缓存的内容。

        写法二、在web.config中加入该配置:     

     1 <system.web>
     2     <caching>
     3         <outputCacheProfiles>
     4            <add name="CacheTest" duration="50" />
     5         </outputCacheProfiles>
     6     </caching>
     7 </system.web>        
     8 
     9 //页面中声明:
    10 <%@ OutputCache CacheProfile="CacheTest" VaryByParam="none" %>

        其他属性用法:VaryByControl通过控制文件中包含的服务器控件来改变缓存。

        3)页面局部缓存

          1、控件缓存(将静态内容放在一个用户控件中,并将该用户控件标记为缓存,用户控件中写法跟上面一样)

          2、缓存后替换(与控件缓存相反,它对整个页面进行缓存,动态变化的内容使用Subtitution控件)

        4)利用.NET提供的System.Web.Caching缓存(GetCache()与SetCache()方法)

        5)缓存依赖

          a)、文件缓存依赖(可以解决缓存带来的数据滞后性问题,一般适合读取配置文件的缓存处理)

           这种策略让缓存依赖于一个指定的文件,通过改变文件的更新日期来清除缓存。

           关键代码如下所示:

    1 System.Web.Caching.CacheDependency dep=new Syste.Web.Caching.CacheDependency("C:\text.txt");
    2 SetCache(Cachekey,objModel,dep); //objModel:缓存对象

         b)、数据库缓存依赖

           步骤:

           一、在web.config的<system.web>节中写入:

    1 <caching>
    2     <sqlCacheDependency enabled="true" pollTime="6000">
    3         <databases>
    4 <add naem="codematic" connectionStringName="strcodematic"/>
    5       </databases>
    6     </sqlCacheDependency >
    7 </caching>    

          二、执行下面的命令,为数据库启用缓存依赖

            aspnet_regsql.exe工具位于windows\microsoft.net\framework\[版本]文件夹中。

    aspnet_regsql -c "连接数据库的字符串" -ed -et -t "表名"

           三、在代码中使用缓存,关键代码如下所示:

    1 1 System.Web.Caching.SqlCacheDependency dep=new Syste.Web.Caching.SqlCacheDependency("codematic","p_Product");
    2 2 SetCache(Cachekey,objModel,dep); //objModel:缓存对象,p_Product:表名

      3、第三方缓存系统(Cache Application Block,Memcached,Cacheman)

  • 相关阅读:
    ValidateRequest问题
    通过反射得到object[]数组的类型并且的到此类型所有的字段及字段的值
    正则表达式匹配括号中的字符,不包括括号
    C# String
    C# TYPES
    C# History and Future
    Mixing unmanaged C++ and CSharp
    [Tip: property]C#3.0 autoimplemented property
    C#: constant and readonly
    c#:Nullable Types
  • 原文地址:https://www.cnblogs.com/yuchengping/p/3485427.html
Copyright © 2020-2023  润新知