SpringBoot Caffeine cache with PostgreSQL backend

Performance is a big problem in software development. And Caching is one solution to speed up system. In the tutorial, JavaSampleApproach will show you how to use SpringBoot Caffeine cache with PostGreSQL backend.

Related posts:
How to work with Spring Cache | Spring Boot
Couchbase – How to create Spring Cache Couchbase application with SpringBoot
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. SpringBoot Caffeine cache

What is Caffeine cache?

In the org.springframework.boot.autoconfigure.cache.CacheProperties, we can see:

-> Caffeine is a Java 8 rewrite of Guava’s cache that supersede the Guava support.

In the tutorial, we use SpringBoot and Caffeine cache with PostGreSQL backend as below architecture:

Springboot Caffeine Cache - architecture

For work with SpringCache Caffeine, we need dependencies:

If Caffeine is present, spring-boot-starter-cache ‘Starter’ will auto-configure a CaffeineCacheManager. Caches can be created on startup using the spring.cache.cache-names. We can cutomize a cache via properties in order:
1. A cache spec defined by spring.cache.caffeine.spec
2. A com.github.benmanes.caffeine.cache.CaffeineSpec bean is defined
3. A com.github.benmanes.caffeine.cache.Caffeine bean is defined

More details, we need check out the sourcecode of com.github.benmanes.caffeine.cache.CaffeineSpec or
com.github.benmanes.caffeine.cache.Caffeine:

Explanation:
maximumSize: Specifies the maximum number of entries the cache may contain
maximumWeight: Specifies the maximum weight of entries the cache may contain
expireAfterAccessDuration: Specifies that each entry should be automatically removed from the cache once a fixed duration has elapsed after the entry’s creation, the most recent replacement of its value, or its last read.
expireAfterWriteDuration: Specifies that each entry should be automatically removed from the cache once a fixed duration has elapsed after the entry’s creation, or the most recent replacement of its value.
refreshAfterWriteDuration: Specifies that active entries are eligible for automatic refresh once a fixed duration has elapsed after the entry’s creation, or the most recent replacement of its value.

Example:

III. Practice

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

Springboot Caffeine Cache - project structure

Step to do:
– Create SpringBoot project
– Create data model
– Create Repository
– Implement Service on top of Cache
– Implement RestAPIs
– Implement CommandLineRunner to init data
– Configuration
– Run and check results

1. Create SpringBoot project

Using SpringToolSuite to create a SpringBoot project, then add dependencies:

2. Create data model

– Create a Customer model:

3. Create Repository

– Using CrudRepository, create a interface CustomerRepository:

4. Implement Service on top of Cache

Implement a cache component CustomerCache as below:

Then implement CustomerServices service on top of CustomerCache:

5. Implement RestAPIs

Create a WebController with 3 APIs {/api/cachable, /api/cacheput, /api/cacheevict}:

6. Implement CommandLineRunner to init data

In main class, use CustomerRepository and CommandLineRunner to init data:

Note: @EnableCaching is used to enable caching.

7. Configuration

Open application.properties file, configure spring.cache.* and spring.datasource.*

8. Run and check results

Build and Run the SpringBoot project as commandlines: {mvn clean install, mvn spring-boot:run}.

-> See PostgreSQL database:

Springboot Caffeine Cache - database initial

– Make request 1: http://localhost:8080/api/cachable?id=1

-> Results:
Service process slowly and,

Springboot Caffeine Cache - request 1

Server has displayed a text on console:

– Make request 2: http://localhost:8080/api/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.

Springboot Caffeine Cache - request 1

– Make request 3: http://localhost:8080/api/cacheput?id=1&firstname=Peter
Message is returned on Browser:
Done
Now customer with id=1 is modified: firstname=’Peter’, NOT ‘Jack’.

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

Springboot Caffeine Cache - request 4

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

– Make request 6: http://localhost:8080/api/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:

Springboot Caffeine Cache - request 4

IV. Sourcecode

SpringBootCaffeineCache


Related Posts


Got Something To Say:

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

*