Try Playground
Show / Hide Table of Contents

Configure Applications for Generic Spring Cache Provider

You can enable NCache as a Spring cache manager for your application by following the steps below.

Prerequisites to Configure Applications for Generic Spring Cache Provider

  • To learn about the supported Java versions, please refer to the NCache Installation Guide.
  • The Spring Framework version should be 6.0.12 or higher.

Add Maven Packages

You must add the following Maven <dependency> in your 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>
Note

To use Maven packages for the NCache Professional, change the <artifactId> as shown below:
<artifactId>ncache-professional-spring</artifactId>.

Defining Configurations

Note

To enable caching in the Spring application, add @EnableCaching annotation.

After adding the required Maven dependencies, you must define beans in your Spring application using either the Java or 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.

@Configuration
class CachingConfiguration {
    @Bean
    public CacheManager cacheManager() {
        String resource = Path.of(System.getenv("NCHOME"), "config/ncache-spring.xml").toString();

        SpringConfigurationManager springConfigurationManager = new SpringConfigurationManager();
        springConfigurationManager.setConfigFile(resource);

        NCacheCacheManager cacheManager = new NCacheCacheManager();
        cacheManager.setSpringConfigurationManager(springConfigurationManager);

        return cacheManager;
    }
}
Note

Use the setConfigFile method to specify the path of the xml file where caches are configured.

The Java-based Bean definition is a better approach than the XML-based definition as it is type-safe and catches all type errors at compile time.

XML-based Bean Definition

To define beans using the XML-based definition, add a .xml file in your application that defines beans for the application and use the @ImportResource annotation to import the relevant resource in your main file. Further, to ensure correct caching, specify the following properties for the cacheManager.

  • springConfigurationManager: Reference to the 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:\Program Files\NCache\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>
Note

If the logFilePath is not specified, and NCache has been installed, then the NCHOME/log-files/springCacheLogs path will be used by default by the application. Otherwise, it uses the User Working Directory/springCacheLogs path.

Configure Spring Caches

The NCache Management Center employs the ncache-spring.xml file provided in the cacheManager bean to configure caches. To configure caches, you must add a cache with its own properties (for example, the expiration and eviction of items).

  • default-cache-name: This lets you specify a default cache for the Spring application. If the required cache configuration is missing in the configuration file, it will use the default cache.
  • caches: Multiple caches can be defined under this tag.
  • ncache-instance: Each Spring cache maps on a specific cache instance. Multiple Spring caches can use the same cache instance. The NCache cacheManager for Spring records each Spring cache's data in the cache.
  • priority: This specifies the relative priority of the items added to the cache. If priority-based eviction is enabled, the cache evicts the items with the least priority first.
  • expiration-type: Enables Absolute or Sliding expiration.
  • expiration-period: Specifies the time (in seconds) after which the cache triggers expiration.
<application-config default-cache-name="demoCache">
    <caches>
        <cache name="demoCache" ncacheid-instance="democache" priority="normal" expiration-type="absolute" expiration-period="300"/>
    </caches>
</application-config>

Once you have enabled caching, the next step is to bind the caching behavior to the methods and to use NCache as a Caching Provider for Spring. To do so, refer to the section on Configure Applications using Caching Declaration.

See Also

NCache as Spring Data Cache
Error Logging

In This Article
  • Prerequisites to Configure Applications for Generic Spring Cache Provider
  • Add Maven Packages
  • Defining Configurations
    • Java-based Bean Definition
    • XML-based Bean Definition
  • Configure Spring Caches
  • See Also

Contact Us

PHONE

+1 (214) 764-6933   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • NCache Enterprise
  • NCache Professional
  • Edition Comparison
  • NCache Architecture
  • Benchmarks
Download
Pricing
Try Playground

Deployments
  • Cloud (SaaS & Software)
  • On-Premises
  • Kubernetes
  • Docker
Technical Use Cases
  • ASP.NET Sessions
  • ASP.NET Core Sessions
  • Pub/Sub Messaging
  • Real-Time ASP.NET SignalR
  • Internet of Things (IoT)
  • NoSQL Database
  • Stream Processing
  • Microservices
Resources
  • Magazine Articles
  • Third-Party Articles
  • Articles
  • Videos
  • Whitepapers
  • Shows
  • Talks
  • Blogs
  • Docs
Customer Case Studies
  • Testimonials
  • Customers
Support
  • Schedule a Demo
  • Forum (Google Groups)
  • Tips
Company
  • Leadership
  • Partners
  • News
  • Events
  • Careers
Contact Us

  • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

  • Contact Us
  •  
  • Sitemap
  •  
  • Terms of Use
  •  
  • Privacy Policy
© Copyright Alachisoft 2002 - 2025. All rights reserved. NCache is a registered trademark of Diyatech Corp.
Back to top