Spring Modules OSCache

Spring Modules and its caching module

Spring Modules is a collection of tools, add-ons and modules to extend the Spring Framework. The core goal of Spring Modules is to facilitate integration between Spring and other projects without cluttering or expanding the Spring core.Spring Modules is a collection of tools, add-ons and modules to extend the Spring Framework. The core goal of Spring Modules is to facilitate integration between Spring and other projects without cluttering or expanding the Spring core. The Caching Module provides a consistent abstraction for performing caching, delivering the following benefits.

  • Provides a consistent programming model across different caching APIs such as EHCache, JBoss Cache, Java Caching System (JCS) and OSCache.
  • Provides a unified, simpler, easier to use, API for programmatic use of caching services than most of these previously mentioned APIs.
  • Supports different strategies for declarative caching services.
  • The Caching Module may be easily extended to support additional cache providers.

 

Open Symphony  OSCache

OSCache is a caching solution that includes a JSP tag library and set of classes to perform fine grained dynamic caching of JSP content, servlet responses or arbitrary objects. It provides both in memory and persistent on disk caches, and can allow your site to have graceful error tolerance (eg if an error occurs like your db goes down, you can serve the cached content so people can still surf the site almost without knowing).

 

Implementation

The magic behind OSCache is it intercepts all the calls to the targeted interface. (ie, the PublicationDao) If any methods that being specified in the cachingModels list (ie. Key=”get*” means any methods that have a name start with “get”), A caching model will be applied to it, if there is no existing one applied. You can set up your caching model by configuring its length of the caching period (refreshPeriod), group and cronExpression.

 

 <bean id="publicationDaoTarget" class="com.hongliang.PublicationDaoImpl" scope="singleton">

        <property name="sessionFactory" ref="sessionFactory"/>

  </bean>

 

<bean id="publicationDao"

   class="org.springmodules.cache.interceptor.proxy.CacheProxyFactoryBean">

<!– Sets the cache provider facade for the interceptors –>

<property name="cacheProviderFacade" ref="cacheProviderFacade"/>

    <property name="cachingModels">

        <map>

            <entry key="get*">

                <bean class="org.springmodules.cache.provider.oscache.OsCacheCachingModel">

                    <property name="refreshPeriod" value="5000"/>

                    <property name="groups" value="LONG_CACHE"/>

                </bean>

            </entry>

        </map>

    </property>

<property name="proxyInterfaces" value="com.hongliang.PublicationDao"/>

    <property name="target" ref="publicationDaoTarget"/>

</bean>

 

cacheProviderFacade is used to set the cache provider facade for the interceptors. 

 

<!– Singleton FactoryBean that constructs and exposes a OSCache à

<bean id="cacheManager"

class="org.springmodules.cache.provider.oscache.OsCacheManagerFactoryBean">

         <property name="configLocation" value="classpath:oscache.properties" />

</bean>

 

<bean id="cacheProviderFacade"

class="org.springmodules.cache.provider.oscache.OsCacheFacade">

        <property name="cacheManager" ref="cacheManager"/>

        <property name="failQuietlyEnabled" value="false"/>

    </bean>

If no config location is specified, a GeneralCacheAdministrator will be configured from "oscache.properties" in the root of the class path. Here is an example of oscache.properties

 

# oscache.properties file being introduced to counter the memory issues on tomcat 4

# setting cache.memory to false if the memory cache continues to exhaust the application’s memory

cache.memory=true

# setting to 10000

cache.capacity=10000

# LRUCache is the default where cache capacity is set

cache.algorithm=com.opensymphony.oscache.base.algorithm.LRUCache

# no need to limit the amount of cache on disk.

cache.unlimited.disk=false

# 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.

# note surely this is irrelevant when cache.memory = true

cache.persistence.overflow.only=true

 

Spring Modules for Maven

There is one more thing needs to note here for Nexus users, as the Nexus may block your download for the dependency files automatically. Since all those dependencies are optional, you can have them excluded by declaring within your POM file.

 

<dependency>

            <groupId>org.springmodules</groupId>

              <artifactId>spring-modules-cache</artifactId>

              <version>0.8a</version>

            <exclusions>

                <exclusion>

                    <groupId>commons-logging</groupId>

                    <artifactId>commons-logging</artifactId>

                </exclusion>

                  ……..

                  ……..

  </exclusions>

</dependency>     

 

Reference:

https://springmodules.dev.java.net/docs/reference/0.8/html/cache.html

 

Advertisements
This entry was posted in Spring. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s