PetShop的Data Cache
1.Interface
为了工厂模式的需要,PetShopCacheDependency会返回一个包含多个CacheDependency object的AggregateCacheDependency
public interface IPetShopCacheDependency
{
AggregateCacheDependency GetDependency();
}
2.工厂
CacheDependencyFactory模块中的
DependencyAccess会根据web.config的设定生成具体的PetShopCacheDependency
public static class DependencyAccess
{
public static IPetShopCacheDependency CreateProductDependency()
{
return LoadInstance(”Product”);
}
private static IPetShopCacheDependency LoadInstance(string className)
{
string path = ConfigurationManager.AppSettings[”CacheDependencyAssembly”];
string fullyQualifiedClass = path + “.” + className;
//所有的PetShopCacheDependency存在于一个assembly中
return (IPetShopCacheDependency)Assembly.Load(path).CreateInstance(fullyQualifiedClass);
}
}
web.config
<add key=”CacheDependencyAssembly” value=”PetShop.TableCacheDependency”/>
3.具体实现
提供了SQLserver Cache Dependency的通用实现,从web.config中读取table name并生成相应的
AggregateCacheDependency对象.
public abstract class TableDependency : PetShop.ICacheDependency.IPetShopCacheDependency
{
protected char[] configurationSeparator = new char[] { ‘,’ };
protected AggregateCacheDependency dependency = new AggregateCacheDependency();
protected TableDependency(string configKey)
{
string dbName = ConfigurationManager.AppSettings[”CacheDatabaseName”];
string tableConfig = ConfigurationManager.AppSettings[configKey];
string[] tables = tableConfig.Split(configurationSeparator);
foreach (string tableName in tables)
{
//注意dbName要和<caching>一节定义的database对应
dependency.Add(new SqlCacheDependency(dbName, tableName));
}
}
public AggregateCacheDependency GetDependency()
{
return dependency;
}
}
public class Product : TableDependency
{
public Product() : base(”ProductTableDependency”)
{
}
}
web.config
<!– 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”/>
<caching>
<sqlCacheDependency enabled=”true” pollTime=”10000″>
<databases>
<add name=”MSPetShop4″ connectionStringName=”SQLConnString1″ pollTime=”10000″/>
</databases>
</sqlCacheDependency>
</caching>
4.应用:
App_Code中定义了DataProxy类来获取数据,数据可能来自databse,亦可来自cache.
以ProductDataProxy为例,该类封装了BLL层的Product对象,通过Product读取数据,并放入cache
public static class ProductDataProxy
{
}
5. 数据库中的AspNet_SqlCacheTablesForChangeNotification是如何生成的?
使用工具
aspnet_regsql -S <SqlServerName> -U sa -d <DatabaseName> -ed -et -t <TableName>
见<Pet Shop 4 Install Folder>\DatabaseScripts\InstallDatabases.cmd:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql -S localhost -E -d MSPetShop4 -ed
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql -S localhost -E -d MSPetShop4 -t Item -et
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql -S localhost -E -d MSPetShop4 -t Product -et
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql -S localhost -E -d MSPetShop4 -t Category -et
