訪問時將js和css壓縮並且緩存在客戶端,
采用的是Yahoo.Yui.Compressor組件還完成的,從這里可下載
創建一個IHttpHandler來處理文件
1
public
class CombineFiles : IHttpHandler
2 {
3 private const string CacheKeyFormat = " _CacheKey_{0}_ ";
4
5 private const bool IsCompress = true; // 需要壓縮
6
7 public bool IsReusable
8 {
9 get
10 {
11 return false;
12 }
13 }
14
15 public void ProcessRequest(HttpContext context)
16 {
17 HttpRequest request = context.Request;
18 HttpResponse response = context.Response;
19
20 string cachekey = string.Empty;
21
22 string type = request.QueryString[ " type "];
23 if (! string.IsNullOrEmpty(type) && (type == " css " || type == " js "))
24 {
25 if (type == " js ")
26 {
27 response.ContentType = " text/javascript ";
28
29 }
30 else if (type == " css ")
31 {
32 response.ContentType = " text/css ";
33 }
34
35 cachekey = string.Format(CacheKeyFormat, type);
36
37 CompressCacheItem cacheItem = HttpRuntime.Cache[ cachekey] as CompressCacheItem;
38 if (cacheItem == null)
39 {
40 string content = string.Empty;
41 string path = context.Server.MapPath( "");
42 // 找到這個目錄下所有的js或css文件,當然也可以進行配置,需求請求壓縮哪些文件
43 // 這里就將所的有文件都請求壓縮
44 string[] files = Directory.GetFiles(path, " *. " + type);
45 StringBuilder sb = new StringBuilder();
46 foreach ( string fileName in files)
47 {
48 if (File.Exists(fileName))
49 {
50 string readstr = File.ReadAllText(fileName, Encoding.UTF8);
51 sb.Append(readstr);
52 }
53 }
54
55 content = sb.ToString();
56
57 // 開始壓縮文件
58 if (IsCompress)
59 {
60 if (type.Equals( " js "))
61 {
62 content = JavaScriptCompressor.Compress(content);
63 }
64 else if (type.Equals( " css "))
65 {
66 content = CssCompressor.Compress(content);
67 }
68 }
69
70 // 輸入到客戶端還可以進行Gzip壓縮 ,這里就省略了
71
72 cacheItem = new CompressCacheItem() { Type = type, Content = content, Expires = DateTime.Now.AddDays( 30) };
73 HttpRuntime.Cache.Insert(cachekey, cacheItem, null, cacheItem.Expires, TimeSpan.Zero);
74 }
75
76 string ifModifiedSince = request.Headers[ " If-Modified-Since "];
77 if (! string.IsNullOrEmpty(ifModifiedSince)
78 && TimeSpan.FromTicks(cacheItem.Expires.Ticks - DateTime.Parse(ifModifiedSince).Ticks).Seconds < 0)
79 {
80 response.StatusCode = ( int)System.Net.HttpStatusCode.NotModified;
81 response.StatusDescription = " Not Modified ";
82 }
83 else
84 {
85 response.Write(cacheItem.Content);
86 SetClientCaching(response, cacheItem.Expires);
87 }
88 }
89
90 }
91
92 private void SetClientCaching(HttpResponse response, DateTime expires)
93 {
94 response.Cache.SetETag(DateTime.Now.Ticks.ToString());
95 response.Cache.SetLastModified(DateTime.Now);
96
97 // public 以指定響應能由客戶端和共享(代理)緩存進行緩存。
98 response.Cache.SetCacheability(HttpCacheability.Public);
99
100 // 是允許文檔在被視為陳舊之前存在的最長絕對時間。
101 response.Cache.SetMaxAge(TimeSpan.FromTicks(expires.Ticks));
102
103 response.Cache.SetSlidingExpiration( true);
104 }
105 private class CompressCacheItem
106 {
107 /// <summary>
108 /// 類型 js 或 css
109 /// </summary>
110 public string Type { get; set; } // js css
111 /// <summary>
112 /// 內容
113 /// </summary>
114 public string Content { set; get; }
115 /// <summary>
116 /// 過期時間
117 /// </summary>
118 public DateTime Expires { set; get; }
119 }
120 }
2 {
3 private const string CacheKeyFormat = " _CacheKey_{0}_ ";
4
5 private const bool IsCompress = true; // 需要壓縮
6
7 public bool IsReusable
8 {
9 get
10 {
11 return false;
12 }
13 }
14
15 public void ProcessRequest(HttpContext context)
16 {
17 HttpRequest request = context.Request;
18 HttpResponse response = context.Response;
19
20 string cachekey = string.Empty;
21
22 string type = request.QueryString[ " type "];
23 if (! string.IsNullOrEmpty(type) && (type == " css " || type == " js "))
24 {
25 if (type == " js ")
26 {
27 response.ContentType = " text/javascript ";
28
29 }
30 else if (type == " css ")
31 {
32 response.ContentType = " text/css ";
33 }
34
35 cachekey = string.Format(CacheKeyFormat, type);
36
37 CompressCacheItem cacheItem = HttpRuntime.Cache[ cachekey] as CompressCacheItem;
38 if (cacheItem == null)
39 {
40 string content = string.Empty;
41 string path = context.Server.MapPath( "");
42 // 找到這個目錄下所有的js或css文件,當然也可以進行配置,需求請求壓縮哪些文件
43 // 這里就將所的有文件都請求壓縮
44 string[] files = Directory.GetFiles(path, " *. " + type);
45 StringBuilder sb = new StringBuilder();
46 foreach ( string fileName in files)
47 {
48 if (File.Exists(fileName))
49 {
50 string readstr = File.ReadAllText(fileName, Encoding.UTF8);
51 sb.Append(readstr);
52 }
53 }
54
55 content = sb.ToString();
56
57 // 開始壓縮文件
58 if (IsCompress)
59 {
60 if (type.Equals( " js "))
61 {
62 content = JavaScriptCompressor.Compress(content);
63 }
64 else if (type.Equals( " css "))
65 {
66 content = CssCompressor.Compress(content);
67 }
68 }
69
70 // 輸入到客戶端還可以進行Gzip壓縮 ,這里就省略了
71
72 cacheItem = new CompressCacheItem() { Type = type, Content = content, Expires = DateTime.Now.AddDays( 30) };
73 HttpRuntime.Cache.Insert(cachekey, cacheItem, null, cacheItem.Expires, TimeSpan.Zero);
74 }
75
76 string ifModifiedSince = request.Headers[ " If-Modified-Since "];
77 if (! string.IsNullOrEmpty(ifModifiedSince)
78 && TimeSpan.FromTicks(cacheItem.Expires.Ticks - DateTime.Parse(ifModifiedSince).Ticks).Seconds < 0)
79 {
80 response.StatusCode = ( int)System.Net.HttpStatusCode.NotModified;
81 response.StatusDescription = " Not Modified ";
82 }
83 else
84 {
85 response.Write(cacheItem.Content);
86 SetClientCaching(response, cacheItem.Expires);
87 }
88 }
89
90 }
91
92 private void SetClientCaching(HttpResponse response, DateTime expires)
93 {
94 response.Cache.SetETag(DateTime.Now.Ticks.ToString());
95 response.Cache.SetLastModified(DateTime.Now);
96
97 // public 以指定響應能由客戶端和共享(代理)緩存進行緩存。
98 response.Cache.SetCacheability(HttpCacheability.Public);
99
100 // 是允許文檔在被視為陳舊之前存在的最長絕對時間。
101 response.Cache.SetMaxAge(TimeSpan.FromTicks(expires.Ticks));
102
103 response.Cache.SetSlidingExpiration( true);
104 }
105 private class CompressCacheItem
106 {
107 /// <summary>
108 /// 類型 js 或 css
109 /// </summary>
110 public string Type { get; set; } // js css
111 /// <summary>
112 /// 內容
113 /// </summary>
114 public string Content { set; get; }
115 /// <summary>
116 /// 過期時間
117 /// </summary>
118 public DateTime Expires { set; get; }
119 }
120 }
最后在配置文件中配置一下CombineFiles.axd文件,具體配置略
引用如下
<script type="text/javascript" src="/js/CombineFiles.axd?type=js"></script>
<link rel="stylesheet" type="text/css" href="/css/CombineFiles.axd?type=css" />