In petshop4.0, the so-called "allow the intermediate layer object cache to automatically expire when the backend database information is changed"

Source: Internet
Author: User

The Link described in Microsoft's petshop4.0 contains the following: ASP. NET 2.0 is an SQL cache dependency: allows the intermediate layer object cache to automatically expire when the backend database information is changed. For SQL 2000, it is effective at the table level (as in pet shop 4), and for SQL Server 2005, it can also be effective at the individual row level. With this function, the cached database information can always be up-to-date, while the cache can still be used to reduce the load on the intermediate layer and database server.

The key point is: allow the intermediate layer object cache to automatically expire when the backend database information is changed.

I debugged petshop4.0. During the debugging, I directly modified the information of the table product name in the database, check whether it is actively refresh or jump back after the process is the latest data, my conclusion is that it sometimes does not work, but it is better than vs2003.

Because the source code of the petshop4.0 solution is a complete set, it feels too much to know about this function separately, so I will write a small example separately,

Now let's test the instance:

1. Create a program and name webapplication testimmediatecache. Add it to the <system. Web> node of Web. config.

 

    <caching>      <sqlCacheDependency enabled="true" pollTime="10000">        <databases>          <add name="MSPetShop4" connectionStringName="SQLConnectionString" pollTime="10000"/>        </databases>      </sqlCacheDependency>    </caching>

 

The above configuration is mainly used to hook the cache with the specified dB and add it to the <configuration> node.

  <appSettings>    <add key="SQLConnectionString" value="data source=apj007;initial catalog=MSPetShop4;user id=sa;password=1;persist security info=true;packet size=4096;min pool size=1;max pool size=50;connection timeout=180"/>  </appSettings>  <connectionStrings>    <add name="SQLConnectionString" connectionString="data source=apj007;initial catalog=MSPetShop4;user id=sa;password=1;persist security info=true;packet size=4096;min pool size=1;max pool size=50;connection timeout=180"/>  </connectionStrings>
 
2. Add Page code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestImmediateCache._Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
C:
using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Data.SqlClient;using System.Web.Caching;namespace TestImmediateCache{    public partial class _Default : System.Web.UI.Page    {        protected AggregateCacheDependency dependency = new AggregateCacheDependency();        protected void Page_Load(object sender, EventArgs e)        {            string conStr = ConfigurationManager.AppSettings["SQLConnectionString"];            string sqlStr = "";            sqlStr = "select Name from Product where ProductID=@ProductID"; //FI-08            SqlParameter[] parms;            parms = new SqlParameter[]            {new SqlParameter("@ProductID", SqlDbType.VarChar, 10)            };            parms[0].Value = "FI-08";            //Add to Cache            string data = (string)HttpRuntime.Cache["PetName"];            if (data == null)            {                object obj = DataAccess.SqlHelper.ExecuteScalar(conStr, CommandType.Text, sqlStr, parms);                data = obj.ToString();                //HttpRuntime.Cache.Add("PetName", data, cd, DateTime.Now.AddHours(cacheDuration), Cache.NoSlidingExpiration, CacheItemPriority.High, null);                int cacheDuration = 12;                dependency.Add(new SqlCacheDependency("MSPetShop4", "Product"));                AggregateCacheDependency cd = this.dependency;                HttpRuntime.Cache.Add("PetName", data, cd, DateTime.Now.AddHours(cacheDuration), Cache.NoSlidingExpiration, CacheItemPriority.High, null);            }            this.Label1.Text = data;            //this.CachePolicy.Dependency = this.dependency;            //System.Web.UI.UserControl uc = new UserControl();            //uc.CachePolicy        }    }}
After the final debugging, I found that the situation is still the same as that of petshop4.0. Sometimes you can click it once or more. Can I leave a reply to solve the problem if the prawn sees it?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.