Configure Applications for Generic Spring Caching Provider
Spring is very well integrated with NCache and it is enough to provide NCache configuration on the classpath. Simply modify the configuration by following the detailed steps given below to enable NCache as a cache manager for your Spring application.
Pre-Requisites
- For NCache Client, minimum required Java version is 9.0.
- Version of Spring Framework should be 5.2.8.RELEASE or higher.
Note
- To use Maven packages for NCache Professional Edition, change the
<artifactId>
as shown below:<artifactId>ncache-professional-spring</artifactId>
.
Add Maven Packages
You need to add the following Maven <dependency>
in their pom.xml
file while working with NCache integration with Spring.
<dependency>
<groupId>com.alachisoft.ncache</groupId>
<artifactId>ncache-spring</artifactId>
<version>5.2.0</version>
</dependency>
Defining Configurations
Note
To enable caching in Spring application, add @EnableCaching annotation.
After adding the required Maven dependencies, you need to define beans in your Spring application. There are two ways to define beans in your Spring Application. One is using the Java based Bean Definition and the other is using XML based Bean Definition.
Java based Bean Definition
For defining beans using java based definition, add the @Bean annotation in your CacheConfiguration class like below.
Note
Use setConfigFile() method to specify the path of xml file where caches are configured.
@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;
}
}
Java based bean definition is a better approach to use than XML based as it is type-safe and it catches all the type errors at compile time.
XML based Bean Definition
Note
If logFilePath is not specified, then %NCHOME%/log-files/springCacheLogs path where NCache is installed, will be used. Otherwise, the path User Working Directory/springCacheLogs will be used.
For defining beans using XML based Definition, add an xml file in your application that defines beans for the application and use @ImportResource annotation to import the resource containing bean definitions in your main file. To ensure correct caching, following properties must also be specified for cacheManager.
- springConfigurationManager: Reference to NCache configuration manager bean.
- logFilePath: Fully qualified path for cache manager logs.
<bean id="cacheManager" class="com.alachisoft.integrations.spring.NCacheCacheManager">
<property name="springConfigurationManager" ref="NCacheConfigurationManager"/>
<property name="logFilePath" value="C:\log-files\CustomerDBSpringLogs"/>
</bean>
Also, add a <bean> tag with id as NCacheConfigurationManager
and specify the SpringConfigurationManager
class. Specify the fully qualified path of your configuration file ncache-spring.xml as configFile property of configuration manager.
Note
ncache-spring.xml is the XML file where caches are configured.
<bean id="NCacheConfigurationManager" class="com.alachisoft.integrations.spring.configuration.SpringConfigurationManager">
<property name="configFile" value="ncache-spring.xml">
</bean>
Configure Caches
NCache cache manager uses ncache-spring.xml file, provided in cacheManager bean to configure caches. To configure caches here, you need to add a cache with its own set of properties for example the expiration and eviction of items, some of which are explained below.
- default-cache-name: This lets you specify a default cache for Spring application. If the required cache configuration does not exist in configuration file, default cache will be used.
- caches: Multiple caches can be defined under this tag.
- ncache-instance: Each Spring cache is mapped on a specific cache instance. Multiple Spring caches can be configured to use the same cache instance. NCache cacheManager for Spring will keep record of each Spring's cache data in cache.
- priority: This specifies the relative priority of the items being added in the cache. The items with least priority are evicted first if priority based eviction is enabled.
- expiration-type: Allows any specific type of expiration to be done. Possible values are absolute or sliding.
<application-config default-cache-name="default">
<caches>
<cache name="CustomerCollectionCache" ncache-instance="demoCache" priority="normal" expiration-type="absolute" expiration-period="10"/>
<cache name="CustomerEntityCache" ncache-instance="demoCache" priority="normal" expiration-type="absolute" expiration-period="10"/>
</caches>
</application-config>
Once you have enabled caching, the next step is to bind the caching behavior to the methods, to use NCache as Caching Provider for Spring. For that refer to section Configure Applications using Caching Declaration.
See Also
Configure Application for JCache Caching Provider
NCache as Spring Data Cache
Error Logging