Configure JCache Spring Cache Provider
Spring 6.0.12 supports JCache-compliant caching providers. You can also use JCache caching backed by NCache if your Spring version supports JCache. Simply modify the configuration by following the steps below and use Spring seamlessly with the NCache JCache Spring Caching Provider.
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-support
dependency for configuring Spring Framework. - Add
spring-boot-starter-cache
dependency for configuring Spring Boot.
- Add
Add Maven Packages
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>
<!--for NCache Professional-->
<!--artifactId>ncache-professional-client</artifactId-->
<version>x.x.x</version>
</dependency>
Note
To use Maven packages for the NCache Professional, change the <artifactId>
as <artifactId>ncache-professional-client</artifactId>
.
Defining Cache Configurations
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
Configure 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