Coder Social home page Coder Social logo

hibernate-redis's Introduction

hibernate-redis Build Status

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! try 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.debop</groupId>
    <artifactId>hibernate-redis</artifactId>
    <version>1.6.1</version>
</dependency>

add repository

<repositories>
    <repository>
        <id>debop-releases-bintray</id>
        <url>http://dl.bintray.com/debop/maven</url>
    </repository>
</repositories>

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

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

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

 # redis.timeout=2000
 # redis.password=

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

 # hiberante 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 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
    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.

hibernate-redis's People

Contributors

debop avatar stadia avatar apotapov avatar

Watchers

James Cloos avatar ourenyang avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.