Use NCache as Spring Data Cache

NCache, being an in-memory, extremely fast and scalable distributed cache provides integration with Spring framework to let you store your POJO in the cache and boost the application performance.

Spring is a lightweight dependency injection and aspect-oriented development container and framework for Java. It lets you simplify your application development and plug-in reusable pieces in your application like NCache.

Why Integrate your Spring Application with NCache?

Spring itself reduces the overall complexity of Java development by providing cohesion and loose coupling but it creates high traffic in these applications. These high traffic Spring applications face a major scalability problem which is why they need an in-memory distributed cache to meet their scalability requirements.

Although these applications can scale up by adding more servers to their server farm but their database cannot scale in the same way for handling such high loads. For these scenarios, a distributed cache is the best to handle the database scalability problem.

Therefore, the best available option is NCache. It is an extremely fast, in-memory, key-value store distributed cache which has implemented Spring caching module; making your Spring applications scalable and distributed. It offloads your database by reducing the expensive database trips that cause the scalability problem and provides better performance.

How to Configure Spring Application to Use NCache NCache, an in-memory distributed cache, has implemented the Spring caching module by giving such a caching mechanism with which you can easily cache the output of your database bound methods of the Spring application.

Spring application can be configured with NCache in the following two ways:

  • By using configurations only, there is no code change required because this method uses the aspect-oriented programming feature of Spring to add caching as an aspect in Spring application.
  • By using caching annotations defined in the Spring framework, you can add them before definitions of functions to be cached.

Now let us slowly sink into the detail of these configurations.

Configure Spring Application through Configuration Files

In the applicationContext.xml file of the Spring application, enable caching and specify NCache as a cache manager by adding the ones written below.

Add a <bean> with id as cacheManager and specify NCache cache manager class. Following properties must also be specified for cacheManager:

  • NCacheConfigurationManager: Reference to NCache configuration manager bean.
  • logFilePath: Fully qualified path for cache manager logs.
<bean id= "cacheManager" class="com.alachisoft.integrations.spring.CacheManagerWrapper">
    <property name="NCacheConfigurationManager" ref="NCacheConfigurationManager"/>
    <property name="logFilePath" ref="C:\CustomerDBSpringLogs"/>
</bean>

Next, add a <bean> with id as NCacheConfigurationManager and specify the SpringConfigurationManager class. Also specify the fully qualified path of your configuration file NCache-spring.xml as configFile property of configuration manager.

<bean id= "NCacheConfigurationManager" class="com.alachisoft.integrations.spring.configuration.SpringConfigurationManager">
    <property name="configFile" ref="...path of xml file"/>
</bean>

If you are using custom key and entity generator for cache key and entity generation, then beans for key generator and entity generator must be defined also like below.

<bean id="collectionKeyGenerator" class="cachekeygenerator.NCacheCollectionKeyGenerator"/>
<bean id="entityKeyGenerator" class="cachekeygenerator.NCacheEntityKeyGenerator"/>

Add cache advice and specify caching properties and methods separately for both key and entity that need to be cached like below.

//for key generator
<cache:advice id="collectionCacheAdvice" key-generator="collectionKeyGenerator">
    <cache:caching cache="CustomerCollectionCache">
        <cache:cache-evict all-entries="true" method="create"/>
        <cache:cacheable  method="findAll"/>
    </cache:caching>
</cache:advice>

//for entity generator
<cache:advice id="entityCacheAdvice" key-generator="entityKeyGenerator">
    <cache:caching cache="CustomerEntityCache">
        <cache:cache-evict method="create"/>
        <cache:cacheable method="find"/>
    </cache:caching>
</cache:advice>

Also, NCache cache manager uses NCcache-spring.xml file, provided in the cacheManager bean to configure the caches. Here each cache needs to be defined with its own set of properties.

<cache name="demoCache" NCache-instance="mycache" priority="normal" expiration-type="absolute" expiration-period="10"/>

Configure Spring Applications through Caching Annotations

For configuring applications through annotation-based caching, in the applicationContext.xml file the tag <cache:annotation-driven> must be added. After this, you can add different caching annotations on the functions of your choice.

NCache has implemented a set of annotations to be integrated with Spring some of which are explained below:

  • @Cacheable: We mark the method required to be cached along with its properties.
  • @CacheEvict: Indicates the removal of objects so that fresh ones can be loaded again in cache.
  • @POST: Set the value of the object to be cached.
  • @PUT: Update the value of the object to be cached.
  • @GET: Retrieve the value of the object to be cached

What to Do Next?

Signup for monthly email newsletter to get latest updates.

Contact Us

PHONE

+1 (214) 764-6933 (US)

+44 20 7993 8327 (UK)

© Copyright Alachisoft 2002 - . All rights reserved.