Configure Generic Spring Caching Provider
Spring is very well integrated with NCache and it provides NCache configurations on the classpath. Modify the configuration by following the detailed steps given below to enable NCache as a Cache Manager for your Spring application.
Prerequisites
- For the NCache Client, the minimum required Java version is 9.0.
- The Spring Framework version should be 5.2.8.RELEASE or higher.
Note
- To use Maven packages for the 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 the NCache Spring integration.
<dependency>
<groupId>com.alachisoft.ncache</groupId>
<artifactId>ncache-spring</artifactId>
<version>x.x.x</version>
</dependency>
Defining Configurations
Note
To enable caching in Spring application, add @EnableCaching annotation.
After adding 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 the XML-based Bean Definition.
Java based Bean Definition
For defining beans using the Java-based definition, add the @Bean annotation in your CacheConfiguration class as below.
Note
Use the setConfigFile() method to specify the path of the 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;
}
}
The Java-based bean definition is a better approach than the XML-based definition as it is type-safe and it catches all type errors at compile time.
XML based Bean Definition
Note
If the logFilePath is not specified, then the %NCHOME%/log-files/springCacheLogs
path where NCache is installed, will be used. Otherwise, the User Working Directory/springCacheLogs
path will be used.
For defining beans using the XML-based definition, add an xml file in your application that defines beans for the application and use the @ImportResource annotation to import the resource containing bean definitions
in your main file. To ensure correct caching, the following properties must also be specified for the 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 the id as the NCacheConfigurationManager
and specify the SpringConfigurationManager
class. Specify the fully qualified path of your configuration file ncache-spring.xml as a configFile property of the 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
The NCache cache manager uses the ncache-spring.xml file, provided in the cacheManager bean to configure caches. To configure caches, 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 the configuration file, the 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 a 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 the section on Configure Applications using Caching Declaration.
See Also
Configure Application for JCache Caching Provider
NCache as Spring Data Cache
Error Logging