首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

S2SH中运用EhCache实现页面缓存

2012-12-28 
S2SH中使用EhCache实现页面缓存使用S2SH开发网站,网站首页需要展示的数据多,访问量大。如果不做处理,则频繁

S2SH中使用EhCache实现页面缓存

使用S2SH开发网站,网站首页需要展示的数据多,访问量大。如果不做处理,则频繁的查询数据库,结果是页面显示的慢,服务器、数据库不堪重负。如果网站页面所展示的数据的更新不是特别频繁,想提高页面显示的速度,减轻服务器的负担,此时应该考虑使用缓存。

就S2Sh来讲,做缓存有两种方式:

1,启用Hibernate的二级缓存。

2,使用页面缓存。

使用缓存有一个原则:越高层次的缓存效果越好。 推荐使用页面缓存。

?

下面示例如何使用EhCache完成Struts2下的页面缓存。

1,下载EhCache web版 ?http://ehcache.org/downloads/catalog? 注意下载web版。可见附件

2,解压 将ehcache-web-2.0.3.jar 、ehcache-core-2.3.0.jar ?拷入lib中。

3,在src下建立ehcache.xml?

4,开始配置 web.xml ,注意此filter配置应该放在Struts2核心filter的前面。

<!-- 页面缓存配置 ehcache --><filter><filter-name>SimplePageFragmentCachingFilter</filter-name><filter-class>net.sf.ehcache.constructs.web.filter.SimplePageFragmentCachingFilter</filter-class><init-param><param-name>suppressStackTrace</param-name><param-value>false</param-value></init-param><init-param><param-name>cacheName</param-name><param-value>SimplePageFragmentCachingFilter</param-value></init-param></filter><filter-mapping><filter-name>SimplePageFragmentCachingFilter</filter-name><url-pattern>/index.action</url-pattern></filter-mapping>

?5,配置ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:noNamespaceSchemaLocation="ehcache.xsd"         updateCheck="true" monitoring="autodetect"         dynamicConfig="true">         <diskStore path="java.io.tmpdir"/>         <cache name="SimplePageFragmentCachingFilter"  maxElementsInMemory="10"  eternal="false"  timeToIdleSeconds="10000"  timeToLiveSeconds="10000"  overflowToDisk="true"></cache></ehcache>

?

重启服务器,运行页面,第一次访问查询了数据库,此时EhCache将页面的内容缓存,下一次访问的时候,则直接使用缓存中的数据,而不必去访问数据库。一直到缓存过期,才会重新查询数据库。

热点排行