Redis Implementation for Database Operations in Spring Boot

Alexander Ang
3 min readDec 6, 2019

--

Prerequisite: https://medium.com/@alexanderang.24/spring-boot-cache-implementation-for-database-operations-cf846d6d8e3c

Dependency needed on pom.xml (+ from prerequisite):

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

Add these redis configuration to application.properties:

# Redis Config
spring.redis.host=localhost
spring.redis.port=6379
spring.cache.type=redis //set application cache type to redis
spring.cache.redis.time-to-live=60000 //cache expiration time in miliseconds
spring.cache.redis.cache-null-values=false //allow redis to cache null values
spring.cache.redis.use-key-prefix=true //to enable prefix in cache key name
spring.cache.redis.key-prefix=My Cache: //key name prefix

PeopleDto needs to implements Serializable. Objects have to be represented as byte arrays to be stored on disk and in Java the only standard way of doing that is Serialization.

A little changes in PeopleService’s caching annotation. Added cache keys and null result validations.

To see cache stored in redis server, open terminal and use “redis-cli” command to access redis server. Use “keys *” command to see list of all cache. To get cache key value, type “get [cache_name]”. List of all commands can be found at: https://redis.io/commands

You can also use redis GUI applications to access redis server, such as RedisClient, Redily, and rdbtools. In this case I’m using RedisClient because it’s totally free and lightweight. The interface looks like this:

Download link for redis GUI tools mentioned above:

Redis enable cross application cache usage, because caches are stored on redis server, not on application memory. So even when the application is shut down or restarted, the cache can still retrieveable by other application or when the application come back up. From the screenshot below we can see the application retrieve cache right after it is run.

You can see the source code here: https://github.com/plankrun/learn-cache-redis

References

https://www.devglan.com/blog/install-redis-windows-and-mac

https://www.devglan.com/spring-boot/spring-boot-redis-cache

https://www.journaldev.com/18141/spring-boot-redis-cache

https://hub.docker.com/_/redis

https://redis.io/commands

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Alexander Ang
Alexander Ang

Written by Alexander Ang

A curious software developer who loves to write.

No responses yet

Write a response