OSCache是当前运用最广的缓存方案,JBoss,Hibernate,Spring等都对其有支持,
下面简单介绍一下OSCache的配置和使用过程。
1.安装过程
从[url]http://www.opensymphony.com/oscache/download.html[/url]下载合适的OSCache版本,我下载的是最新版本oscache-2.4.1-full.zip,然后解压缩到指定的目录。
把解压缩文件中的oscache-2.4.1.jar放入你的项目的/WEB-INF/lib中。
注:如果你的jdk版本为1.3.x,建议在lib中加入Apache Common Lib 的commons-collections.jar包。如jdk是1.4以上则不必 。
从src或etc目录取得oscache.properties 文件,放入你项目中的src根目录或发布环境的/WEB-INF/classes 目录下。
如你需要建立磁盘缓存,须修改oscache.properties 中的cache.path信息 (去掉前面的#注释)。
win类路径类似为c:\\app\\cache
unix类路径类似为/opt/myapp/cache
oscache.properties文件:
# CACHE IN MEMORY## If you want to disable memory caching, just uncomment this line.#cache.memory=true# CACHE KEY## This is the key that will be used to store the cache in the application# and session scope.## If you want to set the cache key to anything other than the default# uncomment this line and change the cache.key## cache.key=__oscache_cache# USE HOST DOMAIN NAME IN KEY## Servers for multiple host domains may wish to add host name info to# the generation of the key. If this is true, then uncomment the# following line.## cache.use.host.domain.in.key=true# CACHE LISTENERS## These hook OSCache events and perform various actions such as logging# cache hits and misses, or broadcasting to other cache instances across a cluster.# See the documentation for further information.## cache.event.listeners=com.opensymphony.oscache.plugins.clustersupport.JMSBroadcastingListener, \# com.opensymphony.oscache.extra.CacheEntryEventListenerImpl, \# com.opensymphony.oscache.extra.CacheMapAccessEventListenerImpl, \# com.opensymphony.oscache.extra.ScopeEventListenerImpl, \# com.opensymphony.oscache.extra.StatisticListenerImpl# CACHE PERSISTENCE CLASS## Specify the class to use for persistence. If you use the supplied DiskPersistenceListener,# don't forget to supply the cache.path property to specify the location of the cache# directory.# # If a persistence class is not specified, OSCache will use memory caching only.#cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.HashDiskPersistenceListener# cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.HashDiskPersistenceListener# CACHE OVERFLOW PERSISTENCE# Use persistent cache in overflow or not. The default value is false, which means# the persistent cache will be used at all times for every entry. true is the recommended setting.#cache.persistence.overflow.only=true# CACHE DIRECTORY## This is the directory on disk where caches will be stored by the DiskPersistenceListener.# it will be created if it doesn't already exist. Remember that OSCache must have# write permission to this directory.## Note: for Windows machines, this needs \ to be escaped# ie Windows:cache.path=d:\\myapp\\cache# or *ix:# cache.path=/opt/myapp/cache## cache.path=c:\\app\\cache# CACHE ALGORITHM## Default cache algorithm to use. Note that in order to use an algorithm# the cache size must also be specified. If the cache size is not specified,# the cache algorithm will be Unlimited cache.## cache.algorithm=com.opensymphony.oscache.base.algorithm.LRUCache# cache.algorithm=com.opensymphony.oscache.base.algorithm.FIFOCache# cache.algorithm=com.opensymphony.oscache.base.algorithm.UnlimitedCache# THREAD BLOCKING BEHAVIOR## When a request is made for a stale cache entry, it is possible that another thread is already# in the process of rebuilding that entry. This setting specifies how OSCache handles the# subsequent 'non-building' threads. The default behaviour (cache.blocking=false) is to serve# the old content to subsequent threads until the cache entry has been updated. This provides# the best performance (at the cost of serving slightly stale data). When blocking is enabled,# threads will instead block until the new cache entry is ready to be served. Once the new entry# is put in the cache the blocked threads will be restarted and given the new entry.# Note that even if blocking is disabled, when there is no stale data available to be served# threads will block until the data is added to the cache by the thread that is responsible# for building the data.## cache.blocking=false# CACHE SIZE## Default cache size in number of items. If a size is specified but not# an algorithm, the cache algorithm used will be LRUCache.#cache.capacity=5000# CACHE UNLIMITED DISK# Use unlimited disk cache or not. The default value is false, which means# the disk cache will be limited in size to the value specified by cache.capacity.## cache.unlimited.disk=false# JMS CLUSTER PROPERTIES## Configuration properties for JMS clustering. See the clustering documentation# for more information on these settings.##cache.cluster.jms.topic.factory=java:comp/env/jms/TopicConnectionFactory#cache.cluster.jms.topic.name=java:comp/env/jms/OSCacheTopic#cache.cluster.jms.node.name=node1# JAVAGROUPS CLUSTER PROPERTIES## Configuration properites for the JavaGroups clustering. Only one of these# should be specified. Default values (as shown below) will be used if niether# property is set. See the clustering documentation and the JavaGroups project# (www.javagroups.com) for more information on these settings.##cache.cluster.properties=UDP(mcast_addr=231.12.21.132;mcast_port=45566;ip_ttl=32;\#mcast_send_buf_size=150000;mcast_recv_buf_size=80000):\#PING(timeout=2000;num_initial_members=3):\#MERGE2(min_interval=5000;max_interval=10000):\#FD_SOCK:VERIFY_SUSPECT(timeout=1500):\#pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800;max_xmit_size=8192):\#UNICAST(timeout=300,600,1200,2400):\#pbcast.STABLE(desired_avg_gossip=20000):\#FRAG(frag_size=8096;down_thread=false;up_thread=false):\#pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;shun=false;print_local_addr=true)#cache.cluster.multicast.ip=231.12.21.132
拷贝OSCache标签库文件oscache.tld到/WEB-INF/classes目录。
现在你的应用目录类似如下:
$WEB_APPLICATION\WEB-INF\lib\oscache.jar
$WEB_APPLICATION\WEB-INF\classes\oscache.properties
$WEB_APPLICATION\WEB-INF\classes\oscache.tld
将下列代码加入web.xml文件中
<taglib> <taglib-uri>oscache</taglib-uri> <taglib-location>/WEB-INF/classes/oscache.tld</taglib-location> </taglib>
2.oscache.properties 文件配置向导
cache.memory
值为true 或 false ,默认为在内存中作缓存,
如设置为false,那cache只能缓存到数据库或硬盘中,那cache还有什么意义:)
cache.capacity
缓存元素个数
cache.persistence.class
持久化缓存类,如此类打开,则必须设置cache.path信息
cache.cluster 相关
为集群设置信息。
如
cache.cluster.multicast.ip为广播IP地址
cache.cluster.properties为集群属性
3.OSCache的基本用法
cache1.jsp 内容如下
<%@ page import="java.util.*" %> <%@ taglib uri="oscache" prefix="cache" %> <html> <body> 没有缓存的日期: <%= new Date() %><p> <!--自动刷新--> <cache:cache time="30"> 每30秒刷新缓存一次的日期: <%= new Date() %> </cache:cache> <!--手动刷新--> <cache:cache key="testcache"> 手动刷新缓存的日期: <%= new Date() %> <p> </cache:cache> <a href="cache2.jsp">手动刷新</a> </body> </html>
cache2.jsp 执行手动刷新页面如下
<%@ taglib uri="oscache" prefix="cache" %> <html> <body> 缓存已刷新...<p> <cache:flush key="testcache" scope="application"/> <a href="cache1.jsp">返回</a> </body> </html> 你也可以通过下面语句定义Cache的有效范围,如不定义scope,scope默认为Applcation <cache:cache time="30" scope="session"> ... </cache:cache>
4. 缓存过滤器 CacheFilter
你可以在web.xml中定义缓存过滤器,定义特定资源的缓存。
<filter> <filter-name>CacheFilter</filter-name> <filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class> <init-param> <param-name>time</param-name> <param-value>60</param-value> </init-param> <init-param> <param-name>scope</param-name> <param-value>session</param-value> </init-param> </filter> <filter-mapping> <filter-name>CacheFilter</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping>
注:上面定义将缓存所有.jsp页面,缓存刷新时间为60秒,缓存作用域为Session
或者在web.xml先设置页面级缓存处理
<!--页面级缓存处理 --> <filter> <filter-name>CacheFilter</filter-name> <filter-class> com.opensymphony.oscache.web.filter.CacheFilter </filter-class> <init-param> <param-name>time</param-name> <param-value>1200</param-value> </init-param> <init-param> <param-name>disableCacheOnMethods</param-name> <param-value>POST,PUT,DELETE</param-value> </init-param> <init-param> <param-name>oscache-properties-file</param-name> <param-value> /WEB-INF/classes/oscache.properties </param-value> </init-param> </filter>
然后再在web.xml中设置要缓存的页面
<!-- 设置oscache --> <filter-mapping> <filter-name>CacheFilter</filter-name> <!-- <url-pattern>/manager/cust/syscs.action</url-pattern> <url-pattern>/tool_bar/*</url-pattern> <url-pattern>/common/css/*</url-pattern> --> <!-- <url-pattern>/common/js/*</url-pattern> <url-pattern>/news/css/*</url-pattern> <url-pattern>/news/js/*</url-pattern> <url-pattern>/dwr/util.js</url-pattern> --> <url-pattern>/dwrdsfsdf/util.js</url-pattern> </filter-mapping>
注意:CacheFilter只捕获Http头为200的页面请求,即只对无错误请求作缓存,
而不对其他请求(如500,404,400)作缓存处理。
相关资源:oscache的使用实例和详解