缓存补充:

来源:互联网 发布:剑三成男捏脸详细数据 编辑:程序博客网 时间:2024/05/02 04:34

petshop的缓存中还涉及到一个命名空间TableCacheDependency

该命名空间下有如下类:

Category.cs

Item.cs

Product.cs

TableDependency.cs

即我们要对Category,Item,Product 三个表进行缓存依赖。

首先看类TableDependency

 

using System.Web.Caching;
using System.Configuration;

namespace PetShop.TableCacheDependency {

    /// <summary>
    /// This is the base class for SQL2KCacheDependency implementation that encapsulates common
    /// algorithm to retrieve database and table names from configuration file and create
    /// the necessary AggregateCacheDependency object
    /// </summary>
    public abstract class TableDependency : PetShop.ICacheDependency.IPetShopCacheDependency {

        // This is the separator that's used in web.config
        protected char[] configurationSeparator = new char[] { ',' };

        protected AggregateCacheDependency dependency = new AggregateCacheDependency();

        /// <summary>
        /// The constructor retrieves all related configuration and add CacheDependency object accordingly
        /// </summary>
        /// <param name="configKey">Configuration key for specific derived class implementation</param>
        protected TableDependency(string configKey) {

            string dbName = ConfigurationManager.AppSettings["CacheDatabaseName"];
            string tableConfig = ConfigurationManager.AppSettings[configKey];
            string[] tables = tableConfig.Split(configurationSeparator);

            foreach (string tableName in tables)
                dependency.Add(new SqlCacheDependency(dbName, tableName));
        }

        public AggregateCacheDependency GetDependency() {
            return dependency;
        }
    }
}

 

具体类Product

--------------------

using System.Web.Caching;

namespace PetShop.TableCacheDependency {
    /// <summary>
    /// Implementation of Product Cache Dependency for SQL Server 2000
    /// </summary>
    public class Product : TableDependency {

        /// <summary>
        /// Call its base constructor by passing its specific configuration key
        /// </summary>
        public Product() : base("ProductTableDependency") { }
    }
}

 

对应的web.config文件有:


  <!-- Cache dependency options. Possible values: PetShop.TableCacheDependency for SQL Server and keep empty for ORACLE -->
  <add key="CacheDependencyAssembly" value="PetShop.TableCacheDependency"/>
  <!-- CacheDatabaseName should match the name under caching section, when using TableCacheDependency -->
  <add key="CacheDatabaseName" value="MSPetShop4"/>
  <!-- *TableDependency lists table dependency for each instance separated by comma -->
  <add key="CategoryTableDependency" value="Category"/>
  <add key="ProductTableDependency" value="Product,Category"/>
  <add key="ItemTableDependency" value="Product,Category,Item"/>