ASP.NET的缓存机制
来源:互联网 发布:淘宝短视频服务商 编辑:程序博客网 时间:2024/05/16 07:39
Asp.NET提供了2中缓存机制来提供缓存功能:
应用程序缓存:允许开发者将程序生成的数据或报表业务对象放入缓存中。
页输出缓存:可以直接从中获取存放在缓存中的页面,而不需要经过繁杂的对该页面的再次处理。
1.应用程序缓存:
是通过ASP.NET来管理内存中的缓存空间,放入缓存中的对象以键值对的方式存储,这便于用户在访问缓存中的数据项时,可以根据key值判断该项是否存在缓存中。
放入在缓存中的数据对象,其生命周期是受限制的,即使在整个应用程序的生命周期内,也不能保证该数据对象一直有效。此外,调用者还可以通过CachItemRemovedCallback委托定义回调方法使得数据项被移除时能够通知用户。
在.NET Framwork中,应用程序缓存通过System.Web.Caching.Cache类实现,对于每一个应用程序域,都要创建一个cache类的实例,其生命周期与应用程序的生命周期保持一致,可以用Add或insert方法将数据项添加到应用程序缓存中,Cache["first"]="First Item"; Cache.Insert("Second","Second Item");还可以为应用程序缓存添加依赖项,使得当依赖项发生更改时,该数据项能够从缓存中移除:
string[] dependencies ={"second"};
Cache.Insert("Third","Third Item",new CacheDependency(null,dependencies));
可以手动移除相关数据项:Cache.Remove("second");
2.页输出缓存:
可以通过内存将处理后的ASP.NET页面存储起来,当客户端再一次访问该页面时,可以省去页面的处理过程,从而提高页面访问的性能,以及Web服务器的吞吐量。
页输出缓存分为整页缓存和部分缓存,可以通过@OutputCache指令完成对Web页面的输出缓存:<%@OutputCache Duration"60" VaryByParam="none">设置缓存时间60s,只要没有超过设置的期限值,当用户访问相同的页面或空间时,就可以直接在缓存中获取。
VaryByParam参数可以根据设置的参数值建立不同的缓存,如在一个输出天气预报结果的页面中,如果需要为一个ID为txtCiry的textbox控件建立缓存,其值显示某城市的气温,则可如下设置:<%@OutputCache Duration"60" VaryByParam="txtCity"%>,asp.net会对txtCity控件的值进行判断,只有输入的值与缓存值相同才会从缓存中取出数据。
引入缓存可以极大提高应用程序的性能,当缓存机制也有缺点,就是数据过期的问题,一旦应用程序数据或页面结果值发生变化,则再缓存有效期范围内获取的结果将是过期的不准确的数据。.NET2.0引入了自定义缓存依赖项,特别是基于MS-SQL Server的SqlCacheDependency 特性,使得我们可以避免数据过期的问题,它能够根据数据库中相应数据的变化,通知缓存,并移除那些过期的数据。
3.SqlCacheDependency特性:
监视特定的数据库表,并创建依赖于该表以及表中数据行的依赖项,当数据表或表中特定行的数据发生更改时,具有依赖项的数据项就会失效,并自动从Cache中删除,从而保证了缓存中不再保留过期数据.
SQL SERVER 2005及以上完全支持SqlDependency特性,SQL SERVER7.0及2000不支持。配置SQL server以支持SqlCacheDependency特性:aspnet_regsql命令行工具或使用SqlCacheDenpendencyAdmin类:
3.1.使用aspnet_regsql:
aspnet_regsql工具位于C:\Windows\Microsoft.NET\Framework64\v4.0.30319中,使用cmd命令行转到该目录,并执行命令:aspnet_regsql -S localhost -E -d 数据库名 -ed 为数据库启用sqlCacheDependency特性。
执行命令:aspnet_regsql -S localhost -E -t 表名 -et 为某个表启用sqlCacheDependency特性。
此时会在数据库中建立一个名为AspNet_SqlCacheTablesForChangeNotification的表,同时添加了一组存储过程并为启用了SqlCacheDependency特性的表创建了触发器。
命令参数说明:
3.2.利用SqlCacheDependencyAdmin类:
3.3.SqlCacheDependency特性在APS.NET应用程序中的实现:
3.3.1.为了可扩展性,首先建立ICacheDependency类库,在其中建立IPetShopCacheDependency接口:
- public interface IPetShopCacheDependency {
- AggregateCacheDependency GetDependency();
- }
AggregateCacheDependency类起到了组合CacheDependency对象的作用,他可以将多个CacheDependency对象甚至不同类型的CacheDependency对象与缓存项建立关联。
3.3.2实现ICacheDependency:
即为数据库表建立对应的SqlCahcedependency依赖项。首先建立TableCacheDependency类库,在其中建立TableDependency类
- 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"];//要建立SqlCacheDependency依赖项的数据库名
- string tableConfig = ConfigurationManager.AppSettings[configKey];//要建立SqlCacheDependency依赖项的数据库表名
- string[] tables = tableConfig.Split(configurationSeparator);
- foreach (string tableName in tables)
- dependency.Add(new SqlCacheDependency(dbName, tableName));//为数据库表建立对应的SqlCahcedependency依赖项并加入到AggregateCacheDependency的监视中
- }
- public AggregateCacheDependency GetDependency() {
- return dependency;
- }
- }
- <add key="CacheDatabaseName" value="MSPetShop4"/>
- <add key="CategoryTableDependency" value="Category"/>
- <add key="ProductTableDependency" value="Product,Category"/>
- <add key="ItemTableDependency" value="Product,Category,Item"/>
- 根据各个数据表之间的依赖关系,因而不同的数据表需要建立的依赖项也是不同的。
- 然后为各个启用了SqlCacheDependency特性的数据表建立实体对象并继承TableDependency:
- <pre name="code" class="csharp">public class Category : TableDependency {
- /// <summary>
- /// Call its base constructor by passing its specific configuration key
- /// </summary>
- public Category() : base("CategoryTableDependency") { }
- }
- <pre name="code" class="html"> 3.3.3.创建CacheDependncy抽象工厂:
- 由于TableDependency类的派生类(即启用了sqlcachedependency特性的数据表对于的实体)均需要在被调用时创建各自的对象,故可使用工厂模式;
- 首先创建CacheDependencyFactory类库,创建DependencyAccess类:为每个启用了sqlcachedependency特性的数据表对于的实体提供创建实例的方法:
- <pre name="code" class="csharp"> public static class DependencyAccess {
- public static IPetShopCacheDependency CreateCategoryDependency() {
- return LoadInstance("Category");
- }
- public static IPetShopCacheDependency CreateProductDependency() {
- return LoadInstance("Product");
- }
- public static IPetShopCacheDependency CreateItemDependency() {
- return LoadInstance("Item");
- }
- private static IPetShopCacheDependency LoadInstance(string className) {
- string path = ConfigurationManager.AppSettings["CacheDependencyAssembly"];
- string fullyQualifiedClass = path + "." + className;
- return (IPetShopCacheDependency)Assembly.Load(path).CreateInstance(fullyQualifiedClass);
- }
- }
- 虽然DependencyAccess类创建了实现IPetShopCaheDependency接口的类Category\Item\Product,然而我们之所以引入接口IPetShopCaheDependency
- ,其目的就在于<strong>为每个实体获得创建了依赖项的AggregateCacheDependency</strong>。可以通过<strong>Facade模式</strong>来获取。
3.3.4.Facade模式:
Facade模式的引入并非要引入一个新的功能,而是在现有功能的基础上提供一个更高层次的抽象,使得调用者可以直接调用,而不用关心内部的实现形式。
在CacheDependencyFactory类库中创建DependencyFacade类:
- <pre name="code" class="csharp"> public static class DependencyFacade {
- private static readonly string path = ConfigurationManager.AppSettings["CacheDependencyAssembly"];//PetShop.TableCacheDependency
- public static AggregateCacheDependency GetCategoryDependency() {
- if (!string.IsNullOrEmpty(path))
- return DependencyAccess.CreateCategoryDependency().GetDependency();
- else
- return null;
- }
- public static AggregateCacheDependency GetProductDependency() {
- if (!string.IsNullOrEmpty(path))
- return DependencyAccess.CreateProductDependency().GetDependency();
- else
- return null;
- }
- public static AggregateCacheDependency GetItemDependency() {
- if (!string.IsNullOrEmpty(path))
- return DependencyAccess.CreateItemDependency().GetDependency();
- else
- return null;
- }
- }
- <pre name="code" class="html"><pre name="code" class="csharp"><pre name="code" class="html"><pre name="code" class="csharp"><pre name="code" class="csharp">DependencyFacade封装了获取AggregateCacheDependency的方法,这样,调用者可以调用相关方法获得创建相关依赖项的AggregateCacheDependency类型对象
- AggregateCacheDependency denpendency = DependencyFacade.GetCategoryDependency();
- 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);
- string data = (string)HttpRuntime.Cache[cacheKey];
- if (data == null) {
- int cacheDuration = int.Parse(ConfigurationManager.AppSettings["CategoryCacheDuration"]);
- data = category.GetCategory(categoryId).Name;
- <strong> AggregateCacheDependency cd = DependencyFacade.GetCategoryDependency();</strong>
- //Store the output in the data cache, and Add the necessary AggregateCacheDependency object
- HttpRuntime.Cache.Add(cacheKey, data, <strong>cd</strong>, DateTime.Now.AddHours(cacheDuration), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
- }
- return data;
- }
BLL业务逻辑层中与Category,Item等实体有关的业务方法,其实现逻辑是调用DAL层对象访问数据库,以获取相关数据,为了给这些方法增加缓存机制的逻辑,需要引入一个新的对象去控制原来的BLL业务对象,该对象中的方法与BLL业务对象中的方法一样,只是从缓存中获取数据,而不访问DAL层,这个对象就是Proxy模式中的代理对象。
在Web中的APP_Code文件夹中建立ProductDataProxy类作为BLL业务层的业务对象Product的代理对象,并在其方法中使用缓存机制:
- public static class ProductDataProxy {
- 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 BLL 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;
- }
- /// <summary>
- /// This method acts as a proxy between the web and BLL to check whether the
- /// underlying search result has already been cached.
- /// </summary>
- /// <param name="text">Search Text</param>
- /// <returns>List of ProductInfo from Cache or Business component</returns>
- public static IList<ProductInfo> GetProductsBySearch(string text) {
- Product product = new Product();
- if (!enableCaching)
- return product.GetProductsBySearch(text);
- string key = "product_search_" + text;
- 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.GetProductsBySearch(text);
- // 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;
- }
- /// <summary>
- /// This method acts as a proxy between the web and business components to check whether the
- /// underlying product has already been cached.
- /// </summary>
- /// <param name="productId">Product Id</param>
- /// <returns>ProductInfo from Cache or Business component</returns>
- public static ProductInfo GetProduct(string productId) {
- Product product = new Product();
- if (!enableCaching)
- return product.GetProduct(productId);
- string key = "product_" + productId;
- ProductInfo data = (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.GetProduct(productId);
- // 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;
- }
- }
为页面输出缓存设置缓存依赖项SqlCacheDependency:
- this.CachePolicy.Dependency = DependencyFacade.GetProductDependency();
- asp.net的缓存机制
- ASP.NET的缓存机制
- ASP.NET的缓存机制
- ASP.net缓存机制
- ASP.NET缓存机制
- asp.net缓存机制
- ASP.NET: SqlDataSource控件的缓存机制
- asp.net缓存机制(转载)
- asp.net缓存机制总结
- ASP.NET 页面缓存机制
- ASP.NET 4.0 缓存机制
- 浅析ASP.NET FORUMS中缓存机制的应用
- MSDN中关于ASP.NET缓存机制的论述
- ASP.NET的缓存机制及技术实现
- asp.net的缓存
- ASP.NET的缓存
- ASP.NET的缓存
- ASP.NET的缓存
- 服务器基础配置(Redhat/CentOS)
- 在数组 arr 的 index 处添加元素 item。不要直接修改数组 arr,结果返回新的数组
- JS Tab切换 选项卡 五种方法
- Fragment的生命周期以及与Activity生命周期联动
- APP开发流程实例讲解-儒释道网络电台八天开发全程-在Android Studio中完成界面设计
- ASP.NET的缓存机制
- 多说 评论系统
- VirtualBox虚拟vdi磁盘扩容
- 愚人的linux内核2440移植札记(超曲折版)
- swift 小知识
- 逆向工程之表达式优化识别(5)-取模
- [UnityShader3]光之翼效果
- App上架流程
- 在windows 环境下配置并使用Git(以Android Studio 中使用开源中国Git为例)