Configure JCache Spring Caching Provider with NCache
NCache provides a robust, JSR-107 compliant JCache Spring Caching Provider for both Spring Framework and Spring Boot applications. By integrating NCache as a backed JCache provider, Java developers can leverage its high-performance distributed caching within the standard Spring cache to improve application scalability and reduce database load. This guide demonstrates how to configure NCache as the primary JCache provider for your Spring application with minimal configuration changes, enabling standardized caching behavior across your entire ecosystem.
Prerequisites to Configure JCache Spring Cache Provider
- To learn about the supported Java versions, please refer to the NCache Installation Guide.
To configure the Spring Cache Provider ensure that following prerequisites are fulfilled.
- Add
spring-context-supportdependency for configuring Spring Framework. - Add
spring-boot-starter-cachedependency for configuring Spring Boot.
- Add
Step 1: Add NCache Maven Dependencies
The user needs to add the following Maven dependencies in their pom.xml file while configuring JCache Spring Cache provider.
<dependency>
<groupId>com.alachisoft.ncache</groupId>
<!--for NCache Enterprise-->
<artifactId>ncache-client</artifactId>
<version>x.x.x</version>
</dependency>
Step 2: Define JCache Provider Properties
Note
To enable caching in the Spring application, add the @EnableCaching annotation.
JCache is bootstrapped through the javax.cache.spi.CachingProvider, a JSR-107 compliant caching library on the classpath.
If you have more than one provider of JCache in your .classpath, then the tags spring.cache.jcache.provider and spring.cache.type needs to be explicitly added in the application.properties file.
spring.cache.jcache.provider=com.alachisoft.ncache.jsr107.spi.NCacheCachingProvider
spring.cache.type=jcache
Step 3: Initialize Spring Caches
Note
To initialize multiple caches on application startup, provide a spring.cache.cache-names tag with comma-separated cache names.
To configure your cache for the JCache Spring application and to initialize your cache at the application startup, you need to add the spring.cache.cache-names tag in the application.properties file. The cache name should be the same as configured in the NCache Management Center.
spring.cache.cache-names=demoCache,booksCache
Also, you can edit the JCacheManagerCustomizer class to configure the JCache spring cache and initialize it with those configurations at runtime.
@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);
}
}
Once you have enabled caching, bind the caching behavior to the methods to use NCache as a Caching Provider for Spring. To do so, refer to section Configure Applications using Caching Declaration.
See Also
Configure Application for Generic Spring Caching Provider
NCache as Spring Data Cache