petshop缓存依赖及困惑

这应该是我之前安排的最后一个任务吧:CacheDependenceFactory(缓存依赖的工厂类)、ICacheDependency(缓存依赖接口)、              TableCacheDependency(缓存依赖实现类)

很明显,这是一个工厂模式,对这种模式,我还是先来看下接口吧,这个接口很简单,只定义了一个函数:

publicinterface IPetShopCacheDependency {

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

然后就是,中间的缓存依赖实现类,petshop好像只对Category、product和Item这三个类进行缓存。为什么要使用缓存呢?可以提高性能吧。那具体是怎么提高的呢?待续。

publicclass Category : TableDependency {

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

Category这个类直接调用了父类的构造函数,那么看下他传过去的字符串是什么吧,当然我们得先看下父类的构造函数:

protected TableDependency(string configKey) {

string dbName = ConfigurationManager.AppSettings["CacheDatabaseName"];//MSPetShop4
string tableConfig = ConfigurationManager.AppSettings[configKey];//Product,Category,Item
string[] tables = tableConfig.Split(configurationSeparator);//','

foreach (string tableName in tables)
dependency.Add(
new SqlCacheDependency(dbName, tableName));//吧数据库和缓存的类放在SqlCacheDependency中
}

看到ConfigurationManager.AppSettings[configKey]吧,看来这个字符串代笔的含义在web.config中:

<add key="CategoryTableDependency" value="Category"/>

可以看出这个字符串最终传递的是一个Category类吧,那么我们来研究下这个工厂类的最重要一句代码是做了什么吧,它到底是怎么利用TableCacheDependence.Category这个类的呢?

dependency.Add(new SqlCacheDependency(dbName,tableName));

这段先留着待会处理。现在先来补充下缓存的有关知识。

.Net 2.0引入了缓存依赖SqlCacheDependency,较之Cache的最大特点是,SqlCacheDependency可以根据数据库的更改来更改缓存,比如有的数据在缓存中存在,但在数据库中已经不存在了,那么这个时候就会在Cache中删除过期的数据。

/*****************************************************************************************************************/

需要建立依赖项的数据库与数据表都配置在web.config文件中,其设置如下:
 <add key="CacheDatabaseName" value="MSPetShop4"/>
 <add key="CategoryTableDependency" value="Category"/>
 <add key="ProductTableDependency" value="Product,Category"/>
 <add key="ItemTableDependency" value="Product,Category,Item"/>

就拿ProductTableDependency这个的值是Product和Category,就是说,如果Category类实现了父类的方法,那么Product和Category类就会建立SqlCacheDependency依赖。那么为什么是Product和Category?为什么不是Product?这个缓存依赖到底是怎么样实现的?

还有缓存机制是什么?它是怎么调用的呢?

在petshop的App_Code中的WebUtility的GetCategoryName和GetProductName方法用到了DependencyFacade类,例如GetCategoryName方法:

publicstaticstring GetCategoryName(string categoryId) {

Category category
=new Category();
if (!enableCaching)
return category.GetCategory(categoryId).Name;

string cacheKey =string.Format(CATEGORY_NAME_KEY, categoryId);//category_name_birds
//// 检查缓存中是否存在该数据项;
string data = (string)HttpRuntime.Cache[cacheKey];
if (data ==null) {
// Caching duration from Web.config// 通过web.config的配置获取duration值;
int cacheDuration =int.Parse(ConfigurationManager.AppSettings["CategoryCacheDuration"]);//12

//// 如果缓存中不存在该数据项,则通过业务逻辑层访问数据库获取;
data = category.GetCategory(categoryId).Name;

//// 通过Facade类创建AggregateCacheDependency对象;
AggregateCacheDependency cd = DependencyFacade.GetCategoryDependency();

//// 将数据项以及AggregateCacheDependency 对象存储到缓存中;
HttpRuntime.Cache.Add(cacheKey, data, cd, DateTime.Now.AddHours(cacheDuration), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
}

return data;
}

我知道了!第一次页面调用的时候date肯定是为null,后面遇到相同的就可以直接从缓存中找了,这样就可以提高速度,更HastTable一样。

那么我需要来研究下HttpRuntime这个Cache属性了。




原文地址:https://www.cnblogs.com/huaizuo/p/2113214.html