Skip to content

debop/hibernate-redis

Repository files navigation

hibernate-redis

Maven Central Build Status

hibernate (4.x, 5.1.x, 5.2.x) 2nd level cache provider using redis server 3.x. with Redisson 2.3.x

Reduce cache size by Redisson SnappyCodec (see snappy-java, Fast-Serialization)

Note

From 2.2.1 onwards Hibernate region naming (hibernate.cache.region_prefix) has been simplified to "hibernate".

hibernate-core 5.2.x based on Java 8, use hibernate-redis 2.2.0 or higher

Region factory for hibernate 5.2.x is hibernate.redis.cache.hibernate52.SingletonRedisRegionFactory

Setup

Maven Repository

add dependency

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

Optional dependencies. Redisson support various codec (serializer, compression). you can choose other codec. see Redisson Help.

<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson</artifactId>
    <version>${redisson.version}</version>
</dependency>
<dependency>
    <groupId>de.ruedigermoeller</groupId>
    <artifactId>fst</artifactId>
    <version>${fst.version}</version>
</dependency>
<dependency>
    <groupId>org.xerial.snappy</groupId>
    <artifactId>snappy-java</artifactId>
    <version>${snappy-java.version}</version>
</dependency>
Setup hibernate configuration

Setup hibernate configuration (Note package name for hibernate 4 / hibernate 5 / hibernate52)

// Secondary Cache
props.put(Environment.USE_SECOND_LEVEL_CACHE, true);
props.put(Environment.USE_QUERY_CACHE, true);
props.put(Environment.CACHE_REGION_FACTORY, org.hibernate.cache.redis.hibernate52.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");

// Hibernate 4
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

 ##########################################################
 #
 # properities for hibernate-redis
 #
 ##########################################################

 # Redisson configuration file
 redisson-config=conf/redisson.yaml

 # Cache Expiry settings
 # 'hibernate' is second cache prefix
 # 'common', 'account' is actual region name
 redis.expiryInSeconds.default=120
 redis.expiryInSeconds.hibernate.common=0
 redis.expiryInSeconds.hibernate.account=1200

sample for Redisson configuration (see more samples )

# redisson configuration for redis servers
# see : https://github.com/mrniko/redisson/wiki/2.-Configuration

singleServerConfig:
  idleConnectionTimeout: 10000
  pingTimeout: 1000
  connectTimeout: 1000
  timeout: 1000
  retryAttempts: 1
  retryInterval: 1000
  reconnectionTimeout: 3000
  failedAttempts: 1
  password: null
  subscriptionsPerConnection: 5
  clientName: null
  address: "redis://127.0.0.1:6379"
  subscriptionConnectionMinimumIdleSize: 1
  subscriptionConnectionPoolSize: 25
  connectionMinimumIdleSize: 5
  connectionPoolSize: 100
  database: 0
  dnsMonitoring: false
  dnsMonitoringInterval: 5000
threads: 0
# Codec
codec: !<org.redisson.codec.SnappyCodec> {}
useLinuxNativeEpoll: false
eventLoopGroup: null

Hibernate configuration via Spring Application property files

In Spring applications, the hibernate- and hibernate-redis configuration represented above can be configured within Spring application property files like below.

spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.use_query_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.redis.hibernate52.SingletonRedisRegionFactory
spring.jpa.properties.hibernate.cache.region_prefix=hibernate

spring.jpa.properties.hibernate.cache.use_structured_entries=true
spring.jpa.properties.hibernate.generate_statistics=true

spring.jpa.properties.redisson-config=classpath:conf/redisson.yaml

spring.jpa.properties.redis.expiryInSeconds.default=120
spring.jpa.properties.redis.expiryInSeconds.hibernate.common=0
spring.jpa.properties.redis.expiryInSeconds.hibernate.account=1200

Setup hibernate entity to use cache

add @org.hibernate.annotations.Cache annotation to Entity class like this

@Entity
@Cache(region="common", usage = CacheConcurrencyStrategy.READ_WRITE)  // or @Cacheable(true) for JPA
@Getter
@Setter
public class Item implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    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

see hibernate-examples module