README.md 15.5 KB
Newer Older
A
Andrew Lyu 已提交
1
# Redis based Hibernate Cache implementation
2

N
Nikita Koksharov 已提交
3 4
Implements [Hibernate 2nd level Cache](https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#caching) provider based on Redis.  
Supports all Hibernate cache strategies: `READ_ONLY`, `NONSTRICT_READ_WRITE`, `READ_WRITE` and `TRANSACTIONAL`.  
N
Nikita Koksharov 已提交
5
It's recommended to use FST or Snappy as [codec](https://github.com/redisson/redisson/wiki/4.-data-serialization).
6

N
Nikita Koksharov 已提交
7
Compatible with Hibernate 5.3.3+ up to 5.4.x
8

N
Nikita Koksharov 已提交
9 10
Redisson provides various Hibernate Cache factories including those with features below:

A
Andrew Lyu 已提交
11
**local cache** - so called `near cache`, which is useful for use cases when Hibernate Cache used mostly for read operations and/or network roundtrips are undesirable. It caches Map entries on Redisson side and executes read operations up to **5x faster** in comparison with common implementation. All local caches with the same name connected to the same pub/sub channel which is used for messaging between them. In particular to send entity update or entity invalidate event.
N
Nikita Koksharov 已提交
12

N
Nikita Koksharov 已提交
13
**data partitioning** - it allows to scale available memory, read/write operations and entry eviction process for individual Hibernate Cache instance in Redis cluster.
N
Nikita Koksharov 已提交
14

A
Andrew Lyu 已提交
15
Below is the list of all available factories with local cache and/or data partitioning support:
N
Nikita Koksharov 已提交
16

N
Nikita Koksharov 已提交
17
|Class name | Local cache | Data partitioning |
N
Nikita Koksharov 已提交
18
| ------------- | ------------- | ------------|
N
Nikita Koksharov 已提交
19 20 21 22
|RedissonRegionFactory<br/>&nbsp; | :heavy_multiplication_x: | :heavy_multiplication_x: |
|RedissonLocalCachedRegionFactory<br/><sub><i>available only in [Redisson PRO](http://redisson.pro) edition</i></sub>  | :heavy_check_mark: | :heavy_multiplication_x: |
|RedissonClusteredRegionFactory<br/><sub><i>available only in [Redisson PRO](http://redisson.pro) edition</i></sub> | :heavy_multiplication_x: | :heavy_check_mark: |
|RedissonClusteredLocalCachedRegionFactory<br/><sub><i>available only in [Redisson PRO](http://redisson.pro) edition</i></sub> | :heavy_check_mark: | :heavy_check_mark: |
N
Nikita Koksharov 已提交
23

A
Andrew Lyu 已提交
24
## Hibernate Cache Usage
25

A
Andrew Lyu 已提交
26
### 1. Add `redisson-hibernate` dependency into your project:
27

N
Nikita Koksharov 已提交
28
Maven
A
Andrew Lyu 已提交
29

N
Nikita Koksharov 已提交
30
```xml
31 32
     <dependency>
         <groupId>org.redisson</groupId>
N
Nikita Koksharov 已提交
33
         <!-- for Hibernate v5.3.3+ - v5.4.x -->
34
         <artifactId>redisson-hibernate-53</artifactId>
N
Nikita Koksharov 已提交
35
         <version>3.12.2</version>
36
     </dependency>
N
Nikita Koksharov 已提交
37 38 39
```

Gradle
40

A
Andrew Lyu 已提交
41
```groovy
N
Nikita Koksharov 已提交
42
     // for Hibernate v5.3.3+ - v5.4.x
N
Nikita Koksharov 已提交
43
     compile 'org.redisson:redisson-hibernate-53:3.12.2'
A
Andrew Lyu 已提交
44
```
45 46 47

### 2. Specify hibernate cache settings

N
Nikita Koksharov 已提交
48
Define Redisson Region Cache Factory:
49

N
Nikita Koksharov 已提交
50 51
```xml
<!-- Redisson Region Cache factory -->
52
<property name="hibernate.cache.region.factory_class" value="org.redisson.hibernate.RedissonRegionFactory" />
N
Nikita Koksharov 已提交
53 54 55 56 57 58
<!-- or -->
<property name="hibernate.cache.region.factory_class" value="org.redisson.hibernate.RedissonLocalCachedRegionFactory" />
<!-- or -->
<property name="hibernate.cache.region.factory_class" value="org.redisson.hibernate.RedissonClusteredRegionFactory" />
<!-- or -->
<property name="hibernate.cache.region.factory_class" value="org.redisson.hibernate.RedissonClusteredLocalCachedRegionFactory" />
N
Nikita Koksharov 已提交
59
```
A
Andrew Lyu 已提交
60

N
Nikita Koksharov 已提交
61
By default each Region Factory creates own Redisson instance. For multiple applications, using the same Redis setup and deployed in the same JVM, amount of Redisson instances could be reduced using JNDI registry:
A
Andrew Lyu 已提交
62

N
Nikita Koksharov 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
```xml
<!-- name of Redisson instance registered in JNDI -->
<property name="hibernate.cache.region.jndi_name" value="redisson_instance" />
<!-- JNDI Redisson Region Cache factory -->
<property name="hibernate.cache.region.factory_class" value="org.redisson.hibernate.JndiRedissonRegionFactory" />
<!-- or -->
<property name="hibernate.cache.region.factory_class" value="org.redisson.hibernate.JndiRedissonLocalCachedRegionFactory" />
<!-- or -->
<property name="hibernate.cache.region.factory_class" value="org.redisson.hibernate.JndiRedissonClusteredRegionFactory" />
<!-- or -->
<property name="hibernate.cache.region.factory_class" value="org.redisson.hibernate.JndiRedissonClusteredLocalCachedRegionFactory" />
```

```xml
<!-- 2nd level cache activation -->
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.cache.use_query_cache" value="true" />
N
Nikita Koksharov 已提交
80

N
Nikita Koksharov 已提交
81 82 83
<!-- Redisson can fallback on database if Redis cache is unavailable -->
<property name="hibernate.cache.redisson.fallback" value="true" />

N
Nikita Koksharov 已提交
84
<!-- Redisson YAML config (located in filesystem or classpath) -->
85 86 87
<property name="hibernate.cache.redisson.config" value="/redisson.yaml" />
```

A
Andrew Lyu 已提交
88
#### Redisson allows to define follow cache settings per entity, collection, naturalid, query and timestamp regions:
N
Nikita Koksharov 已提交
89

A
Andrew Lyu 已提交
90
`hibernate.cache.redisson.[REGION_NAME].eviction.max_entries` - max size of cache. Superfluous entries in Redis are evicted using LRU algorithm. `0` value means unbounded cache. Default value: 0
N
Nikita Koksharov 已提交
91

A
Andrew Lyu 已提交
92
`hibernate.cache.redisson.[REGION_NAME].expiration.time_to_live` - time to live per cache entry in Redis. Defined in milliseconds. `0` value means this setting doesn't affect expiration. Default value: 0
N
Nikita Koksharov 已提交
93

A
Andrew Lyu 已提交
94
`hibernate.cache.redisson.[REGION_NAME].expiration.max_idle_time` - max idle time per cache entry in Redis. Defined in milliseconds. `0` value means this setting doesn't affect expiration. Default value: 0
N
Nikita Koksharov 已提交
95

A
Andrew Lyu 已提交
96
`hibernate.cache.redisson.[REGION_NAME].localcache.max_idle_time` - max idle time per entry in local cache. Defined in milliseconds. `0` value means this setting doesn't affect expiration. Default value: 0
N
Nikita Koksharov 已提交
97

A
Andrew Lyu 已提交
98
`hibernate.cache.redisson.[REGION_NAME].localcache.time_to_live` - time to live per entry in local cache. Defined in milliseconds. `0` value means this setting doesn't affect expiration. Default value: 0
N
Nikita Koksharov 已提交
99

A
Andrew Lyu 已提交
100
`hibernate.cache.redisson.[REGION_NAME].localcache.eviction_policy` - eviction policy applied to local cache entries when cache size limit reached. LFU, LRU, SOFT, WEAK and NONE policies are available. Default value: NONE
N
Nikita Koksharov 已提交
101

A
Andrew Lyu 已提交
102 103 104 105
`hibernate.cache.redisson.[REGION_NAME].localcache.sync_strategy` - sync strategy used to synchronize local cache changes across all instances. Follow sync strategies are available:
INVALIDATE - Default. Invalidate cache entry across all LocalCachedMap instances on map entry change
UPDATE - Update cache entry across all LocalCachedMap instances on map entry change
NONE - No synchronizations on map changes
N
Nikita Koksharov 已提交
106

N
Nikita Koksharov 已提交
107
Default value: INVALIDATE
N
Nikita Koksharov 已提交
108

A
Andrew Lyu 已提交
109 110 111 112
`hibernate.cache.redisson.[REGION_NAME].localcache.reconnection_strategy` - reconnection strategy used to load missed updates through Hibernate during any connection failures to Redis. Since, local cache updates can't be get in absence of connection to Redis. Follow reconnection strategies are available:
CLEAR - Clear local cache if map instance has been disconnected for a while.
LOAD - Store invalidated entry hash in invalidation log for 10 minutes. Cache keys for stored invalidated entry hashes will be removed if LocalCachedMap instance has been disconnected less than 10 minutes or whole cache will be cleaned otherwise.
NONE - Default. No reconnection handling
N
Nikita Koksharov 已提交
113

A
Andrew Lyu 已提交
114
Default value: NONE
N
Nikita Koksharov 已提交
115 116 117 118

`hibernate.cache.redisson.[REGION_NAME].localcache.size` - max size of local cache. Superfluous entries in Redis are evicted using defined eviction policy.  `0` value means unbounded cache. Default value: 0

_Please note: `*.localcache.*` settings are available for `RedissonClusteredLocalCachedRegionFactory` and `RedissonLocalCachedRegionFactory` classes only._
119

N
Nikita Koksharov 已提交
120
#### Default region configuration used for all caches not specified in configuration:
N
Nikita Koksharov 已提交
121

N
Nikita Koksharov 已提交
122 123
```xml
<!-- cache definition applied to all caches in entity region -->
N
Nikita Koksharov 已提交
124 125 126 127 128 129 130 131 132 133
<property name="hibernate.cache.redisson.entity.eviction.max_entries" value="10000" />
<property name="hibernate.cache.redisson.entity.expiration.time_to_live" value="600000" />
<property name="hibernate.cache.redisson.entity.expiration.max_idle_time" value="300000" />

<property name="hibernate.cache.redisson.entity.localcache.max_idle_time" value="300000" />
<property name="hibernate.cache.redisson.entity.localcache.time_to_live" value="300000" />
<property name="hibernate.cache.redisson.entity.localcache.eviction_policy" value="LRU" />
<property name="hibernate.cache.redisson.entity.localcache.sync_strategy" value="INVALIDATE" />
<property name="hibernate.cache.redisson.entity.localcache.reconnection_strategy" value="CLEAR" />
<property name="hibernate.cache.redisson.entity.localcache.size" value="5000" />
N
Nikita Koksharov 已提交
134

N
Nikita Koksharov 已提交
135 136 137 138 139
<!-- cache definition applied to all caches in collection region -->
<property name="hibernate.cache.redisson.collection.eviction.max_entries" value="10000" />
<property name="hibernate.cache.redisson.collection.expiration.time_to_live" value="600000" />
<property name="hibernate.cache.redisson.collection.expiration.max_idle_time" value="300000" />

N
Nikita Koksharov 已提交
140 141 142 143 144 145 146
<property name="hibernate.cache.redisson.collection.localcache.max_idle_time" value="300000" />
<property name="hibernate.cache.redisson.collection.localcache.time_to_live" value="300000" />
<property name="hibernate.cache.redisson.collection.localcache.eviction_policy" value="LRU" />
<property name="hibernate.cache.redisson.collection.localcache.sync_strategy" value="INVALIDATE" />
<property name="hibernate.cache.redisson.collection.localcache.reconnection_strategy" value="CLEAR" />
<property name="hibernate.cache.redisson.collection.localcache.size" value="5000" />

N
Nikita Koksharov 已提交
147 148 149 150 151
<!-- cache definition applied to all caches in naturalid region -->
<property name="hibernate.cache.redisson.naturalid.eviction.max_entries" value="10000" />
<property name="hibernate.cache.redisson.naturalid.expiration.time_to_live" value="600000" />
<property name="hibernate.cache.redisson.naturalid.expiration.max_idle_time" value="300000" />

N
Nikita Koksharov 已提交
152 153 154 155 156 157 158
<property name="hibernate.cache.redisson.naturalid.localcache.max_idle_time" value="300000" />
<property name="hibernate.cache.redisson.naturalid.localcache.time_to_live" value="300000" />
<property name="hibernate.cache.redisson.naturalid.localcache.eviction_policy" value="LRU" />
<property name="hibernate.cache.redisson.naturalid.localcache.sync_strategy" value="INVALIDATE" />
<property name="hibernate.cache.redisson.naturalid.localcache.reconnection_strategy" value="CLEAR" />
<property name="hibernate.cache.redisson.naturalid.localcache.size" value="5000" />

N
Nikita Koksharov 已提交
159 160 161 162 163
<!-- cache definition applied to all caches in query region -->
<property name="hibernate.cache.redisson.query.eviction.max_entries" value="10000" />
<property name="hibernate.cache.redisson.query.expiration.time_to_live" value="600000" />
<property name="hibernate.cache.redisson.query.expiration.max_idle_time" value="300000" />

N
Nikita Koksharov 已提交
164 165 166 167 168 169 170
<property name="hibernate.cache.redisson.query.localcache.max_idle_time" value="300000" />
<property name="hibernate.cache.redisson.query.localcache.time_to_live" value="300000" />
<property name="hibernate.cache.redisson.query.localcache.eviction_policy" value="LRU" />
<property name="hibernate.cache.redisson.query.localcache.sync_strategy" value="INVALIDATE" />
<property name="hibernate.cache.redisson.query.localcache.reconnection_strategy" value="CLEAR" />
<property name="hibernate.cache.redisson.query.localcache.size" value="5000" />

N
Nikita Koksharov 已提交
171 172 173 174
<!-- cache definition for timestamps region -->
<property name="hibernate.cache.redisson.timestamps.eviction.max_entries" value="10000" />
<property name="hibernate.cache.redisson.timestamps.expiration.time_to_live" value="600000" />
<property name="hibernate.cache.redisson.timestamps.expiration.max_idle_time" value="300000" />
N
Nikita Koksharov 已提交
175 176 177 178 179 180 181

<property name="hibernate.cache.redisson.timestamps.localcache.max_idle_time" value="300000" />
<property name="hibernate.cache.redisson.timestamps.localcache.time_to_live" value="300000" />
<property name="hibernate.cache.redisson.timestamps.localcache.eviction_policy" value="LRU" />
<property name="hibernate.cache.redisson.timestamps.localcache.sync_strategy" value="INVALIDATE" />
<property name="hibernate.cache.redisson.timestamps.localcache.reconnection_strategy" value="CLEAR" />
<property name="hibernate.cache.redisson.timestamps.localcache.size" value="5000" />
N
Nikita Koksharov 已提交
182 183
```

N
Nikita Koksharov 已提交
184
#### Configuration per entity/collection/naturalid/query region overrides default configuration:
185

N
Nikita Koksharov 已提交
186
```xml
187 188 189 190 191
<!-- cache definition for entity region. Example region name: "my_object" -->
<property name="hibernate.cache.redisson.my_object.eviction.max_entries" value="10000" />
<property name="hibernate.cache.redisson.my_object.expiration.time_to_live" value="600000" />
<property name="hibernate.cache.redisson.my_object.expiration.max_idle_time" value="300000" />

N
Nikita Koksharov 已提交
192 193 194 195 196 197 198
<property name="hibernate.cache.redisson.my_object.localcache.max_idle_time" value="300000" />
<property name="hibernate.cache.redisson.my_object.localcache.time_to_live" value="300000" />
<property name="hibernate.cache.redisson.my_object.localcache.eviction_policy" value="LRU" />
<property name="hibernate.cache.redisson.my_object.localcache.sync_strategy" value="INVALIDATE" />
<property name="hibernate.cache.redisson.my_object.localcache.reconnection_strategy" value="CLEAR" />
<property name="hibernate.cache.redisson.my_object.localcache.size" value="5000" />

199 200 201 202 203
<!-- cache definition for collection region. Example region name: "my_list" -->
<property name="hibernate.cache.redisson.my_list.eviction.max_entries" value="10000" />
<property name="hibernate.cache.redisson.my_list.expiration.time_to_live" value="600000" />
<property name="hibernate.cache.redisson.my_list.expiration.max_idle_time" value="300000" />

N
Nikita Koksharov 已提交
204 205 206 207 208 209 210
<property name="hibernate.cache.redisson.my_list.localcache.max_idle_time" value="300000" />
<property name="hibernate.cache.redisson.my_list.localcache.time_to_live" value="300000" />
<property name="hibernate.cache.redisson.my_list.localcache.eviction_policy" value="LRU" />
<property name="hibernate.cache.redisson.my_list.localcache.sync_strategy" value="INVALIDATE" />
<property name="hibernate.cache.redisson.my_list.localcache.reconnection_strategy" value="CLEAR" />
<property name="hibernate.cache.redisson.my_list.localcache.size" value="5000" />

N
Nikita Koksharov 已提交
211
<!-- cache definition for naturalid region. Suffixed by ##NaturalId. Example region name: "my_object" -->
212 213 214 215
<property name="hibernate.cache.redisson.my_object##NaturalId.eviction.max_entries" value="10000" />
<property name="hibernate.cache.redisson.my_object##NaturalId.expiration.time_to_live" value="600000" />
<property name="hibernate.cache.redisson.my_object##NaturalId.expiration.max_idle_time" value="300000" />

N
Nikita Koksharov 已提交
216 217 218 219 220 221
<property name="hibernate.cache.redisson.my_object##NaturalId.localcache.max_idle_time" value="300000" />
<property name="hibernate.cache.redisson.my_object##NaturalId.localcache.time_to_live" value="300000" />
<property name="hibernate.cache.redisson.my_object##NaturalId.localcache.eviction_policy" value="LRU" />
<property name="hibernate.cache.redisson.my_object##NaturalId.localcache.sync_strategy" value="INVALIDATE" />
<property name="hibernate.cache.redisson.my_object##NaturalId.localcache.reconnection_strategy" value="CLEAR" />
<property name="hibernate.cache.redisson.my_object##NaturalId.localcache.size" value="5000" />
222

N
Nikita Koksharov 已提交
223 224 225 226 227 228 229 230 231 232 233
<!-- cache definition for query region. Example region name: "my_query" -->
<property name="hibernate.cache.redisson.my_query.eviction.max_entries" value="10000" />
<property name="hibernate.cache.redisson.my_query.expiration.time_to_live" value="600000" />
<property name="hibernate.cache.redisson.my_query.expiration.max_idle_time" value="300000" />

<property name="hibernate.cache.redisson.my_query.localcache.max_idle_time" value="300000" />
<property name="hibernate.cache.redisson.my_query.localcache.time_to_live" value="300000" />
<property name="hibernate.cache.redisson.my_query.localcache.eviction_policy" value="LRU" />
<property name="hibernate.cache.redisson.my_query.localcache.sync_strategy" value="INVALIDATE" />
<property name="hibernate.cache.redisson.my_query.localcache.reconnection_strategy" value="CLEAR" />
<property name="hibernate.cache.redisson.my_query.localcache.size" value="5000" />
N
Nikita Koksharov 已提交
234
```