README.md 15.4 KB
Newer Older
1 2 3
Redis based Hibernate Cache implementation
===

N
Nikita Koksharov 已提交
4
Implements Hibernate 2nd level Cache provider based on Redisson.  
N
Nikita Koksharov 已提交
5
Supports all Hibernate cache strategies: `READ_ONLY`, `NONSTRICT_READ_WRITE`, `READ_WRITE` and `TRANSACTIONAL`.  
N
Nikita Koksharov 已提交
6
It's recommended to use FST or Snappy as [codec](https://github.com/redisson/redisson/wiki/4.-data-serialization).
7

N
Nikita Koksharov 已提交
8
Compatible with Hibernate 4.x, 5.x
9

N
Nikita Koksharov 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Redisson provides various Hibernate Cache factories including those with features below:

**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.  

**data partitioning** - data partitioning in cluster mode. It allows to scale available memory, read/write operations and entry eviction process for individual Hibernate Cache instance in Redis cluster.  

Below is the list of all available factories with local cache and/or data partitioning support:  

|Class name | Local cache <br/> support | Data partitioning <br/> support |
| ------------- | ------------- | ------------|
|RedissonRegionFactory<br/>&nbsp; | No | No |
|RedissonLocalCachedRegionFactory<br/><sub><i>available only in [Redisson PRO](http://redisson.pro) edition</i></sub>  | **Yes** | No |
|RedissonClusteredRegionFactory<br/><sub><i>available only in [Redisson PRO](http://redisson.pro) edition</i></sub> | No | **Yes** |
|RedissonClusteredLocalCachedRegionFactory<br/><sub><i>available only in [Redisson PRO](http://redisson.pro) edition</i></sub> | **Yes** | **Yes** |

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
Usage
===

### 1.  Add `redisson-hibernate` dependency into your project:

1. __For JDK 1.8+__  

     Maven
     ```xml
     <dependency>
         <groupId>org.redisson</groupId>
         <!-- for Hibernate v4.x -->
         <artifactId>redisson-hibernate-4</artifactId>
         <!-- for Hibernate v5.0.x - v5.1.x -->
         <artifactId>redisson-hibernate-5</artifactId>
         <!-- for Hibernate v5.2.x -->
         <artifactId>redisson-hibernate-52</artifactId>
         <!-- for Hibernate v5.3.x -->
         <artifactId>redisson-hibernate-53</artifactId>
         <version>3.10.0</version>
     </dependency>
     ```
     Gradle

     ```java
     // for Hibernate v4.x
     compile 'org.redisson:redisson-hibernate-4:3.10.0'
     // for Hibernate v5.0.x - v5.1.x
     compile 'org.redisson:redisson-hibernate-5:3.10.0'
     // for Hibernate v5.2.x
     compile 'org.redisson:redisson-hibernate-52:3.10.0'
     // for Hibernate v5.3.x
     compile 'org.redisson:redisson-hibernate-53:3.10.0'
     ```  

2. __For JDK 1.6+__  

     Maven
     ```xml
     <dependency>
         <groupId>org.redisson</groupId>
         <!-- for Hibernate v4.x -->
         <artifactId>redisson-hibernate-4</artifactId>
         <!-- for Hibernate v5.0.x - v5.1.x -->
         <artifactId>redisson-hibernate-5</artifactId>
         <version>2.15.0</version>
     </dependency>
     ```
     Gradle

     ```java
     // for Hibernate v4.x
     compile 'org.redisson:redisson-hibernate-4:2.15.0'
     // for Hibernate v5.0.x - v5.1.x
     compile 'org.redisson:redisson-hibernate-5:2.15.0'
     ``` 


### 2. Specify hibernate cache settings

N
Nikita Koksharov 已提交
85
```xml
86 87 88 89 90 91
<!-- 2nd level cache activation -->
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.cache.use_query_cache" value="true" />

<!-- Redisson Hibernate Cache factory -->
<property name="hibernate.cache.region.factory_class" value="org.redisson.hibernate.RedissonRegionFactory" />
N
Nikita Koksharov 已提交
92 93 94 95 96 97 98
<!-- 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" />

99 100 101 102 103 104
<!-- Redisson YAML config (searched in filesystem and classpath) -->
<property name="hibernate.cache.redisson.config" value="/redisson.yaml" />
<!-- Redisson JSON config (searched in filesystem and classpath) -->
<property name="hibernate.cache.redisson.config" value="/redisson.json" />
```

N
Nikita Koksharov 已提交
105
#### Redisson allows to define follow cache settings per entity, collection, naturalid, query and timestamp regions:  
N
Nikita Koksharov 已提交
106

N
Nikita Koksharov 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
`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  

`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  

`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  

`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  

`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  

`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  

`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 已提交
123

N
Nikita Koksharov 已提交
124
Default value: INVALIDATE
N
Nikita Koksharov 已提交
125

N
Nikita Koksharov 已提交
126 127 128 129 130 131 132 133 134 135
`hibernate.cache.redisson.[REGION_NAME].localcache.reconnection_strategy` - reconnection strategy used to load missed updates 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  

Default value: NONE  

`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._
136

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

N
Nikita Koksharov 已提交
139 140 141 142 143 144
```xml
<!-- cache definition applied to all caches in entity region -->
<property name="hibernate.cache.redisson.entry.eviction.max_entries" value="10000" />
<property name="hibernate.cache.redisson.entry.expiration.time_to_live" value="600000" />
<property name="hibernate.cache.redisson.entry.expiration.max_idle_time" value="300000" />

N
Nikita Koksharov 已提交
145 146 147 148 149 150 151
<property name="hibernate.cache.redisson.entry.localcache.max_idle_time" value="300000" />
<property name="hibernate.cache.redisson.entry.localcache.time_to_live" value="300000" />
<property name="hibernate.cache.redisson.entry.localcache.eviction_policy" value="LRU" />
<property name="hibernate.cache.redisson.entry.localcache.sync_strategy" value="INVALIDATE" />
<property name="hibernate.cache.redisson.entry.localcache.reconnection_strategy" value="CLEAR" />
<property name="hibernate.cache.redisson.entry.localcache.size" value="5000" />

N
Nikita Koksharov 已提交
152 153 154 155 156
<!-- 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 已提交
157 158 159 160 161 162 163
<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 已提交
164 165 166 167 168
<!-- 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 已提交
169 170 171 172 173 174 175
<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 已提交
176 177 178 179 180
<!-- 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 已提交
181 182 183 184 185 186 187
<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 已提交
188 189 190 191
<!-- 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 已提交
192 193 194 195 196 197 198

<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 已提交
199 200
```

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

N
Nikita Koksharov 已提交
203
```xml
204 205 206 207 208
<!-- 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 已提交
209 210 211 212 213 214 215
<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" />

216 217 218 219 220
<!-- 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 已提交
221 222 223 224 225 226 227
<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 已提交
228
<!-- cache definition for naturalid region. Suffixed by ##NaturalId. Example region name: "my_object" -->
229 230 231 232
<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 已提交
233 234 235 236 237 238
<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" />
239

N
Nikita Koksharov 已提交
240 241 242 243 244 245 246 247 248 249 250
<!-- 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 已提交
251
```