zoukankan      html  css  js  c++  java
  • SQL 缓存依赖入门介绍

    创建依赖于数据库中表或行的缓存项(即缓存数据)。当表中或特定行中发生更改时,带有依赖项的项(缓存数据)便会失效,并会从缓存中移除。并重新读取数据到缓存中,这时候应用程序获取的是最新的数据.

    特点:
    G5_jt$K2R#lwFa'~0 SQL 缓存依赖项可用于应用程序缓存和页输出缓存。ITPUB个人空间{aGRL p hm:l
    可以在网络园(一台服务器上存在多个处理器)或网络场(多台服务器运行同一应用程序)中使用 SQL 缓存依赖项。ITPUB个人空间7R)]4b7nyY#j;S@
    与 SQL 缓存依赖项关联的数据库操作比较简单,因此不会给服务器带来很高的处理成本。

    打开Visual Studio命令提示:ITPUB个人空间OM)^L(h+F)}j @
    1):为SQL Server中数据库Northwind的Customers表启用缓存依赖项(依赖项名为customers,由-et -t指定):
    1tBR_j$y[@2i0必须注意下面的大小写之分:

    Setting environment for using Microsoft Visual Studio 2008 x86 tools.

    d:\Program Files\Microsoft Visual Studio 9.0\VC>aspnet_regsql.exe -S localhost -U sa -P admin -ed -d Northwind -et -t Orders

    为 SQL 缓存依赖项启用该数据库。

    .

    已完成。

    为 SQL 缓存依赖项启用该表。

    已完成。

    当看到"已完成"时,也就已经为SQL 缓存依赖项启用该表Orders。

    2):在Web.config文件为 SQL 缓存依赖项配置页ITPUB个人空间/S`)x X3~Y
    <appSettings/>
    <!--connectionStrings是数据库连接字符串配置,须手动添加-->
    <connectionStrings>
    <add name="NorthwindConnectionString" connectionString="Data Source=.;Initial Catalog=Northwind;

    Integrated Security=True" providerName="System.Data.SqlClient" />
    </connectionStrings>
    <system.web>
    <caching>
    <sqlCacheDependency enabled="true" pollTime="1000">
    <databases>
    <add connectionStringName="NorthwindConnectionString" name="Northwind"/>
    </databases>
    </sqlCacheDependency>
    </caching>
    ...
    ITPUB个人空间_^I4o5w[1V9i
    ITPUB个人空间8WG f(kdz.[
    ASP.NET 应用程序的缓存中所存储的项与特定 SQL Server 数据库表的关系进行配置后,ITPUB个人空间`^i9Z.g%mu"]
    SqlCacheDependency 类的一个实例将监视该表,以便在表中的项发生更改时自动更新该项或从缓存中移除该项。
    ;@:E(d4Y4?+B0监视将以 PollTime 所指定的频率进行。

    代码实现版本:
    Ypxz:H N+N0 protected void Page_Load(object sender, EventArgs e)
    {
    Response.AddCacheItemDependency("Northwind:customers");

    // Set additional properties to enable caching.
    Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
    Response.Cache.SetCacheability(HttpCacheability.Public);
    Response.Cache.SetValidUntilExpires(true);
    }

    3):在aspx页面中使用缓存:
    -dY/OV5v!i5D @9O0<!--下面的SqlDependency为"数据库名:表名",其中缓存通知必须与用aspnet_regsql.exe时提供的-et -t一致-->
    .e7k`O_d-Dz0<%@ OutputCache SqlDependency="Northwind:customers" Duration="30" VaryByParam="none" %>

    4):工作做到这里,所以的工作已完成. 一旦应用程序启动,就会每隔一段时间就去数据库查询.

    ITPUB个人空间]A7I4Qd&oN&s/Y
    PS:

    其实对一个表设置依赖性其实就是在该表内添加一个触发器,当执行INSERT, UPDATE, DELETE等操作时执行一个名叫AspNet_SqlCacheUpdateChangeIdStoredProcedure的存储过程,其修改AspNet_SqlCacheTablesForChangeNotification表对于tableName的changeId,使之递增1.

  • 相关阅读:
    解释*args和**kwargs的含义
    字典推导式创建字典
    返回json格式数据乱码
    DataTables warning: table id=data-table
    echart折线图,柱状图,饼图设置颜色
    No mapping found for HTTP request with URI
    [Err] 1111
    echart提示框内容数据添加单位
    rg.apache.ibatis.binding.BindingException: Mapper method 'com.dao.Cameao.getOnlineDayRation attempted to return null from a method with a primitive return type (float)
    bootstrap datarangepicker如何使用
  • 原文地址:https://www.cnblogs.com/lvfeilong/p/hgfhgfhgfhgfh.html
Copyright © 2011-2022 走看看