由petShop项目中看到的一些亮点,实际开发中我们应该注意和借鉴的
时间:2010-11-01 来源:〤‵坠落者...
2. 缓存设置,缓存时间和是否开启缓存配置到配置文件中方便我们关闭和启用缓存
private static readonly int productTimeout = int.Parse(ConfigurationManager.AppSettings["ProductCacheDuration"]);
private static readonly bool enableCaching = bool.Parse(ConfigurationManager.AppSettings["EnableCaching"]);
/// <summary>
/// This method acts as a proxy between the web and business components to check whether the
/// underlying data has already been cached.
/// </summary>
/// <param name="category">Category</param>
/// <returns>List of ProductInfo from Cache or Business component</returns>
public static IList<ProductInfo> GetProductsByCategory(string category) {
Product product = new Product();
if (!enableCaching)
return product.GetProductsByCategory(category);
string key = "product_by_category_" + category;
IList<ProductInfo> data = (IList<ProductInfo>)HttpRuntime.Cache[key];
// Check if the data exists in the data cache
if (data == null) {
// If the data is not in the cache then fetch the data from the business logic tier
data = product.GetProductsByCategory(category);
// Create a AggregateCacheDependency object from the factory
AggregateCacheDependency cd = DependencyFacade.GetProductDependency();
// Store the output in the data cache, and Add the necessary AggregateCacheDependency object
HttpRuntime.Cache.Add(key, data, cd, DateTime.Now.AddHours(productTimeout), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
}
return data;
}
private static readonly int productTimeout = int.Parse(ConfigurationManager.AppSettings["ProductCacheDuration"]);
private static readonly bool enableCaching = bool.Parse(ConfigurationManager.AppSettings["EnableCaching"]);
/// <summary>
/// This method acts as a proxy between the web and business components to check whether the
/// underlying data has already been cached.
/// </summary>
/// <param name="category">Category</param>
/// <returns>List of ProductInfo from Cache or Business component</returns>
public static IList<ProductInfo> GetProductsByCategory(string category) {
Product product = new Product();
if (!enableCaching)
return product.GetProductsByCategory(category);
string key = "product_by_category_" + category;
IList<ProductInfo> data = (IList<ProductInfo>)HttpRuntime.Cache[key];
// Check if the data exists in the data cache
if (data == null) {
// If the data is not in the cache then fetch the data from the business logic tier
data = product.GetProductsByCategory(category);
// Create a AggregateCacheDependency object from the factory
AggregateCacheDependency cd = DependencyFacade.GetProductDependency();
// Store the output in the data cache, and Add the necessary AggregateCacheDependency object
HttpRuntime.Cache.Add(key, data, cd, DateTime.Now.AddHours(productTimeout), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
}
return data;
}
相关阅读 更多 +