二)网站项目-ProductDataProxy,Petshop 源码分析

本文介绍了一个产品数据代理类,该类通过缓存机制提高数据访问效率。具体实现了按类别获取产品列表、按搜索文本获取产品列表及按ID获取单个产品的功能。

 

Code
  1using System;
  2using System.Web;
  3using System.Web.Caching;
  4using System.Collections.Generic;
  5using System.Configuration;
  6using PetShop.Model;
  7using PetShop.BLL;
  8using PetShop.CacheDependencyFactory;
  9
 10namespace PetShop.Web {
 11    public static class ProductDataProxy {
 12
 13        private static readonly int productTimeout = int.Parse(ConfigurationManager.AppSettings["ProductCacheDuration"]);
 14        private static readonly bool enableCaching = bool.Parse(ConfigurationManager.AppSettings["EnableCaching"]);
 15
 16        /**//// <summary>
 17        /// This method acts as a proxy between the web and business components to check whether the 
 18        /// underlying data has already been cached.
 19        /// </summary>
 20        /// <param name="category">Category</param>
 21        /// <returns>List of ProductInfo from Cache or Business component</returns>

 22        public static IList<ProductInfo> GetProductsByCategory(string category) {
 23
 24            Product product = new Product();
 25
 26            if (!enableCaching)
 27                return product.GetProductsByCategory(category);
 28
 29            string key = "product_by_category_" + category;
 30            IList<ProductInfo> data = (IList<ProductInfo>)HttpRuntime.Cache[key];
 31
 32            // Check if the data exists in the data cache
 33            if (data == null{
 34
 35                // If the data is not in the cache then fetch the data from the business logic tier
 36                data = product.GetProductsByCategory(category);
 37
 38                // Create a AggregateCacheDependency object from the factory
 39                AggregateCacheDependency cd = DependencyFacade.GetProductDependency();
 40
 41                // Store the output in the data cache, and Add the necessary AggregateCacheDependency object
 42                HttpRuntime.Cache.Add(key, data, cd, DateTime.Now.AddHours(productTimeout), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
 43            }

 44
 45            return data;
 46        }

 47
 48        /**//// <summary>
 49        /// This method acts as a proxy between the web and business components to check whether the 
 50        /// underlying search result has already been cached.
 51        /// </summary>
 52        /// <param name="text">Search Text</param>
 53        /// <returns>List of ProductInfo from Cache or Business component</returns>

 54        public static IList<ProductInfo> GetProductsBySearch(string text) {
 55
 56            Product product = new Product();
 57
 58            if (!enableCaching)
 59                return product.GetProductsBySearch(text);
 60
 61            string key = "product_search_" + text;
 62            IList<ProductInfo> data = (IList<ProductInfo>)HttpRuntime.Cache[key];
 63
 64            // Check if the data exists in the data cache
 65            if (data == null{
 66
 67                // If the data is not in the cache then fetch the data from the business logic tier
 68                data = product.GetProductsBySearch(text);
 69
 70                // Create a AggregateCacheDependency object from the factory
 71                AggregateCacheDependency cd = DependencyFacade.GetProductDependency();
 72
 73                // Store the output in the data cache, and Add the necessary AggregateCacheDependency object
 74                HttpRuntime.Cache.Add(key, data, cd, DateTime.Now.AddHours(productTimeout), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
 75            }

 76
 77            return data;
 78        }

 79
 80        /**//// <summary>
 81        /// This method acts as a proxy between the web and business components to check whether the 
 82        /// underlying product has already been cached.
 83        /// </summary>
 84        /// <param name="productId">Product Id</param>
 85        /// <returns>ProductInfo from Cache or Business component</returns>

 86        public static ProductInfo GetProduct(string productId) {
 87
 88            Product product = new Product();
 89
 90            if (!enableCaching)
 91                return product.GetProduct(productId);
 92
 93            string key = "product_" + productId;
 94            ProductInfo data = (ProductInfo)HttpRuntime.Cache[key];
 95
 96            // Check if the data exists in the data cache
 97            if (data == null{
 98
 99                // If the data is not in the cache then fetch the data from the business logic tier
100                data = product.GetProduct(productId);
101
102                // Create a AggregateCacheDependency object from the factory
103                AggregateCacheDependency cd = DependencyFacade.GetProductDependency();
104
105                // Store the output in the data cache, and Add the necessary AggregateCacheDependency object
106                HttpRuntime.Cache.Add(key, data, cd, DateTime.Now.AddHours(productTimeout), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
107            }

108            return data;
109        }

110    }

111}

112


这个类是一个产品数据代理类,
    public static IList<ProductInfo> GetProductsByCategory(string category)
    public static IList<ProductInfo> GetProductsBySearch(string text)
    public static ProductInfo GetProduct(string productId)
这3个类其实加个缓存罢了,我想不考虑缓存的话,这个类可以省去了

转载于:https://www.cnblogs.com/loning/archive/2008/01/28/1055376.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值