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

10

namespace 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 cache33

if (data == null)
{34

35
// If the data is not in the cache then fetch the data from the business logic tier36
data = product.GetProductsByCategory(category);37

38
// Create a AggregateCacheDependency object from the factory39
AggregateCacheDependency cd = DependencyFacade.GetProductDependency();40

41
// Store the output in the data cache, and Add the necessary AggregateCacheDependency object42
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 cache65

if (data == null)
{66

67
// If the data is not in the cache then fetch the data from the business logic tier68
data = product.GetProductsBySearch(text);69

70
// Create a AggregateCacheDependency object from the factory71
AggregateCacheDependency cd = DependencyFacade.GetProductDependency();72

73
// Store the output in the data cache, and Add the necessary AggregateCacheDependency object74
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 cache97

if (data == null)
{98

99
// If the data is not in the cache then fetch the data from the business logic tier100
data = product.GetProduct(productId);101

102
// Create a AggregateCacheDependency object from the factory103
AggregateCacheDependency cd = DependencyFacade.GetProductDependency();104

105
// Store the output in the data cache, and Add the necessary AggregateCacheDependency object106
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个类其实加个缓存罢了,我想不考虑缓存的话,这个类可以省去了