First they ignore you
then they ridicule you
then they fight you
then you win
    -- Mahatma Gandhi
Chinese => English     英文 => 中文             
随笔-221  评论-1047  文章-0  trackbacks-0
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>
      
    
<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>
     
<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 山风小子 阅读(3652) 评论(5)  编辑  收藏 所属分类: Groovy & Grails