hibernate-redis

Hibernate second level cache with Redis

License

License

The Apache Software License, Version 2.0
Categories

Categories

Redis Data Databases Hibernate ORM
GroupId

GroupId

com.github.hibernate-redis
ArtifactId

ArtifactId

hibernate-redis
Last Version

Last Version

1.6.8
Release Date

Release Date

Type

Type

jar
Description

Description

hibernate-redis
Hibernate second level cache with Redis
Project URL

Project URL

https://github.com/hibernate-redis/hibernate-redis
Source Code Management

Source Code Management

https://github.com/hibernate-redis/hibernate-redis

Download hibernate-redis

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.hibernate-redis/hibernate-redis/ -->
<dependency>
    <groupId>com.github.hibernate-redis</groupId>
    <artifactId>hibernate-redis</artifactId>
    <version>1.6.8</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.hibernate-redis/hibernate-redis/
implementation 'com.github.hibernate-redis:hibernate-redis:1.6.8'
// https://jarcasting.com/artifacts/com.github.hibernate-redis/hibernate-redis/
implementation ("com.github.hibernate-redis:hibernate-redis:1.6.8")
'com.github.hibernate-redis:hibernate-redis:jar:1.6.8'
<dependency org="com.github.hibernate-redis" name="hibernate-redis" rev="1.6.8">
  <artifact name="hibernate-redis" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.hibernate-redis', module='hibernate-redis', version='1.6.8')
)
libraryDependencies += "com.github.hibernate-redis" % "hibernate-redis" % "1.6.8"
[com.github.hibernate-redis/hibernate-redis "1.6.8"]

Dependencies

compile (6)

Group / Artifact Type Version
org.hibernate : hibernate-entitymanager jar 4.3.11.Final
redis.clients : jedis jar 2.8.2
de.ruedigermoeller : fst jar 2.45
org.xerial.snappy : snappy-java jar 1.1.2.1
org.slf4j : slf4j-api jar 1.7.7
org.hibernate : hibernate-core jar 4.3.11.Final

test (16)

Group / Artifact Type Version
junit : junit jar 4.11
com.h2database : h2 jar 1.3.175
org.springframework.data : spring-data-jpa jar 1.6.0.RELEASE
org.springframework : spring-test jar 4.2.5.RELEASE
mysql : mysql-connector-java jar 5.1.27
org.objenesis : objenesis jar 2.1
cglib : cglib-nodep jar 3.1
org.hibernate : hibernate-testing jar 4.3.11.Final
org.springframework : spring-context jar 4.2.5.RELEASE
org.springframework : spring-orm jar 4.2.5.RELEASE
org.easytesting : fest-assert jar 1.4
org.spockframework : spock-core jar 1.0-groovy-2.4
com.zaxxer : HikariCP jar 2.4.5
org.springframework : spring-beans jar 4.2.5.RELEASE
org.springframework : spring-tx jar 4.2.5.RELEASE
org.springframework : spring-core jar 4.2.5.RELEASE

Project Modules

There are no modules declared in this project.

hibernate-redis Maven Central Build Status Join the chat at https://gitter.im/hibernate-redis/hibernate-redis

Hibernate (4.2.x.Final, 4.3.x.Final) 2nd level cache using Redis server. with Jedis 2.4.1 or higher

Reduce cache size by Fast-Serialization and snappy-java. Thanks!

See serialization benchmark.

NOTE

Don't use Hibernate 4.3.2.Final, 4.2.9.Final!!! It has bug in CacheKey! Recommend use 4.3.4.Final or 4.2.10.Final

Hibernate 4.3.2.Final CacheKey eliminate entityOrRoleName property for reduce CacheKey size. if multiple entity cached in same region, can't figure out wanted entity.

Maven Repository

Add dependency

<dependency>
    <groupId>com.github.hibernate-redis</groupId>
    <artifactId>hibernate-redis</artifactId>
    <version>1.6.8</version>
</dependency>

Setup hibernate configuration

Setup hibernate configuration

// Secondary Cache
props.put(Environment.USE_SECOND_LEVEL_CACHE, true);
props.put(Environment.USE_QUERY_CACHE, true);
props.put(Environment.CACHE_REGION_FACTORY, SingletonRedisRegionFactory.class.getName());
props.put(Environment.CACHE_REGION_PREFIX, "hibernate");

// optional setting for second level cache statistics
props.setProperty(Environment.GENERATE_STATISTICS, "true");
props.setProperty(Environment.USE_STRUCTURED_CACHE, "true");

props.setProperty(Environment.TRANSACTION_STRATEGY, JdbcTransactionFactory.class.getName());

// configuration for Redis that used by hibernate
props.put(Environment.CACHE_PROVIDER_CONFIG, "hibernate-redis.properties");

Also same configuration for using Spring Framework or Spring Data JPA

Redis settings for hibernate-redis

Sample for hibernate-redis.properties

 ##########################################################
 #
 # properties for hibernate-redis
 #
 ##########################################################

 # Redis Server for hibernate 2nd cache
 redis.host=localhost
 redis.port=6379

 # use redis-sentinel cluster as opposed to a single redis server (use only if not using host/port)
 # redis.sentinels = host1:26379,host2:26379,host3:26379
 # redis.masterName = mymaster

 # redis.timeout=2000
 # redis.password=

 # database for hibernate cache
 # redis.database=0
 redis.database=1

 # Hibernate 2nd cache default expiry (seconds)
 redis.expiryInSeconds=120

 # expiry of hibernate.common region (seconds) // hibernate is prefix, region name is common
 redis.expiryInSeconds.hibernate.common=0

 # expiry of hibernate.account region (seconds) // hibernate is prefix, region name is account
 redis.expiryInSeconds.hibernate.account=1200

Setup Hibernate entity to use cache

Add @org.hibernate.annotations.Cache annotation to your Entity class

@Entity
@Cache(region="common", usage = CacheConcurrencyStrategy.READ_WRITE)  // or @Cacheable(true) for JPA
@Getter
@Setter
public class Item implements Serializable {
    @Id
    @GeneratedValue
    private Long id;

    private String name;

    private String description;

    private static final long serialVersionUID = -281066218676472922L;
}

How to monitor hibernate-cache is running

Run "redis-cli monitor" command in terminal. you can see putting cached items, retrieving cached items.

Sample code

Read HibernateCacheTest.java for more usage.

com.github.hibernate-redis

Versions

Version
1.6.8
1.6.7
1.6.6
1.6.4
1.6.3