DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. Spring cache abstraction

Spring cache abstraction

Ashish Sarin user avatar by
Ashish Sarin
·
Feb. 07, 13 · Interview
Like (0)
Save
Tweet
Share
7.00K Views

Join the DZone community and get the full member experience.

Join For Free

This article is taken from the book  Getting started with Spring Framework 

If you want to use caching in your application, you can consider using Spring’s cache abstraction. Spring’s cache abstraction shields developers from directly dealing with the underlying caching implementation’s API. As of Spring 3.2, cache abstraction is available out-of-the-box for java.util.concurrent.ConcurrentMap, Ehcache and for caching solutions that implement JSR 107 – Java Temporary Caching API (referred to as JCACHE).

Spring provides a CacheManager interface that defines methods for managing a collection of Cache instances. A Cache instance is a wrapper around the underlying cache, and it provides methods for interacting with the underlying cache.

SimpleCacheManager (an implementation of CacheManager) is useful for simple caching scenarios and for testing purposes. For instance, if you want to use java.util.concurrent.ConcurrentMap as the underlying cache store, you can use SimpleCacheManager to manage the cache.

Let’s now look at how a CacheManager is configured in the application context XML file.

Configuring a CacheManager

If in an application a collection of java.util.concurrent.ConcurrentMap instances are used as the underlying cache store, then the SimpleCacheManager is used to manage the cache.

The following example listing shows how a SimpleCacheManager instance is configured:

<bean id="myCacheManager" class="org.springframework.cache.support.SimpleCacheManager">
    <property name="caches">
        <set>
            <bean
                class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
                <property name="name" value="fixedDepositList" />
            </bean>
            <bean
                class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
                <property name="name" value="fixedDeposit" />
            </bean>
        </set>
    </property>
</bean>
In the above example listing, the fixedDepositList and fixedDeposit caches are managed by the SimpleCacheManager instance.

Let's now look at different Spring annotations that are used for caching purposes.

 

Caching annotations - @Cacheable, @CacheEvict and @CachePut

To use caching annotations, you need to configure <annotation-driven> element of Spring’s cache schema, as shown here:

<beans . xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="..... http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
    <cache:annotation-driven cache-manager="myCacheManager" />
    .....
</beans>
In the above example listing, Spring’s cache schema is included so that its elements are accessible in the application context XML file. The <annotation-driven> element’s cache-manager attribute refers to the CacheManager bean that is used for managing the cache.

@Cacheable annotation on a method indicates that the value returned by the method is cached. The following example listing shows the usage of @Cacheable annotation to cache the value returned by FixedDepositService’s findFixedDepositsByBankAccount method:

package sample.spring.chapter07.springbankapp.service; 
import org.springframework.cache.annotation.Cacheable; 
..... 
@Service(value = "fixedDepositService") 
public class FixedDepositServiceImpl implements FixedDepositService { 
    ..... 
    @Cacheable(value = { "fixedDepositList" })
    public List<FixedDepositDetails> findFixedDepositsByBankAccount(int bankAccountId) { 
        logger.info("findFixedDepositsByBankAccount method invoked"); 
        return myFixedDepositDao.findFixedDepositsByBankAccount(bankAccountId); 
    } 
}

In the above example listing, the @Cacheable annotation specifies that the value returned by the findFixedDepositsByBankAccount method is stored in the fixedDepositList cache. It is important to note that @Cacheable annotated method is not invoked if the same set of argument values are passed to the method.

If you want to evict data from the cache when a method is called, annotate the method with the @CacheEvict annotation. The following example listing shows usage of @CacheEvict annotation:

package sample.spring.chapter07.springbankapp.service; 
import org.springframework.cache.annotation.CacheEvict; 
..... 
@Service(value = "fixedDepositService") 
public class FixedDepositServiceImpl implements FixedDepositService { 
    ..... 
    @CacheEvict(value = { "fixedDepositList" }, allEntries=true, beforeInvocation = true) 
    public void createFixedDeposit(final FixedDepositDetails fdd) throws Exception { 
        ..... 
    } 
    ..... 
}

The @CacheEvict annotation on the createFixedDeposit method instructs Spring to remove all the cached entries from the cache region named fixedDepositList. The value attribute specifies the cache region from which to evict the cached item, and allEntries attribute specifies whether or not all entries from the specified cache region are evicted.

Spring also provides a @CachePut annotation that indicates that a method is always invoked, and the value returned by the method is put into the cache.

Download samples.zip from here and refer to ch07-springbankapp project that shows the usage of Spring's cache abstraction.

Cache (computing) Spring Framework Abstraction (computer science)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Building a Scalable Search Architecture
  • Microservices Discovery With Eureka
  • How Do the Docker Client and Docker Servers Work?
  • How to Quickly Build an Audio Editor With UI

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: