How to Cache Data for Better Performance

Teradata Employee

How to Cache Data for Better Performance

The following article describes how to implement data caching for better performance in your portlet.

A declarative caching framework is implemented in the tdcommons-utils jar. This framework is based on EhCache, Spring AOP, and JDK 1.5 annotations.

Quick Start

1. Add one of the following annotations to your business delegate methods (either in the interface or the implementation):

// This annotation will cache method return values based on method arguments.
@Cache(modelId = "cacheName")

// This annotation will flush the entire cache.
@CacheFlush(modelId = "cacheName")

// This annotation will delete a value from the cache based on the method arguments.
// Differs from @CacheRemove in that all the parameters are used.
@CacheDelete(modelId = "cacheName")

// This annotation will update the value in the cache based on all but the last method argument, the last argument
// is assumed to be the object that should be updated in the cache.
@CacheUpdate(modelId = "cacheName")

// This annotation will remove the value from the cache based on all but the last method argument
// (similar to @CacheUpdate). This annotation is useful when you want an update operation to clean
// the value from the cache so that it is not cached again until the updated value is obtained from
// the database.
@CacheRemove(modelId = "cacheName")

2. Add the following lines to your application context:

<bean id="cacheManager" class="com.teradata.commons.util.cache.ehcache.EhCacheManagerFactoryBean">
<property name="uniqueName" ref="uniqueName"/>
<property name="configLocation">

<bean id="cacheProvider" class="com.teradata.commons.util.cache.ehcache.EhCacheProvider">
<property name="cacheManager" ref="cacheManager" />

<bean id="cacheAutoProxy" class="com.teradata.commons.util.cache.CacheAdvisorAutoProxyCreator">
<property name="cacheProvider" ref="cacheProvider"/>

3. Create cache rules in /WEB-INF/ehcache.xml:


<!--Default Cache configuration. These will applied to caches programmatically created through
the CacheManager.

The following attributes are required for defaultCache:

maxInMemory - Sets the maximum number of objects that will be created in memory
eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
is never expired.
timeToIdleSeconds - Sets the time to idle for an element before it expires.
i.e. The maximum amount of time between accesses before an element expires
Is only used if the element is not eternal.
Optional attribute. A value of 0 means that an Element can idle for infinity
timeToLiveSeconds - Sets the time to live for an element before it expires.
i.e. The maximum time between creation time and when an element expires.
Is only used if the element is not eternal.
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
has reached the maxInMemory limit.


Mandatory Default Cache configuration. These settings will be applied to caches
created programmtically using CacheManager.add(String cacheName).

The defaultCache has an implicit name "default" which is a reserved cache name.
<defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120"
timeToLiveSeconds="120" overflowToDisk="true" maxElementsOnDisk="10000000" diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />

<cache name="cacheName" maxElementsInMemory="300" eternal="false" timeToIdleSeconds="500"
timeToLiveSeconds="500" overflowToDisk="true" />

Tags (3)