This guide only covers the configuration of OSCache by using the oscache.properties file. To see how to install OSCache and where to place the oscache.properties file, see the Installation Guide.
The following properties are able to be set in the oscache.properties file:

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.

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

Indicates whether the disk cache should be treated as unlimited or not. The default value is false. In this case, the disk cache capacity will be equal to the memory cache capacity set 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 (NEW! Since 2.1)

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)

See the Clustering OSCache documentation for further details on the above two properties.