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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Scaling in Practice: Caching and Rate-Limiting With Redis and Next.js
  • Caching RESTful API Requests With Heroku Data for Redis
  • Enhancing Performance With Amazon Elasticache Redis: In-Depth Insights Into Cluster and Non-Cluster Modes
  • Simple Sophisticated Object Cache Service Using Azure Redis

Trending

  • AI-Driven Root Cause Analysis in SRE: Enhancing Incident Resolution
  • How to Build Real-Time BI Systems: Architecture, Code, and Best Practices
  • Customer 360: Fraud Detection in Fintech With PySpark and ML
  • Mastering Advanced Aggregations in Spark SQL
  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 user avatar
Nikita Koksharov
·
Dec. 23, 19 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
16.4K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • Scaling in Practice: Caching and Rate-Limiting With Redis and Next.js
  • Caching RESTful API Requests With Heroku Data for Redis
  • Enhancing Performance With Amazon Elasticache Redis: In-Depth Insights Into Cluster and Non-Cluster Modes
  • Simple Sophisticated Object Cache Service Using Azure Redis

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!