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.
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:
Now let us slowly sink into the detail of these configurations.
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:
<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"/>
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: