petshop中的控件缓存与数据缓存
2008-11-12 17:15
176 查看
以petshop中的NavigationControl.ascx控件为例,以下的“页”即指“NavigationControl.ascx”控件
页中通过:
Code
/// <summary>
/// Method to retrieve and cache category name by its ID
/// </summary>
/// <param name="categoryId">Category id</param>
/// <returns>Category name</returns>
public static string GetCategoryName(string categoryId) {
Category category = new Category();
if (!enableCaching)
return category.GetCategory(categoryId).Name;
string cacheKey = string.Format(CATEGORY_NAME_KEY, categoryId);
// Check if the data exists in the data cache
string data = (string)HttpRuntime.Cache[cacheKey];//1
if (data == null) {
// Caching duration from Web.config
int cacheDuration = int.Parse(ConfigurationManager.AppSettings["CategoryCacheDuration"]);
// If the data is not in the cache then fetch the data from the business logic tier
data = category.GetCategory(categoryId).Name;
// Create a AggregateCacheDependency object from the factory
AggregateCacheDependency cd = DependencyFacade.GetCategoryDependency();
// Store the output in the data cache, and Add the necessary AggregateCacheDependency object
HttpRuntime.Cache.Add(cacheKey, data, cd, DateTime.Now.AddHours(cacheDuration), Cache.NoSlidingExpiration, CacheItemPriority.High, null);//2
}
return data;
}
页中通过:
Code
/// <summary>
/// Method to retrieve and cache category name by its ID
/// </summary>
/// <param name="categoryId">Category id</param>
/// <returns>Category name</returns>
public static string GetCategoryName(string categoryId) {
Category category = new Category();
if (!enableCaching)
return category.GetCategory(categoryId).Name;
string cacheKey = string.Format(CATEGORY_NAME_KEY, categoryId);
// Check if the data exists in the data cache
string data = (string)HttpRuntime.Cache[cacheKey];//1
if (data == null) {
// Caching duration from Web.config
int cacheDuration = int.Parse(ConfigurationManager.AppSettings["CategoryCacheDuration"]);
// If the data is not in the cache then fetch the data from the business logic tier
data = category.GetCategory(categoryId).Name;
// Create a AggregateCacheDependency object from the factory
AggregateCacheDependency cd = DependencyFacade.GetCategoryDependency();
// Store the output in the data cache, and Add the necessary AggregateCacheDependency object
HttpRuntime.Cache.Add(cacheKey, data, cd, DateTime.Now.AddHours(cacheDuration), Cache.NoSlidingExpiration, CacheItemPriority.High, null);//2
}
return data;
}
相关文章推荐
- petshop的缓存输出
- ASP.NET 4中的可扩展输出缓存(可以缓存页面/控件等)
- 《解剖PetShop》系列之四 PetShop之ASP.NET缓存
- petshop学习笔记(4) - 缓存
- 用户控件的缓存技术之二【共三篇】
- winform 设定双缓存避免控件闪烁
- 学习PetShop3.0(3)查询及simplePager控件
- ASP.NET用户控件缓存造成的BUG一例
- 浅析showModalDialog数据缓存问题(用禁止浏览器缓存解决)
- asp.net textbox 控件如何清除缓存
- PetShop 4.0 详解之四(PetShop之ASP.NET缓存)
- 用户控件缓存
- 在PetShop 4.0中ASP.NET缓存的实现:『一』
- PetShop之ASP.NET缓存
- PetShop 4.0的缓存处理
- Redis 高速缓存控件
- PetShop之ASP.NET缓存
- 《解剖PetShop》系列之四——ASP.NET缓存
- PetShop之ASP.NET缓存
- [转]ASP.NET 缓存(十三)--通过使用声明性的属性缓存用户控件的多个版本