oscache的使用

1.从http://www.opensymphony.com/oscache/download.html下载合适的OSCache版本.就现在最新版本oscache-2.4.1-full.zip.
解压缩下载的文件到指定目录
2. 将oscache-2.4.1.jar,commons-logging.jar复制到/WEB-INF/lib,将oscache.properties,oscache.tld拷到src目录下
3.在web.xml加上oscache.tld的配置:
<jsp-config>
<taglib>
    <taglib-uri>oscacheTag</taglib-uri>
    <taglib-location>/WEB-INF/class/oscache.tld</taglib-location>
</taglib>
</jsp-config>

4.如果需要对特定的页面进行过滤,使用oscache过滤器,在web.xml中添加
<filter>
<filter-name>CacheFilter</filter-name>
<filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CacheFilter</filter-name>
<!--对所有jsp页面内容进行缓存-->
<url-pattern>*.jsp</url-pattern>
</filter-mapping>

5.在jsp页面中引用oscache标签
<%@ taglib  uri="oscacheTag" prefix="oscache" %>
也可以使用如下方式进行引用:
<%@ taglib  uri="/WEB-INF/classes/oscache.tld" prefix="oscache" %>
在jsp页面中使用oscache标签:
<oscache:cache>
<%
out.print("这是在缓存内容");
%>
</oscache:cache>
6.oscache.properties的配置说明:
cache.memory
Valid values are true or false, with true being the default value. If you want to disable memory caching, just comment out or remove this line.
Note: disabling memory AND disk caching is possible but fairly stupid

cache.capacity
The maximum number of items that a cache will hold. By default the capacity is unlimited - the cache will never remove any items. Negative values will also be treated as meaning unlimited capacity.If a size is specified but not an algorithm, the cache algorithm used will be LRUCache.

cache.algorithm
The 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 regardless of the value of this property. If you specify a size but not an algorithm, the cache algorithm used will be com.opensymphony.oscache.base.algorithm.LRUCache.

OSCache currently comes with three algorithms:

com.opensymphony.oscache.base.algorithm.LRUCache - Least Recently Used. This is the default when a cache.capacity is set.
com.opensymphony.oscache.base.algorithm.FIFOCache - First In First Out.
com.opensymphony.oscache.base.algorithm.UnlimitedCache - Content that is added to the cache will never be discarded. This is the default when no value is set for the cache.capacity property.
cache.blocking
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.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.persistence.class
Specifies the class to use for persisting cache entries. This class must implement the PersistenceListener interface. OSCache comes with an implementation that provides filesystem based persistence. Set this property to com.opensymphony.oscache.plugins.diskpersistence.HashDiskPersistenceListener to enable this implementation. By specifying your own class here you should be able to persist cache data using say JDBC or LDAP. NOTE: This class hashes the toString() of the object being cached to produce the file name of the entry. If you prefer readable file names, the parent DiskPersistenceListener can still be used but it will have issues with illegal filesystem characters or long names.

Note
The HashDiskPersistenceListener and DiskPersistenceListener classes require cache.path to be set in order to know where to persist the files to disk.

cache.path
This specifies the directory on disk where the caches will be stored. The directory will be created if it doesn't already exist, but remember that OSCache must have permission to write to this location. Avoid sharing the same cache path between different caches, because OSCache has not been designed to handle this.


Note
For Windows machines, the backslash character '\' needs to be escaped. ie in Windows:
    cache.path=c:\\myapp\\cache
    or *ix:
    cache.path=/opt/myapp/cache

cache.persistence.overflow.only
Indicates whether the persistence should only happen once the memory cache capacity has been reached. The default value is false for backwards compatibility but the recommended value is true when the memory cache is enabled. This property drastically changes the behavior of the cache in that the persisted cache will now be different then what is in memory.

cache.event.listeners
This takes a comma-delimited list of fully-qualified class names. Each class in the list must implement one (or more) of the following interfaces:

CacheEntryEventListener - Receives cache add/update/flush and remove events.
CacheMapAccessEventListener - Receives cache access events. This allows you to keep statistical information to track how effectively the cache is working.
No listeners are configured by default, however some ship with OSCache that you may wish to enable:

com.opensymphony.oscache.plugins.clustersupport.BroadcastingCacheEventListener - provides clustering support for OSCache. Enabling this will cause cache flush events to be broadcast to other instances of OSCache running on your LAN. See Clustering OSCache for further information about this event listener.
com.opensymphony.oscache.extra.CacheEntryEventListenerImpl - a simple listener implementation that maintains a running count of all of the entry events that occur during a cache's lifetime.
com.opensymphony.oscache.extra.CacheMapAccessEventListenerImpl - a simple listener implementation that keeps count of all the cache map events (cache hits and misses, and stale hits) that occur on a cache instance.
It is also of course quite straightforward to write your own event listener. See the JavaDoc API for further details and Statistics for an example.

cache.key
This is the key that will be used by the ServletCacheAdministrator (and hence the custom tags) to store the cache object in the application and session scope. The default value when this property is not specified is "__oscache_cache". If you want to access this default value in your code, it is available as com.opensymphony.oscache.web.ServletCacheAdministrator.DEFAULT_CACHE_KEY.

cache.use.host.domain.in.key
If your server is configured with multiple hosts, you may wish to add host name information to automatically generated cache keys. If so, set this property to true. The default value is false.

Additional Properties
In additon to the above basic options, any other properties that are specified in this file will still be loaded and can be made available to your event handlers. For example, the JavaGroupsBroadcastingListener supports the following additional properties:

cache.cluster.multicast.ip
The multicast IP to use for this cache cluster. Defaults to 231.12.21.132.

cache.cluster.properties
Specifies additional configuration options for the clustering. The default setting is

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)

 

你可能感兴趣的:(OScache)