您的位置:首页 > 运维架构

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: