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.

I. Technologies

II. SpringBoot Caffeine cache

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

[..]

@ConfigurationProperties(prefix="spring.cache")

publicclassCacheProperties{

[..]

/**

* Guava specific cache properties.

*/

publicstaticclassGuava{

@Deprecated

@DeprecatedConfigurationProperty(reason="Caffeine will supersede the Guava support in Spring Boot 2.0",replacement="spring.cache.caffeine.spec")

publicStringgetSpec(){

returnthis.spec;

}

@Deprecated

publicvoidsetSpec(Stringspec){

this.spec=spec;

}

[..]

-> 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:

For work with SpringCache Caffeine, we need dependencies:

1

2

3

4

5

6

7

8

9

10

11

<!-- Spring Framework Caching Support -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-cache</artifactId>

</dependency>

<!-- caching provider -->

<dependency>

<groupId>com.github.ben-manes.caffeine</groupId>

<artifactId>caffeine</artifactId>

</dependency>

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 orcom.github.benmanes.caffeine.cache.Caffeine:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

...

publicfinalclassCaffeineSpec{

...

longmaximumSize=UNSET_INT;

longmaximumWeight=UNSET_INT;

...

longexpireAfterAccessDuration=UNSET_INT;

longexpireAfterWriteDuration=UNSET_INT;

longrefreshAfterWriteDuration=UNSET_INT;

...

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.

8. Run and check results

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

-> See PostgreSQL database:

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

-> Results:
Service process slowly and,

Server has displayed a text on console:

1

############# Backend processing...

– 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.

– 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:

– 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:

grokonez

ABOUT US
We are passionate engineers in software development by Java Technology & Spring Framework. We believe that creating little good thing with specific orientation everyday can make great influence on the world someday.