• Facebook
  • Twitter
  • Youtube
  • LinedIn
  • RSS
  • Docs
  • Comparisons
  • Blogs
  • Download
  • Contact Us
Download
Show / Hide Table of Contents

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-support dependency for configuring Spring Framework.
    • Add spring-boot-starter-cache dependency for configuring Spring Boot.

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

Contact Us

PHONE

+1 214-619-2601   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • 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 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.
Back to top