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.
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.
NCache provides Spring caching support in the following two ways:
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.
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.
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.
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"/>
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:
For more information, you can see our JCache Spring Docs.
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.