You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
redisson/redisson-mybatis/README.md

165 lines
8.4 KiB
Markdown

5 years ago
# Redis based MyBatis Cache implementation
5 years ago
5 years ago
Implements [MyBatis Cache](https://mybatis.org/mybatis-3/sqlmap-xml.html#cache) based on Redis.
5 years ago
Compatible with MyBatis 3.0.0+
8 months ago
Redisson provides various MyBatis Cache implementations with features below:
5 years ago
8 months ago
**local cache** - so called `near cache` used to speed up read operations and avoid network roundtrips. It caches Map entries on Redisson side and executes read operations up to **45x faster** in comparison with common implementation. Local cache instances with the same name connected to the same pub/sub channel. This channel is used for exchanging of update/invalidate events between all instances. Local cache store doesn't use `hashCode()`/`equals()` methods of key object, instead it uses hash of serialized state.
5 years ago
8 months ago
**data partitioning** - although Map object is cluster compatible its content isn't scaled/partitioned across multiple Redis master nodes in cluster. Data partitioning allows to scale available memory, read/write operations and entry eviction process for individual Map instance in Redis cluster.
5 years ago
6 months ago
#### 1. Scripted eviction
Allows to define `time to live` or `max idle time` parameters per entry. Redis hash structure doesn't support eviction thus it's done on Redisson side through a custom scheduled task which removes expired entries using Lua script. Eviction task is started once per unique object name at the moment of getting Map instance. If instance isn't used and has expired entries it should be get again to start the eviction process. This leads to extra Redis calls and eviction task per unique map object name.
Entries are cleaned time to time by `org.redisson.eviction.EvictionScheduler`. By default, it removes 100 expired entries at a time. This can be changed through [cleanUpKeysAmount](https://github.com/redisson/redisson/wiki/2.-Configuration#cleanupkeysamount) setting. Task launch time tuned automatically and depends on expired entries amount deleted in previous time and varies between 5 second to 30 minutes by default. This time interval can be changed through [minCleanUpDelay](https://github.com/redisson/redisson/wiki/2.-Configuration#mincleanupdelay) and [maxCleanUpDelay](https://github.com/redisson/redisson/wiki/2.-Configuration#maxcleanupdelay). For example, if clean task deletes 100 entries each time it will be executed every 5 seconds (minimum execution delay). But if current expired entries amount is lower than previous one then execution delay will be increased by 1.5 times and decreased otherwise.
Available implementations:
|Class name | Local<br/>cache | Data<br/>partitioning | Ultra-fast<br/>read/write |
| ------------- | :-----------: | :----------:| :----------:|
|RedissonCache<br/><sub><i>open-source version</i></sub> | ❌ | ❌ | ❌ |
|RedissonCache<br/><sub><i>[Redisson PRO](http://redisson.pro) version</i></sub> | ❌ | ❌ | ✔️ |
|RedissonLocalCachedCache<br/><sub><i>available only in [Redisson PRO](http://redisson.pro)</i></sub> | ✔️ | ❌ | ✔️ |
|RedissonClusteredCache<br/><sub><i>available only in [Redisson PRO](http://redisson.pro)</i></sub> | ❌ | ✔️ | ✔️ |
|RedissonClusteredLocalCachedCache<br/><sub><i>available only in [Redisson PRO](http://redisson.pro)</i></sub> | ✔️ | ✔️ | ✔️ |
#### 2. Advanced eviction
Allows to define `time to live` parameter per map entry. Doesn't use an entry eviction task, entries are cleaned on Redis side.
Available implementations:
|Class name | Local<br/>cache | Data<br/>partitioning | Ultra-fast<br/>read/write |
| ------------- | :-----------: | :----------:| :----------:|
|RedissonCacheV2<br/><sub><i>available only in [Redisson PRO](http://redisson.pro)</i></sub> | ❌ | ✔️ | ✔️ |
|RedissonLocalCachedCacheV2<br/><sub><i>available only in [Redisson PRO](http://redisson.pro)</i></sub> | ✔️ | ✔️ | ✔️ |
#### 3. Native eviction
Allows to define `time to live` parameter per map entry. Doesn't use an entry eviction task, entries are cleaned on Redis side.
Requires **Redis 7.4+**.
Available implementations:
|Class name | Local<br/>cache | Data<br/>partitioning | Ultra-fast<br/>read/write |
| ------------- | :-----------: | :----------:| :----------:|
|RedissonCacheNative<br/><sub><i>open-source version</i></sub> | ❌ | ❌ | ❌ |
|RedissonCacheNative<br/><sub><i>[Redisson PRO](http://redisson.pro) version</i></sub> | ❌ | ❌ | ✔️ |
6 months ago
|RedissonLocalCachedCacheNative<br/><sub><i>available only in [Redisson PRO](http://redisson.pro)</i></sub> | ✔️ | ❌ | ✔️ |
6 months ago
|RedissonClusteredCacheNative<br/><sub><i>available only in [Redisson PRO](http://redisson.pro)</i></sub> | ❌ | ✔️ | ✔️ |
5 years ago
5 years ago
## MyBatis Cache Usage
5 years ago
### 1. Add `redisson-mybatis` dependency into your project:
Maven
```xml
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-mybatis</artifactId>
6 months ago
<version>3.35.0</version>
5 years ago
</dependency>
```
Gradle
```groovy
6 months ago
compile 'org.redisson:redisson-mybatis:3.35.0'
5 years ago
```
### 2. Specify MyBatis cache settings
Redisson allows to define follow settings per Cache instance:
`timeToLive` - defines time to live per cache entry
`maxIdleTime` - defines max idle time per cache entry
`maxSize` - defines max size of entries amount stored in Redis
5 years ago
`localCacheProvider` - cache provider used as local cache store. `REDISSON` and `CAFFEINE` providers are available. Default value: `REDISSON`
5 years ago
`localCacheEvictionPolicy` - local cache eviction policy. `LFU`, `LRU`, `SOFT`, `WEAK` and `NONE` eviction policies are available.
`localCacheSize` - local cache size. If size is `0` then local cache is unbounded.
`localCacheTimeToLive` - time to live in milliseconds for each map entry in local cache. If value equals to `0` then timeout is not applied.
`localCacheMaxIdleTime` - max idle time in milliseconds for each map entry in local cache. If value equals to `0` then timeout is not applied.
`localCacheSyncStrategy` - local cache sync strategy. `INVALIDATE`, `UPDATE` and `NONE` eviction policies are available.
5 years ago
`redissonConfig` - defines path to redisson config in YAML format
8 months ago
Example cache definitions:
5 years ago
```xml
5 years ago
<cache type="org.redisson.mybatis.RedissonCache">
5 years ago
<property name="timeToLive" value="200000"/>
<property name="maxIdleTime" value="100000"/>
<property name="maxSize" value="100000"/>
<property name="redissonConfig" value="redisson.yaml"/>
</cache>
8 months ago
<cache type="org.redisson.mybatis.RedissonCacheNative">
<property name="timeToLive" value="200000"/>
<property name="redissonConfig" value="redisson.yaml"/>
</cache>
<cache type="org.redisson.mybatis.RedissonCacheV2">
<property name="timeToLive" value="200000"/>
<property name="redissonConfig" value="redisson.yaml"/>
</cache>
5 years ago
<cache type="org.redisson.mybatis.RedissonLocalCachedCache">
5 years ago
<property name="timeToLive" value="200000"/>
<property name="maxIdleTime" value="100000"/>
<property name="maxSize" value="100000"/>
5 years ago
<property name="localCacheEvictionPolicy" value="LRU"/>
<property name="localCacheSize" value="1000"/>
<property name="localCacheTimeToLive" value="2000000"/>
<property name="localCacheMaxIdleTime" value="1000000"/>
<property name="localCacheSyncStrategy" value="INVALIDATE"/>
5 years ago
<property name="redissonConfig" value="redisson.yaml"/>
</cache>
8 months ago
<cache type="org.redisson.mybatis.RedissonLocalCachedCacheV2">
<property name="timeToLive" value="200000"/>
<property name="localCacheEvictionPolicy" value="LRU"/>
<property name="localCacheSize" value="1000"/>
<property name="localCacheTimeToLive" value="2000000"/>
<property name="localCacheMaxIdleTime" value="1000000"/>
<property name="localCacheSyncStrategy" value="INVALIDATE"/>
<property name="redissonConfig" value="redisson.yaml"/>
</cache>
5 years ago
<cache type="org.redisson.mybatis.RedissonClusteredCache">
5 years ago
<property name="timeToLive" value="200000"/>
<property name="maxIdleTime" value="100000"/>
<property name="maxSize" value="100000"/>
<property name="redissonConfig" value="redisson.yaml"/>
</cache>
5 years ago
<cache type="org.redisson.mybatis.RedissonClusteredLocalCachedCache">
5 years ago
<property name="timeToLive" value="200000"/>
<property name="maxIdleTime" value="100000"/>
<property name="maxSize" value="100000"/>
5 years ago
<property name="localCacheEvictionPolicy" value="LRU"/>
<property name="localCacheSize" value="1000"/>
<property name="localCacheTimeToLive" value="2000000"/>
<property name="localCacheMaxIdleTime" value="1000000"/>
<property name="localCacheSyncStrategy" value="INVALIDATE"/>
5 years ago
<property name="redissonConfig" value="redisson.yaml"/>
</cache>
5 years ago
```