Use NCache as Spring Data Cache

NCache, being an in-memory, extremely fast and scalable distributed cache provides integration with Spring to boost the application performance by reducing expensive data trips.

Spring is a lightweight dependency injection and aspect-oriented development container and framework for Java. It lets you simplify your application development and plug-in reusable pieces in your application like NCache.

Why Integrate your Spring Application with NCache?

Spring itself reduces the overall complexity of Java development by providing cohesion and loose coupling but it creates high traffic in these applications. These high traffic Spring applications face a major scalability problem which is why they need an in-memory distributed cache to meet their scalability requirements.

Although these applications can scale up by adding more servers to their server farm but their database cannot scale in the same way for handling such high loads. For these scenarios, a distributed cache is the best to handle the database scalability problem.

Therefore, the best option is NCache. It is an extremely fast, in-memory, key-value store distributed cache, which has implemented Spring caching module; making your Spring applications scalable and distributed. It offloads your database by reducing the expensive database trips that cause the scalability problem and provides faster performance.

How to Configure Spring Application to Use NCache

NCache provides Spring caching support in the following two ways:

  • Through Generic Spring Caching Provider, where NCache acts as a cache manager for your Spring application.
  • Through JCache Caching Provider, where NCache uses the supported feature of JCache.

Configure Spring Application through Generic Spring Caching Provider

For configuring your Spring application with NCache Generic Spring Caching Provider, first you need to add all the required Maven dependencies that Spring and NCache provides. After the addition of these dependencies, beans need to defined in your Spring Application.

Beans can be defined through two ways in your applications. One is through the Java based Bean Definition and other is by XML based Bean Definition.

  • Java based Bean Definition:

    For defining beans using Java based definition, you need to add the @Bean annotation in the CachingConfiguration class where the setConfigFile() method will specify the path to your ncache-spring.xml file. For detailed information, refer to Java based bean definition.

  • XML based Bean Definition:

    For defining beans using XML based definition, you need to add an xml file that enables caching and specifies NCache as a cache manager. Bean tags must be defined for both cacheManager and NCacheConfigurationManager. Following properties must be specified for cacheManager.

    • springConfigurationManager: Reference to NCache configuration manager bean.
    • logFilePath: Fully qualified path for cache manager logs.

    For details, go to XML based bean definition.

Also, caches must be configured in the ncache-spring.xml file which is used by the NCache Cache Manager. Here each cache needs to be defined with its own set of properties.

<cache name="demoCache" NCache-instance="mycache" priority="normal" expiration-type="absolute" expiration-period="10"/>

Configure Spring Applications through JCache Caching Provider

Spring also supports the JCache complaint caching providers where you can use JCache in your Spring application with NCache. First of all, Maven dependencies need to be added for Spring, NCache and JCache. After adding dependencies, caches must be configured.

Caches can be configured through the following ways:

  • Add spring.cache.cache-names tag in the file. Cache name should be the same as configured in NCache Web Manager.
  • Configure your cache through the JCacheManagerCustomizer class which initializes the cache at runtime with the desired configuration.

For more information, you can see our JCache Spring Docs.

Identifying Methods Needed to be Cached

Once you enabled caching through both the configurations defined above, the next step is to now bind these caching behaviors to their respective methods in order to use NCache as a Caching Provider for Spring.

You can bind caching behaviors to their methods through two ways. One is through Caching Annotations and the other is through Declarative XML based Caching.

If you do not use annotations, then Declarative XML based Caching can be used where you can specify the target method and caching directives externally.

What to Do Next?

Signup for monthly email newsletter to get latest updates.

© Copyright Alachisoft 2002 - . All rights reserved.