DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Prompt Caching: Overriding Tokenization for Faster and More Cost-Effective AI
  • Caching Issues With the Spring Expression Language
  • A Practical Guide to Semantic Caching With Redis LangCache
  • Caching Mechanisms Using Spring Boot With Redis or AWS ElastiCache

Trending

  • Building an AI-Powered Code Review Assistant in Java
  • Android 4.4 KitKat, the Browser and the Chrome WebView
  • Five Common Time-Series Queries Made Simple With Enterprise Java
  • Migrating From the OpenAI API to vLLM: Configure It Right

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Data
  4. Caching in MyBatis With Redis

Caching in MyBatis With Redis

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

By 
Nikita Koksharov
·
Dec. 23, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
17.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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:

XML
xxxxxxxxxx
1
 
1
     <dependency>
2
         <groupId>org.redisson</groupId>
3
         <artifactId>redisson-mybatis</artifactId>
4
         <version>3.12.0</version>
5
     </dependency>


The Gradle dependency is:

XML
xxxxxxxxxx
1
 
1
     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:

XML
xxxxxxxxxx
1
 
1
<cache type="org.redisson.hibernate.RedissonCache">
2
 <property name="timeToLive" value="200000"></property>
3
 <property name="maxIdleTime" value="100000"></property>
4
 <property name="maxSize" value="100000"></property>
5
 <property name="redissonConfig" value="redisson.yaml"></property>
6
</cache>


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

XML
xxxxxxxxxx
1
13
 
1
<cache type="org.redisson.hibernate.RedissonLocalCachedCache">
2
  <property name="timeToLive" value="200000"></property>
3
  <property name="maxIdleTime" value="100000"></property>
4
  <property name="maxSize" value="100000"></property>
5

          
6
  <property name="localCacheEvictionPolicy" value="LRU"></property>
7
  <property name="localCacheSize" value="1000"></property>
8
  <property name="localCacheTimeToLive" value="2000000"></property>
9
  <property name="localCacheMaxIdleTime" value="1000000"></property>
10
  <property name="localCacheSyncStrategy" value="INVALIDATE"></property>
11
     
12
  <property name="redissonConfig" value="redisson.yaml"></property>
13
</cache>


Thanks for reading!

Further Reading

Where Is My Cache? Architectural Patterns for Caching Microservices

Database Caching With Redis and Java

Cache (computing) MyBatis Redis (company)

Opinions expressed by DZone contributors are their own.

Related

  • Prompt Caching: Overriding Tokenization for Faster and More Cost-Effective AI
  • Caching Issues With the Spring Expression Language
  • A Practical Guide to Semantic Caching With Redis LangCache
  • Caching Mechanisms Using Spring Boot With Redis or AWS ElastiCache

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook