![](/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Web;
3
using System.Web.Caching;
4
using System.Collections.Generic;
5
using System.Configuration;
6
using PetShop.Model;
7
using PetShop.BLL;
8
using PetShop.CacheDependencyFactory;
9![](/Images/OutliningIndicators/None.gif)
10![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
namespace PetShop.Web
{
11![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static class ProductDataProxy
{
12![](/Images/OutliningIndicators/InBlock.gif)
13
private static readonly int productTimeout = int.Parse(ConfigurationManager.AppSettings["ProductCacheDuration"]);
14
private static readonly bool enableCaching = bool.Parse(ConfigurationManager.AppSettings["EnableCaching"]);
15![](/Images/OutliningIndicators/InBlock.gif)
16![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static IList<ProductInfo> GetProductsByCategory(string category)
{
23![](/Images/OutliningIndicators/InBlock.gif)
24
Product product = new Product();
25![](/Images/OutliningIndicators/InBlock.gif)
26
if (!enableCaching)
27
return product.GetProductsByCategory(category);
28![](/Images/OutliningIndicators/InBlock.gif)
29
string key = "product_by_category_" + category;
30
IList<ProductInfo> data = (IList<ProductInfo>)HttpRuntime.Cache[key];
31![](/Images/OutliningIndicators/InBlock.gif)
32
// Check if the data exists in the data cache
33![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (data == null)
{
34![](/Images/OutliningIndicators/InBlock.gif)
35
// If the data is not in the cache then fetch the data from the business logic tier
36
data = product.GetProductsByCategory(category);
37![](/Images/OutliningIndicators/InBlock.gif)
38
// Create a AggregateCacheDependency object from the factory
39
AggregateCacheDependency cd = DependencyFacade.GetProductDependency();
40![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
45
return data;
46
}
47![](/Images/OutliningIndicators/InBlock.gif)
48![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static IList<ProductInfo> GetProductsBySearch(string text)
{
55![](/Images/OutliningIndicators/InBlock.gif)
56
Product product = new Product();
57![](/Images/OutliningIndicators/InBlock.gif)
58
if (!enableCaching)
59
return product.GetProductsBySearch(text);
60![](/Images/OutliningIndicators/InBlock.gif)
61
string key = "product_search_" + text;
62
IList<ProductInfo> data = (IList<ProductInfo>)HttpRuntime.Cache[key];
63![](/Images/OutliningIndicators/InBlock.gif)
64
// Check if the data exists in the data cache
65![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (data == null)
{
66![](/Images/OutliningIndicators/InBlock.gif)
67
// If the data is not in the cache then fetch the data from the business logic tier
68
data = product.GetProductsBySearch(text);
69![](/Images/OutliningIndicators/InBlock.gif)
70
// Create a AggregateCacheDependency object from the factory
71
AggregateCacheDependency cd = DependencyFacade.GetProductDependency();
72![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
77
return data;
78
}
79![](/Images/OutliningIndicators/InBlock.gif)
80![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static ProductInfo GetProduct(string productId)
{
87![](/Images/OutliningIndicators/InBlock.gif)
88
Product product = new Product();
89![](/Images/OutliningIndicators/InBlock.gif)
90
if (!enableCaching)
91
return product.GetProduct(productId);
92![](/Images/OutliningIndicators/InBlock.gif)
93
string key = "product_" + productId;
94
ProductInfo data = (ProductInfo)HttpRuntime.Cache[key];
95![](/Images/OutliningIndicators/InBlock.gif)
96
// Check if the data exists in the data cache
97![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (data == null)
{
98![](/Images/OutliningIndicators/InBlock.gif)
99
// If the data is not in the cache then fetch the data from the business logic tier
100
data = product.GetProduct(productId);
101![](/Images/OutliningIndicators/InBlock.gif)
102
// Create a AggregateCacheDependency object from the factory
103
AggregateCacheDependency cd = DependencyFacade.GetProductDependency();
104![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/None.gif)
这个类是一个产品数据代理类,
public static IList<ProductInfo> GetProductsByCategory(string category)
public static IList<ProductInfo> GetProductsBySearch(string text)
public static ProductInfo GetProduct(string productId)
这3个类其实加个缓存罢了,我想不考虑缓存的话,这个类可以省去了