refer to : http://static.springsource.org/spring/docs/3.1.0.M1/spring-framework-reference/html/cache.html
<cache:annotation-driven cache-manager="cacheManager" />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cache-manager-ref="ehcache" />
<!-- use ehcache here -->
<bean id="ehcache"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="classpath:ehcache.xml" />
2) ehcache.xml (refer to http://ehcache.org/documentation )
<ehcache>
<diskStore path="java.io.tmpdir" />
<cache name="userCache" maxElementsInMemory="100" eternal="false"
timeToIdleSeconds="60" timeToLiveSeconds="60" overflowToDisk="false"
maxElementsOnDisk="10000000" />
</ehcache>
it is configured to refresh the content of cache per 60 seconds
3) add annotation @Cacheable(value = "userCache") to enable cache
@Transactional
public interface AccountService {
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
@Cacheable(value = "userCache")
public List<Account> getAccounts();
@Cacheable(value = "userCache")
public List<Account> getAccounts(int currPageNo,int pageSize);
@CacheEvict (value = "userCache", allEntries=true)
public void addAccount(String userId,String password);
@CacheEvict (value = "userCache", allEntries=true)
public void addAccounts(List<Account> accounts) throws DataAccessException ;
@CacheEvict (value = "userCache", allEntries=true)
public void updateAccount(Account account);
@Cacheable(value = "userCache")
public Account getAccount(String userId);
@CacheEvict (value = "userCache", allEntries=true)
public void deleteAccount(String userId);
@CacheEvict (value = "userCache", allEntries=true)
public void updateAccount(String userId,int status);
}
4) add maven dependency
refer to : http://code.google.com/p/ehcache-spring-annotations/
<dependency> <groupId>com.googlecode.ehcache-spring-annotations</groupId> <artifactId>ehcache-spring-annotations</artifactId> <version>1.2.0</version> </dependency>
No comments:
Post a Comment