Use Redis cache in Hibernate

Hibernate is an open source Java programming language, object / relational mapping framework. Hibernate's goal is to help developers get rid of many tedious manual data processing tasks. Hibernate can create a mapping between data types between Java classes and database tables as well as Java and SQL.

Any non-trivial application to process large amounts of data have to rely on caching and other techniques to improve efficiency. Cache is a frequently accessed data stored in the buffer by using strategies to improve program performance applications. By reducing the number of data stored in the database and requested position closer to the CPU, the cache can significantly improve the speed of the application.

In this article, we will examine how to use Redisson (for accessing memory data structure stored Redis of Java wrapper) to perform caching in Hibernate.

The first level two cache Hibernate

Hibernate uses multi-level caching scheme. The first stage is mandatory, is enabled by default, and the second stage is optional.

Cache

A cache (also referred to as L1 cache) and the Hibernate Session object associated with a Java object represents a connection between the application and the SQL database. This means that the premise of the existence of the session, only the first-level cache available. Each first-level cache only by the Session object associated with access.

For the first time from the database query entity that will be stored in the first-level cache associated with that session. During the same session for this database to retrieve any subsequent queries entities are entities from the cache instead.

Secondary cache

Secondary cache (also called the L2 cache) is disabled by default, but can be enabled by modifying configuration settings Hibernate. The cache and Hibernate SessionFactory associated with an object, to be mainly used for storing persistent data between Session. Before looking for the second-level cache, the application will always be in the first-level cache is searched for the presence of a given entity.

Hibernate cache further have a third type: query cache for storing a specific database query result. When you need to use the same parameters to run the same query multiple times, it is useful

Hibernate secondary cache in several different implementations, including Ehcache and OSCache. In the remainder of this article, we will explore other options for Hibernate second-level cache: Redisson, which allows Redis as a Hibernate cache

How to use Hibernate and Redis caching

Source Case - RedissonCacheRegionFactory

Selection Policy

Redisson is Redis client in Java, which contains many Java object that implements and services, including Hibernate cache. Redisson supports all four Hibernate caching strategy:

  • READ_ONLY: only entity within the cache does not change.
  • NONSTRICT_READ_WRITE: After the transaction to modify the database entities, update the cache. We can not guarantee strong consistency, but you can guarantee eventual consistency.
  • READ_WRITE: through the use of "soft" lock to ensure strong consistency, these locks will maintain control of the entity, until the transaction is completed.
  • Transactional: By using distributed XA transactions to ensure data integrity. Be sure to update committed or rolled back to the database and cache.

Redisson offers a variety of Hibernate CacheFactories, including those that support local cache. If you plan to major Hibernate cache for read operations, or if you do not want to make too many network round trips, the local cache is a sensible solution.

The RedissonRegionFactory tool to achieve the basic Hibernate cache, RedissonLocalCachedRegionFactory (in Redisson PRO version available) is achieved with a local cache Hibernate cache support.

Configuration dependency

redisson-hibernate or can use Maven dependency Gradle will easily integrate into your project. For JDK 1.8, Maven is set to:

<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 - v5.4.x -->
    <artifactId>redisson-hibernate-53</artifactId>
    <version>3.10.2</version>
</dependency>

And Gradle settings are:

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

To CacheFactory in Redisson defined in, insert the appropriate attributes in Hibernate configuration:

<!-- Redisson Region Cache factory -->
<property name="hibernate.cache.region.factory_class" value="org.redisson.hibernate.RedissonRegionFactory" />
<!-- or -->
<property name="hibernate.cache.region.factory_class" value="org.redisson.hibernate.RedissonLocalCachedRegionFactory" />

Active configuration

You also need to activate Hibernate second-level cache and specify Redisson configuration file:

<!-- 2nd level cache activation -->
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.cache.use_query_cache" value="true" />
<!-- Redisson YAML config (located in filesystem or classpath) -->
<property name="hibernate.cache.redisson.config" value="/redisson.yaml" />
<!-- Redisson JSON config (located in filesystem or classpath) -->
<property name="hibernate.cache.redisson.config" value="/redisson.json" />

Cache configuration parameters

Redisson allows you to change a number of different important parameters for the Hibernate cache, including:

  • The maximum size of the cache Redis
  • Redis survival time for each cache entry
  • Redis maximum idle time for each cache entry
  • The maximum size of the local cache
  • Survival time for each local cache entry

Each local cache entry maximum idle time

Once it reaches the maximum cache size (LFU, LRU, SOFT, WEAK or NONE), on the local cache eviction policy

Examples of policy changes across all synchronized to a local cache (INVALIDATE, UPDATE, or NONE)

Reload reconnect the missing strategy update after a connection failure (CLEAR, LOAD, or NONE)

Hibernate secondary cache - RedissonCacheRegionFactory

mPaaS (Microservice PaaS) micro-services development platform based on SpringBoot2.x, SpringCloud and front and rear end of the separation of micro multi-tenant service system architecture development platform

Like reading Spring, SpringBoot, SpringCloud such as the underlying source code can focus the next mPass micro-services development platform and look forward to your comments!

Guess you like

Origin www.cnblogs.com/lishangzhi/p/11758427.html