Couchbase – How to create Spring Cache Couchbase application with SpringBoot

Performance is a big problem in software development. And Caching is one solution to speed up system. In the past post, We are familiar with Couchbase database by learning how to build Spring JPA Couchbase application. So in the tutorial, JavaSampleApproach will continue to show you how to create Spring Cache Couchbase application with SpringBoot.

Related posts:
How to work with Spring Cache | Spring Boot
Couchbase – How to create Spring JPA Couchbase application with SpringBoot
SpringBoot Caffeine cache with PostgreSQL backend
SpringBoot Hazelcast cache with PostgreSQL backend

I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.6RELEASE
– Couchbase 4.6.2

II. Spring Cache Couchbase

We can use Couchbase as backing cache:

spring cache couchbase - architecture

For work with SpringCache Couchbase, we need a dependency:

How to configure Couchbase server as Cache?
-> We need to build a CacheManager with Couchbase cluster:

@EnableCaching is used to enable the caching.

Document for Couchbase cache is built with Serializable:

We use SpringCache annotations to define the ways for caching with each operation {@Cacheable, @CachePut, @CacheEvict}:

@Cacheable is one of the most important and common annotation for caching the requests. If the application sends multiple requests, this annotation will not execute the method which is Cachable multiple times, instead it will return the result from the cached storage.
@CachePut updates the cache which is stored and execute the method.
@CacheEvict is used for removing a single cache or clearing the entire cache from the cache storage. The method evict uses the input parameter id as a key to find the object that has the same id and removes it from the cache.

III. Practice

In the tutorial, we create a SpringBoot project as below:

spring cache couchbase - project structure

Step to do:
– Create SpringBoot project
– Create model class
– Configure Couchbase cache
– Implement repository
– Implement cache service
– Implement business service
– Create Restful APIs
– Deployment

1. Create SpringBoot project

Using SpringToolSuite to create a SpringBoot project, then add dependencies {pring-boot-starter-web, couchbase-spring-cache}:

2. Create model class

– Create a Customer model:

3. Configure Couchbase cache

Open file, configure Couchbase server:

Then configure CacheManager:

4. Implement repository

– Create an interface CustomerRepository:

– Implement above CustomerRepository:

5. Implement cache service

Using SpringCache annotations to implement CustomerCache:

6. Implement business service

Implement CustomerServices on top of above CustomerCache:

7. Create Restful APIs

Create 3 RestAPIs {/cachable, /cacheput, /cacheevict}:

8. Deployment
8.1 Setup Couchbase server

Follow the guide to setup Couchbase server and create a jsabucket

8.2 Run and check results

Check document in jsabucket before run above SpringBoot project.

Build and Run the SpringBoot project as commanlines {mvn clean install, mvn spring-boot:run}.
Request 1: http://localhost:8080/cachable?id=1
-> Results:
Service process slowly and,

spring cache couchbase - cacheable request 1

Server has displayed a text on console:

Check documents of Couchebase jsabucket:

spring cache couchbase - cacheable request 1 - couche document

Request 2: http://localhost:8080/cachable?id=1
Now the response is faster because Customer with id = 1 has been cached before, the application just get data from cache storage.

spring cache couchbase - cacheable request 1

Request 3: http://localhost:8080/cacheput?id=1&firstname=Peter
Message is returned on Browser:
Now customer with id=1 is modified: firstname=”Peter”, not “Jack”.

Request 4: http://localhost:8080/cachable?id=1
Response is faster BUT the result is difference from first request:

spring cache couchbase - cacheable request 4

Request 5 – Make a cache-evict request: http://localhost:8080/cacheevict?id=1
Browser displays:
Now customer with id=1 was evicted from cache storage.

spring cache couchbase - jsabucket - no document

Request 6: http://localhost:8080/cachable?id=1
Now the behavior of the browser is the same as the first request because customer with id=1 was evicted to cache, and the method under @Cachable is executed.
Service process slowly and result:

spring cache couchbase - cacheable request 4

Server, of course, displays a text on console:

spring cache couchbase - cacheable request 1 - couche document

IV. Sourcecode


By grokonez | August 24, 2017.

Related Posts

Got Something To Say:

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