Ehcache入门指南

openkk 12年前
     <table style="width:572px;border-collapse:collapse;background:#6699ff;" class="ke-zeroborder" border="0" cellspacing="0" cellpadding="0">     <tbody>      <tr style="height:24.3pt;">       <td style="border-bottom-color:#ece9d8;padding-bottom:0cm;background-color:transparent;border-top-color:#ece9d8;padding-left:5.4pt;width:428.7pt;padding-right:5.4pt;height:24.3pt;border-right-color:#ece9d8;border-left-color:#ece9d8;padding-top:0cm;" width="572">        <div>         <span style="font-size:medium;"><strong><span style="font-size:small;">简介</span></strong></span>        </div> </td>      </tr>     </tbody>    </table>    <div>     <span style="font-size:9pt;"> </span>    </div>    <div>     <span style="font-size:9pt;">Ehcache</span>     <span style="font-size:9pt;">可以直接使用。也可以和</span>     <span style="font-size:9pt;">Hibernate</span>     <span style="font-size:9pt;">对象</span>     <span style="font-size:9pt;">/</span>     <span style="font-size:9pt;">关系框架结合使用。还可以做</span>     <span style="font-size:9pt;">Servlet</span>     <span style="font-size:9pt;">缓存。</span>    </div>    <div>     <span style="font-size:9pt;"> </span>    </div>    <table style="width:572px;border-collapse:collapse;background:#6699ff;" class="ke-zeroborder" border="0" cellspacing="0" cellpadding="0">     <tbody>      <tr style="height:23.4pt;">       <td style="border-bottom-color:#ece9d8;padding-bottom:0cm;background-color:transparent;border-top-color:#ece9d8;padding-left:5.4pt;width:428.7pt;padding-right:5.4pt;height:23.4pt;border-right-color:#ece9d8;border-left-color:#ece9d8;padding-top:0cm;" width="572">        <div>         <span style="font-size:small;"><span style="font-size:x-small;"><strong>通用缓存</strong></span></span>        </div> </td>      </tr>     </tbody>    </table>    <div>     <span style="font-size:9pt;"> </span>    </div>    <div style="text-indent:-8.5pt;margin:0cm 0cm 0pt 8.5pt;">     <span style="color:#999999;"><span style="font-size:small;"><span style="font-size:x-small;">·</span><span style="font:7pt 'Times New Roman';">   </span></span></span>     <span style="font-size:9pt;">确保</span>     <span style="font-size:9pt;">JDK</span>     <span style="font-size:9pt;">版本支持你现有的</span>     <span style="font-size:9pt;">Ehcache</span>     <span style="font-size:9pt;">版本,</span>     <span style="font-size:9pt;">Ehcache</span>     <span style="font-size:9pt;">支持</span>     <span style="font-size:9pt;">JDK1.4</span>     <span style="font-size:9pt;">和</span>     <span style="font-size:9pt;">1.5</span>     <span style="font-size:9pt;">以及</span>     <span style="font-size:9pt;">1.6</span>     <span style="font-size:9pt;">版本。</span>    </div>    <div style="text-indent:-8.5pt;margin:0cm 0cm 0pt 8.5pt;">     <span style="color:#999999;"><span style="font-size:small;"><span style="font-size:x-small;">·</span><span style="font:7pt 'Times New Roman';">   </span></span></span>     <span style="font-size:9pt;">Ehcache jar</span>     <span style="font-size:9pt;">包加入你的</span>     <span style="font-size:9pt;">classpath</span>     <span style="font-size:9pt;">环境变量中。</span>    </div>    <div style="text-indent:-8.5pt;margin:0cm 0cm 0pt 8.5pt;">     <span style="color:#999999;"><span style="font-size:small;"><span style="font-size:x-small;">·</span><span style="font:7pt 'Times New Roman';">   </span></span></span>     <span style="font-size:9pt;">Ehcache</span>     <span style="font-size:9pt;">依赖的类库加入到你的</span>     <span style="font-size:9pt;">classpath</span>     <span style="font-size:9pt;">环境变量中,不同版本的</span>     <span style="font-size:9pt;">Ehcache</span>     <span style="font-size:9pt;">依赖类库不尽相同,请注意的</span>     <span style="font-size:9pt;">Ehcache</span>     <span style="font-size:9pt;">版本以及相关依赖类库,这里不做累述,可以参考</span>    </div>    <div style="text-indent:8.5pt;">     <span style="font-size:9pt;"><a href="/misc/goto?guid=4959498180411066934"><span style="color:#99ccff;">http://ehcache.sourceforge.net/documentation/dependencies.html</span></a></span>    </div>    <div style="text-indent:-8.5pt;margin:0cm 0cm 0pt 8.5pt;">     <span style="color:#999999;"><span style="font-size:small;"><span style="font-size:x-small;">·</span><span style="font:7pt 'Times New Roman';">   </span></span></span>     <span style="font-size:9pt;">配置</span>     <span style="font-size:9pt;">ehcache.xml</span>     <span style="font-size:9pt;">配置文件,并加入到</span>     <span style="font-size:9pt;">classpath</span>     <span style="font-size:9pt;">中。</span>    </div>    <div style="text-indent:-8.5pt;margin:0cm 0cm 0pt 8.5pt;">     <span style="color:#999999;"><span style="font-size:small;"><span style="font-size:x-small;">·</span><span style="font:7pt 'Times New Roman';">   </span></span></span>     <span style="font-size:9pt;">配置</span>     <span style="font-size:9pt;">logging</span>     <span style="font-size:9pt;">到合适的级别。</span>    </div>    <div>     <span style="font-size:9pt;"> </span>    </div>    <table style="width:572px;border-collapse:collapse;background:#6699ff;" class="ke-zeroborder" border="0" cellspacing="0" cellpadding="0">     <tbody>      <tr style="height:23.4pt;">       <td style="border-bottom-color:#ece9d8;padding-bottom:0cm;background-color:transparent;border-top-color:#ece9d8;padding-left:5.4pt;width:428.7pt;padding-right:5.4pt;height:23.4pt;border-right-color:#ece9d8;border-left-color:#ece9d8;padding-top:0cm;" width="572">        <div>         <strong><span style="font-size:small;"><span><span style="font-size:x-small;">Hibernate</span></span></span></strong>        </div> </td>      </tr>     </tbody>    </table>    <div>     <span style="font-size:9pt;"> </span>    </div>    <div style="text-indent:-8.5pt;margin:0cm 0cm 0pt 8.5pt;">     <span style="color:#999999;"><span style="font-size:small;"><span style="font-size:x-small;">·</span><span style="font:7pt 'Times New Roman';">   </span></span></span>     <span style="font-size:9pt;">参考通用缓存的步骤。</span>    </div>    <div style="text-indent:-8.5pt;margin:0cm 0cm 0pt 8.5pt;">     <span style="color:#999999;"><span style="font-size:small;"><span style="font-size:x-small;">·</span><span style="font:7pt 'Times New Roman';">   </span></span></span>     <span style="font-size:9pt;">在</span>     <span style="font-size:9pt;">ehcache.xml</span>     <span style="font-size:9pt;">中创建缓存。</span>    </div>    <div>     <span style="font-size:9pt;"> </span>    </div>    <table style="width:572px;border-collapse:collapse;background:#6699ff;" class="ke-zeroborder" border="0" cellspacing="0" cellpadding="0">     <tbody>      <tr style="height:24.45pt;">       <td style="border-bottom-color:#ece9d8;padding-bottom:0cm;background-color:transparent;border-top-color:#ece9d8;padding-left:5.4pt;width:428.7pt;padding-right:5.4pt;height:24.45pt;border-right-color:#ece9d8;border-left-color:#ece9d8;padding-top:0cm;" width="572">        <div>         <span style="font-size:small;"><span style="font-size:x-small;"><strong><span>Java EE Servlet</span></strong><strong>缓存</strong></span></span>        </div> </td>      </tr>     </tbody>    </table>    <div>     <span style="font-size:9pt;"> </span>    </div>    <div style="text-indent:-8.5pt;margin:0cm 0cm 0pt 8.5pt;">     <span style="color:#999999;"><span style="font-size:small;"><span style="font-size:x-small;">·</span><span style="font:7pt 'Times New Roman';">   </span></span></span>     <span style="font-size:9pt;">参考通用缓存步骤。</span>    </div>    <div style="text-indent:-8.5pt;margin:0cm 0cm 0pt 8.5pt;">     <span style="color:#999999;"><span style="font-size:small;"><span style="font-size:x-small;">·</span><span style="font:7pt 'Times New Roman';">   </span></span></span>     <span style="font-size:9pt;">在</span>     <span style="font-size:9pt;">ehcache.xml</span>     <span style="font-size:9pt;">中为你的</span>     <span style="font-size:9pt;">web</span>     <span style="font-size:9pt;">页面配置缓存。</span>    </div>    <div style="text-indent:-8.5pt;margin:0cm 0cm 0pt 8.5pt;">     <span style="color:#999999;"><span style="font-size:small;"><span style="font-size:x-small;">·</span><span style="font:7pt 'Times New Roman';">   </span></span></span>     <span style="font-size:9pt;">如果要缓存全部页面,可以使用</span>     <span style="font-size:9pt;">Ehcache</span>     <span style="font-size:9pt;">提供的</span>     <span style="font-size:9pt;">SimplePageCachingFilter</span>     <span style="font-size:9pt;">或是自己写一个子类继承</span>     <span style="font-size:9pt;">CachingFilter</span>     <span style="font-size:9pt;">。</span>    </div>    <div style="text-indent:-8.5pt;margin:0cm 0cm 0pt 8.5pt;">     <span style="color:#999999;"><span style="font-size:small;"><span style="font-size:x-small;">·</span><span style="font:7pt 'Times New Roman';">   </span></span></span>     <span style="font-size:9pt;">若要缓存某个具体的</span>     <span style="font-size:9pt;">JSP</span>     <span style="font-size:9pt;">页面(包括由</span>     <span style="font-size:9pt;">RequestDispatcher</span>     <span style="font-size:9pt;">返回的页面),你可以使用</span>     <span style="font-size:9pt;">Ehcache</span>     <span style="font-size:9pt;">提供的</span>     <span style="font-size:9pt;">SimplePageFragmentCachingFilter</span>     <span style="font-size:9pt;">或是写个子类去继承</span>     <span style="font-size:9pt;">PageFragmentCachingFilter</span>     <span style="font-size:9pt;">。</span>    </div>    <div style="text-indent:-8.5pt;margin:0cm 0cm 0pt 8.5pt;">     <span style="color:#999999;"><span style="font-size:small;"><span style="font-size:x-small;">·</span><span style="font:7pt 'Times New Roman';">   </span></span></span>     <span style="font-size:9pt;">配置</span>     <span style="font-size:9pt;">web.xml</span>     <span style="font-size:9pt;">。这里就相对简单一些,只是将你上俩个步骤使用的</span>     <span style="font-size:9pt;">Filter</span>     <span style="font-size:9pt;">配置到</span>     <span style="font-size:9pt;">web.xml</span>     <span style="font-size:9pt;">中,以便请求再此到来时能够访问缓存中的页面,从而达到提供页面的相应速度。</span>    </div>    <table style="width:572px;border-collapse:collapse;background:#6699ff;" class="ke-zeroborder" border="0" cellspacing="0" cellpadding="0">     <tbody>      <tr style="height:23.55pt;">       <td style="border-bottom-color:#ece9d8;padding-bottom:0cm;background-color:transparent;border-top-color:#ece9d8;padding-left:5.4pt;width:428.7pt;padding-right:5.4pt;height:23.55pt;border-right-color:#ece9d8;border-left-color:#ece9d8;padding-top:0cm;" width="572">        <div>         <span style="font-size:small;"><span style="font-size:x-small;"><strong><span>RESTful</span></strong><strong>和</strong><strong><span>SOAP</span></strong><strong>缓存</strong></span></span>        </div> </td>      </tr>     </tbody>    </table>    <div>     <span style="font-size:9pt;"> </span>    </div>    <div style="text-indent:-8.5pt;margin:0cm 0cm 0pt 8.5pt;">     <span style="color:#999999;"><span style="font-size:small;"><span style="font-size:x-small;">·</span><span style="font:7pt 'Times New Roman';">   </span></span></span>     <span style="font-size:9pt;">从</span>     <span style="font-size:9pt;">http://sourceforge.net/project/showfiles.php?group_id=93232</span>     <span style="font-size:9pt;">下载缓存服务端。</span>    </div>    <div style="text-indent:-8.5pt;margin:0cm 0cm 0pt 8.5pt;">     <span style="color:#999999;"><span style="font-size:small;"><span style="font-size:x-small;">·</span><span style="font:7pt 'Times New Roman';">   </span></span></span>     <span style="font-size:9pt;">使用</span>     <span style="font-size:9pt;">cd</span>     <span style="font-size:9pt;">命令切换到</span>     <span style="font-size:9pt;">bin</span>     <span style="font-size:9pt;">目录下。</span>    </div>    <div style="text-indent:-8.5pt;margin:0cm 0cm 0pt 8.5pt;">     <span style="color:#999999;"><span style="font-size:small;"><span style="font-size:x-small;">·</span><span style="font:7pt 'Times New Roman';">   </span></span></span>     <span style="font-size:9pt;">键入</span>     <span style="font-size:9pt;">startup.sh</span>     <span style="font-size:9pt;">启动服务。</span>    </div>    <div style="margin:0cm 0cm 0pt 8.5pt;">     <span style="font-size:9pt;"> </span>    </div>    <div style="margin:0cm 0cm 0pt 8.5pt;">     <span style="font-size:9pt;">默认情况下使用的是</span>     <span style="font-size:9pt;">8080</span>     <span style="font-size:9pt;">端口,</span>     <span style="font-size:9pt;">RESTful</span>     <span style="font-size:9pt;">和</span>     <span style="font-size:9pt;">SOAP web</span>     <span style="font-size:9pt;">服务器也都使用这个端口,请注意端口占用问题。</span>    </div>    <div>     <span style="font-size:9pt;"> </span>    </div>    <div style="text-indent:-8.5pt;margin:0cm 0cm 0pt 8.5pt;">     <span style="color:#999999;"><span style="font-size:small;"><span style="font-size:x-small;">·</span><span style="font:7pt 'Times New Roman';">   </span></span></span>     <span style="font-size:9pt;">这个时候就可以使用</span>     <span style="font-size:9pt;">Cache Server</span>     <span style="font-size:9pt;">了,你可以使用</span>     <span style="font-size:9pt;">Java</span>     <span style="font-size:9pt;">后者任何其他语言。具体示例请参考</span>    </div>    <div style="margin:0cm 0cm 0pt 8.5pt;">     <span style="font-size:9pt;"><span style="color:#99ccff;"><a href="/misc/goto?guid=4959498180514139191"><span style="color:#006bad;">http://ehcache.sourceforge.net/documentation/cache_server.html</span></a></span></span>     <span style="font-size:9pt;">。</span>    </div>    <div>     <span style="font-size:9pt;"> </span>    </div>    <table style="width:572px;border-collapse:collapse;background:#6699ff;" class="ke-zeroborder" border="0" cellspacing="0" cellpadding="0">     <tbody>      <tr style="height:22.65pt;">       <td style="border-bottom-color:#ece9d8;padding-bottom:0cm;background-color:transparent;border-top-color:#ece9d8;padding-left:5.4pt;width:428.7pt;padding-right:5.4pt;height:22.65pt;border-right-color:#ece9d8;border-left-color:#ece9d8;padding-top:0cm;" width="572">        <div>         <strong><span style="font-size:small;"><span><span style="font-size:x-small;">Jcache style caching</span></span></span></strong>        </div> </td>      </tr>     </tbody>    </table>    <div>     <span style="font-size:9pt;"> </span>    </div>    <div>     <span style="font-size:9pt;">Ehcache</span>     <span style="font-size:9pt;">在</span>     <span style="font-size:9pt;">net.sh.ehcache.jcache</span>     <span style="font-size:9pt;">这个包下,有</span>     <span style="font-size:9pt;">Ehcache</span>     <span style="font-size:9pt;">早期对</span>     <span style="font-size:9pt;">Jcache</span>     <span style="font-size:9pt;">的一个粗略实现。</span>    </div>    <div>     <span style="font-size:9pt;"> </span>    </div>    <table style="width:572px;border-collapse:collapse;background:#6699ff;" class="ke-zeroborder" border="0" cellspacing="0" cellpadding="0">     <tbody>      <tr style="height:23.75pt;">       <td style="border-bottom-color:#ece9d8;padding-bottom:0cm;background-color:transparent;border-top-color:#ece9d8;padding-left:5.4pt;width:428.7pt;padding-right:5.4pt;height:23.75pt;border-right-color:#ece9d8;border-left-color:#ece9d8;padding-top:0cm;" width="572">        <div>         <span style="font-size:small;"><span style="font-size:x-small;"><strong><span>Spring</span></strong><strong>,</strong><strong><span>Cocoon</span></strong><strong>,</strong><strong><span>Acegi</span></strong><strong>和其他框架</strong></span></span>        </div> </td>      </tr>     </tbody>    </table>    <div>     <span style="font-size:9pt;"> </span>    </div>    <div>     <span style="font-size:9pt;">一般,和这些框架结合使用</span>     <span style="font-size:9pt;">Ehcache</span>     <span style="font-size:9pt;">时,没有太多特殊的地方。你只需要注意以下几点:</span>    </div>    <div>     <span style="font-size:9pt;"> </span>    </div>    <div style="text-indent:-8.5pt;margin:0cm 0cm 0pt 8.5pt;">     <span style="color:#999999;"><span style="font-size:small;"><span style="font-size:x-small;">·</span><span style="font:7pt 'Times New Roman';">   </span></span></span>     <span style="font-size:9pt;">注意这些框架中使用的是什么缓存。</span>    </div>    <div style="text-indent:-8.5pt;margin:0cm 0cm 0pt 8.5pt;">     <span style="color:#999999;"><span style="font-size:small;"><span style="font-size:x-small;">·</span><span style="font:7pt 'Times New Roman';">   </span></span></span>     <span style="font-size:9pt;">创建</span>     <span style="font-size:9pt;">ehcache.xml</span>     <span style="font-size:9pt;">,配置缓存再放入你的</span>     <span style="font-size:9pt;">classpath</span>     <span style="font-size:9pt;">路径下。</span>    </div>    <br />