Caching in MyBatis With Redis
In this article, we'll discuss how to use caching in MyBatis via the Redisson Java client for Redis.
Join the DZone community and get the full member experience.
Join For FreeMyBatis 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:
xxxxxxxxxx
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-mybatis</artifactId>
<version>3.12.0</version>
</dependency>
The Gradle dependency is:
xxxxxxxxxx
compile 'org.redisson:redisson-mybatis:3.12.0'
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:
xxxxxxxxxx
<cache type="org.redisson.hibernate.RedissonCache">
<property name="timeToLive" value="200000"></property>
<property name="maxIdleTime" value="100000"></property>
<property name="maxSize" value="100000"></property>
<property name="redissonConfig" value="redisson.yaml"></property>
</cache>
The specification below is an example of MyBatis cache settings in Redisson with local caching:
xxxxxxxxxx
<cache type="org.redisson.hibernate.RedissonLocalCachedCache">
<property name="timeToLive" value="200000"></property>
<property name="maxIdleTime" value="100000"></property>
<property name="maxSize" value="100000"></property>
<property name="localCacheEvictionPolicy" value="LRU"></property>
<property name="localCacheSize" value="1000"></property>
<property name="localCacheTimeToLive" value="2000000"></property>
<property name="localCacheMaxIdleTime" value="1000000"></property>
<property name="localCacheSyncStrategy" value="INVALIDATE"></property>
<property name="redissonConfig" value="redisson.yaml"></property>
</cache>
Thanks for reading!
Further Reading
Where Is My Cache? Architectural Patterns for Caching Microservices
Opinions expressed by DZone contributors are their own.
Comments