Spring is a lightweight, dependency injection and aspect-oriented development container and framework for Java. It reduces the overall complexity of J2EE development and provides high cohesion and loose coupling. Because of the benefits Spring provides, it is used by a lot of developers for creating high traffic small to enterprise level applications.
But these high traffic Spring applications face a major scalability problem. Although they can scale by adding more servers to the application server farm, their database server cannot scale in the same fashion to handle the growing transaction load. In such situations, a Java distributed cache is your best bet to handle as it offloads your database by reducing those expensive database trips and also improves your application performance.
NCache Details NCache Docs NCache Spring Caching Docs
Why Spring Application with NCache?
NCache, an in-memory, distributed cache has implemented the Spring Caching provider by introducing a generic Java cache mechanism with which you can easily cache the output of your CPU intensive, time consuming, and database bound methods of Spring application. This approach not only reduces the database load but also reduces the number of method executions and improves application performance. In this blog, I will be further discussing about how to configure Spring Caching with NCache.
NCache Details NCache as Spring Data Cache Configure Application Using NCache
How to Configure Spring Caching to Use NCache?
NCache provides Spring caching support through Generic Spring Caching Provider, where NCache acts as a cache manager for your Spring application and through JCache Caching Provider, where NCache uses the supported feature of JCache. Simply follow the steps given below for both the ways and configure NCache with your Spring application.
Generic Spring Caching Provider
For configuring your Spring application with NCache Generic Spring Caching Provider, first you need to add the required Maven dependency that Spring and NCache provides. Then, define beans in your Spring application either through Java Based Bean Definition or XML Based Bean Definition.
For defining beans using Java based definition, you need to add the @Bean
annotation in the CachingConfiguration
class where the setConfigFile()
method will specify the path to your ncache-spring.xml
file, shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
@Configuration @EnableCaching public class CachingConfiguration { @Bean public CacheManager cacheManager() { SpringConfigurationManager springConfigurationManager = new SpringConfigurationManager(); URL resource = getClass().getClassLoader().getResource("ncache-spring.xml"); springConfigurationManager.setConfigFile(resource.getPath()); NCacheCacheManager cacheManager = new NCacheCacheManager(); cacheManager.setSpringConfigurationManager(springConfigurationManager); return cacheManager; } } |
For defining beans using XML based definition, you need to add an xml file that enables caching and specifies NCache as a cache manager. Bean tags must be defined for both cacheManager
and NCacheConfigurationManager
. Also, springConfigurationManager
and logFilePath
properties must be specified for cacheManager. For more details on these properties, refer to our Spring Caching Docs.
NCache Cache Manager uses the ncache-spring.xml
file, where caches must be configured and defined with its own set of properties. Sample code is given below.
1 2 3 4 5 |
<application-config default-cache-name="default"> <caches> <cache name="CustomerCollectionCache" ncache-instance="demoCache" priority="normal" expiration-type="absolute" expiration-period="10"/> </caches> </application-config> |
JCache Spring Caching Provider
Spring also supports the JCache complaint caching providers where you can use JCache in your Spring application with NCache. Add the required Maven dependency for Spring Framework or Spring Boot and NCache for integration. Then configure your cache for JCache Spring application.
You can configure your cache with the further explained two ways. One is, you can add spring.cache.cache-names
tag in the application.properties
file. Cache name should be the same as configured in NCache Web Manager.
1 |
spring.cache.cache-names=demoCache,booksCache |
Other is, you can configure your cache through the JCacheManagerCustomizer
class which initializes the cache at runtime with the desired configuration.
1 2 3 4 5 6 7 8 9 10 11 |
@Configuration public class CacheConfiguration implements JCacheManagerCustomizer { @Override public void customize(CacheManager cacheManager) { MutableConfiguration mutableConfiguration = new MutableConfiguration(); mutableConfiguration.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.ONE_MINUTE)); cacheManager.createCache("booksCache", mutableConfiguration); } } |
Identify Methods Need to be Cached using Caching Declaration
Once you enabled caching through both the configurations defined above, the next step is to now bind these caching behaviors to their respective methods in order to use NCache as a Caching Provider for Spring.
You can bind caching behaviors to their methods through two ways. One is through Caching Annotations and the other is through Declarative XML based Caching. To get a detailed understanding, go to the Configure Caching Declaration Spring Docs.
Following sample code using @Cacheable annotation to perform caching on the web service containing cache booksCache.
1 2 3 4 5 6 7 8 9 |
// BookService.java @Service public class BookService { @Cacheable("booksCache") public String getBookNameByIsbn(String isbn) { return findBookInSlowSource(isbn); } // further code } |
NCache Details JCache API Docs Hibernate Caching
Conclusion!
By using NCache as a Spring Caching provider, you can scale your Spring applications linearly and with boosted performance. Just simply modify your configuration files and you are good to go! So, download NCache now and try it out for yourself!