grails-cache插件項目發起于2007年7月,由于James和我兩人都很忙,中間中斷過一些時間。
幾個星期前,我將整個grails-cache重寫了一下,這周末將我們Team新成員Bakhtiyor所寫的另外一個插件的一些功能整合進來,并作了一些小重構。
現在對該插件的功能做一些介紹:
1,緩存靜態內容,比如js文件,css文件,圖片文件等,grails-cache會對js文件和css文件優化壓縮,并設置header以確保沒有stale的文件不會再次從服務器端傳給客戶端,換句話說,如果服務器端的靜態比如js文件,css文件和圖片文件沒有修改,那么客戶端的瀏覽器一旦第一次接收到請求文件后,就將它們緩存起來,服務器不需要一而再,再而三地將相同的文件發送給它們了,這樣就可以顯著地減少帶寬占用,提升服務器的性能。要知道在客戶的大部分時間都用在等待靜態文件的下載,一旦省去了這些文件的下載,客戶就可以很快看到頁面。
2,緩存動態內容,比如生成的頁面片段。使頁面展現緩慢的另外一個因素就是生成頁面本身就十分耗時,所以緩存這些耗時的生成結果對于提升性能是十分可觀的。還有些動態內容生成一次就夠,沒必要重復生成,這樣也可以起到提升性能的效果。grails-cache同樣提供了相關解決方案。
此外,考慮到部分Grails開發人員對gsp不太熟悉,我重寫gsp標簽的同時,也實現了相同功能的jsp標簽。
下面是grails-cache的使用:
1,簡單地緩存靜態文件(緩存制定目錄(dir)下的制定文件(file)):
<%@?page?language="java"?import="java.util.*"?pageEncoding="GB18030"%>
<%@?taglib?uri="http://grails.codehaus.org/tags"?prefix="g"?%>
<%
String?path?=?request.getContextPath();
String?basePath?=?request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">
<html>
??<head>
????<base?href="<%=basePath%>">
????
????<title>My?JSP?'test.jsp'?starting?page</title>
????
????<meta?http-equiv="pragma"?content="no-cache">
????<meta?http-equiv="cache-control"?content="no-cache">
????<meta?http-equiv="expires"?content="0">????
????<meta?http-equiv="keywords"?content="keyword1,keyword2,keyword3">
????<meta?http-equiv="description"?content="This?is?my?page">
????<!--
????<link?rel="stylesheet"?type="text/css"?href="styles.css">
????-->
<link?href="${cache(dir:'css',file:'toCache.css')}"?rel="stylesheet"?type="text/css"?/>
<script?type="text/javascript"?src="${cache(dir:'js',?file:'toCache.js')}"></script>
??</head>
??
??<body>
??????<h1>Test</h1>
??????
????<a?href="javascript:sayHello();">sayHello</a>?<br>
????<img?src="${cache(dir:'images',?file:'hat.gif')}">
??</body>
</html>
2,合并緩存靜態文件(合并指定的目錄(dir)下的制定類型(type)的靜態文本文件,注意:沒有遞歸合并子目錄內容,這是出于性能考慮)
<%@?page?language="java"?import="java.util.*"?pageEncoding="GB18030"%>
<%@?taglib?uri="http://grails.codehaus.org/tags"?prefix="g"?%>
<%
String?path?=?request.getContextPath();
String?basePath?=?request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">
<html>
??<head>
????<base?href="<%=basePath%>">
????
????<title>My?JSP?'testMerge.jsp'?starting?page</title>
????
????<meta?http-equiv="pragma"?content="no-cache">
????<meta?http-equiv="cache-control"?content="no-cache">
????<meta?http-equiv="expires"?content="0">????
????<meta?http-equiv="keywords"?content="keyword1,keyword2,keyword3">
????<meta?http-equiv="description"?content="This?is?my?page">
????<!--
????<link?rel="stylesheet"?type="text/css"?href="styles.css">
????-->
????
????<link?href="${cache(dir:'css',?type:'text/css')}"?rel="stylesheet"?type="text/css"?/>
????<script?type="text/javascript"?src="${cache(dir:'js',?type:'text/js')}"></script>
??</head>
??
??<body>
?????<h1>Test</h1>
?????<a?href="javascript:sayHello();">testMerge</a>?<br>
??</body>
</html>
3,緩存耗時的生成結果(g:cacheFragment標簽有3個屬性: key, ttl, group,其中key是必須的。key是緩存內容的一個在group中的id,ttl可以理解為緩存多久,group類似于命名空間防止id與id之間發生沖突)
<%@?page?language="java"?import="java.util.*"?pageEncoding="GB18030"%>
<%@?taglib?uri="http://grails.codehaus.org/tags"?prefix="g"?%>
<%
String?path?=?request.getContextPath();
String?basePath?=?request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">
<html>
??<head>
????<base?href="<%=basePath%>">
????
????<title>My?JSP?'testFragment.jsp'?starting?page</title>
????
????<meta?http-equiv="pragma"?content="no-cache">
????<meta?http-equiv="cache-control"?content="no-cache">
????<meta?http-equiv="expires"?content="0">????
????<meta?http-equiv="keywords"?content="keyword1,keyword2,keyword3">
????<meta?http-equiv="description"?content="This?is?my?page">
????<!--
????<link?rel="stylesheet"?type="text/css"?href="styles.css">
????-->
??</head>
??
??<body>?
????This?is?my?JSP?page.?<br>
????<g:cacheFragment?key="for-loop"?ttl="3600">
????????<%
????????for?(i?in?0..10000)?{
????????????println?"<font?color='#57${i?%?10}BB8'>${i}</font>"
????????}
????%>
????</g:cacheFragment>
??</body>
</html>
我們正對該插件進行性能測試,相信該插件的發布也快了。
附:
朝花夕拾——Groovy & Grails
posted on 2008-03-30 23:16
山風小子 閱讀(3662)
評論(5) 編輯 收藏 所屬分類:
Groovy & Grails