Redis cache in use in MyBatis

In this context, sharp distinctions lesson will discuss how Redis of Redisson Java client MyBatis to use the cache.

MyBatis is a Java open source, lightweight persistence framework programming language. Use MyBatis , the user can Java mapping method to a stored procedure or SQL statement. MyBatis transaction query caching function enables the user to cache their queries, thus reducing search time and improve performance.

Java developers commonly MyBatis with the Redis (an open source data structure stored in memory) used in combination, the Redis commonly used to construct NoSQL database. But developers how to use Redis in MyBatis achieve cache?

MyBatis project includes Redis Cache extension, but you can also use a third-party Redis Java client (eg Redisson ), which provides many useful advantages. In this context, sharp distinctions lesson will discuss how Redis of Redisson Java client MyBatis to use the cache.

 

MyBatis and Redis and Redisson

Redisson third-party Redis Java clients, including many familiar Java objects and realize collections. Redisson support Java caching, data processing, task scheduling and execution as well as micro-services.

redisson-mybatis expansion achieved Redisson of MyBatis cache. As of this writing, Redisson with MyBatis 3.0.0 and later versions are compatible. Redisson to MyBatis provides two important buffer function, Redisson PRO version offers these features:

·          Local cache, the speed of performing a read operation can be increased up to 45 times

·          Data partition in the cluster model, allows developers to extend the available memory, and read / write operations

 

The MyBatis cache and Redis and Redisson used together

Redis and MyBatis Cache integration just Redisson two simple steps.

1. Add redisson-mybatis dependencies

First, use Maven or Gradle will redisson-mybatis added to your project dependencies. Maven dependencies are:

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

 

Gradle dependencies are:

1      compile 'org.redisson:redisson-mybatis:3.12.0'

 

2. specify MyBatis cache settings

Second, you need to specify the project MyBatis cache settings. Available parameters are:

·          TimeToLive : This parameter specifies the object is deleted before the maximum time the cache storage object.

·          MaxIdleTime: This parameter specifies the object before being automatically deleted may remain idle / maximum time unused. 

·          MaxSize: This parameter specifies the entries stored in the cache of the maximum size. 

·          RedissonConfig: This parameter YAML provide point format Redisson profile link. 

If you Redisson local cache function for MyBatis , you can also specify the following parameters:

·          LocalCacheEvictionPolicy: This parameter specifies the local cache eviction policy. Available policy for LFU , the LRU , SOFT , WEAK and NONE 

·          LocalCacheSize: This parameter specifies the maximum size of the local cache 

·          LocalCacheTimeToLive: This parameter specifies the local cache lifetime 

·          LocalCacheMaxIdleTime: This parameter specifies the maximum idle time local cache 

·          LocalCacheSyncStrategy: This parameter specifies the local cache of data synchronization strategy. Available policy INVALIDATE , UPDATE , and NONE 

请注意,与MyBatis捆绑在一起的当前版本的Redis缓存仅支持整个缓存的timeToLive参数。它不支持每个条目的timeToLivemaxIdleTime参数。这是Redisson优于MyBatis中默认Redis Cache实现的优势之一。

以下是RedissonMyBatis缓存设置的示例规范:

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>

 

以下规范是Redisson中具有本地缓存的MyBatis缓存设置的示例:

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

 

谢谢阅读!

更深入的探讨欢迎留言或私信,还可以和你分享更多Java学习资料!

 


Guess you like

Origin blog.51cto.com/14631216/2461965