Infinispan Cache Solution | Spring Cache | Spring Boot

Infinispan cache is a distributed in-memory key/value data store, it is a excellent cache for software system.The tutorial will guide you how to start with Infinispan by Spring Boot for making a caching solution.

Related Articles:
How to start Embedded Gemfire Application with SpringBoot
How to create a SpringBoot Gemfire RestfulApi

I. Technology for Infinispan cache tutorial

– Java 1.8
– Maven: 3.3.9
– Editor: Spring Tool Suite – Version 3.7.3.RELEASE
– Spring Boot: Version: 3.8.0.RELEASE

II. Overview
1. Structure of Project

spring caching- spring boot - infinispan - structure of project

2. Step to do

– Create Spring Boot project
– Add needed dependencies
– Configure Infinispan cache
– Create Data Model
– Create Caching Service
– Create Web Controller
– Enable Caching
– Run & Check Result with JConsole

3. Demo video
III. Practices
1. Create Spring Boot project

Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, input project info as below pic:

createproject-springboot-springcaching-infinispan

Press Next button, then press Finish, Spring Boot project is created.

2. Add needed dependencies

Need Infinispan Dependencies and Spring Cache & Spring Web MVC dependencies

3. Configure Infinispan cache

Open application.properties, config Infinispan

Create a infinispan.xml file then place it at: src/main/resources
Infinispan.xml file define a customer cache:

4. Create Data Model

Create a simple Customer Model that implements Serializable

5. Create Caching Service

Create caching service with 4 functions:
– @Cacheable(key=”#id”)
public Customer findByCode(long id): find a Customer in cache with key = “#id”. If a Customer with the key does Not exist in cache, it will create a Customer with the id then caching it.
@Cacheable: Annotation indicating that the result of invoking a method (or all methods in a class) can be cached.

– @CachePut(key=”#id”)
public Customer putCustomerToCache(long id): put a Customer to Cache, We can use it to modify an instance has been cached.
@CachePut: Annotation indicating that a method (or all methods on a class) triggers a cache put operation.

– @CacheEvict(allEntries = true)
@CacheEvict: public void evictAllEntries(): remove all instances in cache.
Annotation indicating that a method (or all methods on a class) triggers a cache evict operation.

– @CacheEvict(key=”#id”)
public void evictEntry(long id): remove a instance in cache.

6. Create Web Controller

Create a CacheController with 4 @RequestMapping.
– @RequestMapping(“/findCustById”)
– @RequestMapping(“/putCust”)
– @RequestMapping(“/evictAll”)
– @RequestMapping(“/evictEntry”)

7. Enable Caching

Using @EnableCaching: Enables Spring’s annotation-driven cache management capability, similar to the support found in Spring’s XML namespace.

8. Run & Check Result with JConsole

– Build project with Maven configure:
clean install

– Run Project with mode: Spring Boot App

– Open JConsole then chooses SpringCachingInfinispanApplication as pic:
spring caching- spring boot - infinispan - connection with process applicatio

Press Connect then choose MBeans as below pic:

spring caching- spring boot - infinispan - connectio - choose mbean

– Make a put Customers request:
http://localhost:8080/putCust?custNumber=100

Observe Cache customer, numberOfEntries has value: 100
See this image:

spring caching- spring boot - infinispan - put customer

See Logs:

– Make a findCustomerById request:
http://localhost:8080/findCustById?id=1

Results:

spring caching- spring boot - infinispan - cachable

Because the customer with (key = 1) does Not exist in Cache, So Log has:

– Make a findCustomerById request:
http://localhost:8080/findCustById?id=1

Observer caching, press Refresh: numberOfEntries has value: 101

spring caching- spring boot - infinispan - cachable - observer caching

– Make a findCustomerById request again:
http://localhost:8080/findCustById?id=1

Result the same with above request, because customer with id=1 has been cached.
Observer caching, press Refresh: numberOfEntries has value: 101

– Make Evict request to an element:
http://localhost:8080/evictEntry?id=1

Observer caching, press Refresh: numberOfEntries has value: 100.

– Make Evict request with all Entries:
http://localhost:8080/evictAll

Observer caching, press Refresh: numberOfEntries has value: 0.

IV. Source code

SpringCachingInfinispan

By JavaSampleApproach | August 27, 2016.


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*