Caching in MyBatis With Redis

DZone 's Guide to

Caching in MyBatis With Redis

In this article, we'll discuss how to use caching in MyBatis via the Redisson Java client for Redis.

· Database Zone ·
Free Resource

man coding on three monitors birdseye view

MyBatis is an open-source, lightweight persistence framework for the Java programming language. With MyBatis, users can map Java methods to stored procedures or SQL statements. The transactional query caching feature in MyBatis allows users to cache their queries, reducing lookup times and improving performance.

Java developers often use MyBatis together with Redis, an open-source, in-memory data structure store that is often used to build NoSQL databases. But how do developers implement caching in MyBatis with Redis?

The MyBatis project includes a Redis Cache extension, but you can also use third-party Redis Java clients such as Redisson, which provides a number of useful advantages. In this article, we'll discuss how to use caching in MyBatis via the Redisson Java client for Redis.

You might also be interested in:  Java-Distributed Caching in Redis

MyBatis and Redis With Redisson

Redisson is a third-party Redis Java client that includes many implementations of familiar Java objects and collections. Java caching, data processing, task scheduling and execution, and microservices are all supported in Redisson.

The redisson-mybatis extension implements MyBatis caching for Redisson. As of this writing, Redisson is compatible with MyBatis 3.0.0 and above. Redisson offers two important caching features for MyBatis that are available in the Redisson PRO edition:

  • Local caching, which can execute read operations up to 45 times faster
  • Data partitioning in cluster mode, which helps developers scale the available memory and read/write operations

Using MyBatis Cache and Redis With Redisson

Integrating Redis with MyBatis Cache is just a matter of two simple steps with Redisson.

1. Add the redisson-mybatis Dependency

First, add the redisson-mybatis dependency to your project using Maven or Gradle. The Maven dependency is:


The Gradle dependency is:


2. Specify the MyBatis Cache Settings

Second, you'll need to specify the MyBatis cache settings for your project. The available parameters are:

  • timeToLive: This parameter specifies the maximum time an object is stored in the cache before deletion
  • maxIdleTime: This parameter specifies the maximum time an object can remain idle/unused before being automatically deleted
  • maxSize: This parameter specifies the maximum size of the entries stored in the cache
  • redissonConfig: This parameter provides a link to the Redisson configuration file in YAML format

If you are using Redisson's local caching feature for MyBatis, you can also specify the following parameters:

  • localCacheEvictionPolicy: This parameter specifies the local cache eviction policy. The available policies are LFU, LRU, SOFT, WEAK and NONE
  • localCacheSize: This parameter specifies the maximum size of the local cache
  • localCacheTimeToLive: This parameter specifies the local cache's time to live
  • localCacheMaxIdleTime: This parameter specifies the local cache's maximum idle time
  • localCacheSyncStrategy: This parameter specifies the local cache's strategy for synchronizing data. The available policies are INVALIDATE, UPDATE and NONE

Note that the current version of Redis Cache bundled with MyBatis supports only the timeToLive parameter for the entire cache; it does not support the timeToLive and maxIdleTime parameters for each entry. This is one advantage that Redisson has over the default Redis Cache implementation in MyBatis.

Below is an example specification for the MyBatis cache settings in Redisson:


The specification below is an example of MyBatis cache settings in Redisson with local caching:


Thanks for reading!

Further Reading

Where Is My Cache? Architectural Patterns for Caching Microservices

Database Caching With Redis and Java

redis ,java ,mybatis-cache ,database ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}