缓存+SqlCacheDependency(一)

来源:互联网 发布:淘宝代购dw表是真的吗 编辑:程序博客网 时间:2024/06/01 14:57

***** 为了避免“数据过期”,.net2.0引入了自定义缓存依赖项,特别是:System.Web.Caching.SqlCacheDependency : CacheDependency

他能够根据数据库中相应数据的变化,通知缓存,移除哪些过期的数据,借鉴PetShop5.0的案例,接下来详细说明:

------------------------------------------数据库不同版本的处理------------------------------------------------

Sql Server2000 不支持自动监视数据库表的功能,对于2000而言,还需要特殊的办法是他也有办法得到数据库数据有没有变化

1. 通过工具aspnet_regsql ;   通过输入配置命令,就可以在要监视的数据库表的数据上建立触发器,存储过程等来监视这些数据的变化

2.利用编程,通过类SqlCacheDependencyAdmin,可以自定义的控制要不要监听数据库里的数据表

 

Sql Server2005 就不需要这样的麻烦,他自身就具备了“变更通知的功能”

 

------------------------------------------PetShop5.0中,如何实现缓存的策略---------------------------------

 

一、为了扩展CacheDependency类,建立接口ICacheDependency

    public interface IPetShopCacheDependency {

        /// <summary>
        /// Method to create the appropriate implementation of Cache Dependency
        /// </summary>
        /// <returns>CacheDependency object(s) embedded in AggregateCacheDependency</returns>
        AggregateCacheDependency GetDependency();    
    }

    AggregateCacheDependency  是依赖项的集合

二、实现CacheDependency

 

  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;        }    }

通过Web.config配置依赖项的数据库和数据库表,还有对SqlCacheDependency的支持:

<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"/>
<caching><sqlCacheDependency enabled="true" pollTime="10000"><databases><add name="MSPetShop4" connectionStringName="SQLConnString1" pollTime="10000"/></databases></sqlCacheDependency></caching>

 

不同数据库表,所需要的依赖项是不同的,但是他们有共同特征,都是要把不同数量的数据库表放到数据依赖集合里面.

对于某一个表,他的依赖项的添加都可以调用父类的方法

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


到目前为止:我们已经能过可以把需要监视的数据库表放到依赖项集合里面

 

 

 

 

原创粉丝点击