본문 바로가기

개발/java_spring

[Spring boot] JPA hibernate second level cache with redisson

반응형

이미 조회된 DB데이터의 접근 비용을 줄이기 위해 데이터를 캐시하여 retrieve 한다.

https://redisson.org/glossary/hibernate-second-level-cache.html

 

What is the Hibernate second-level cache? | Redisson

Part of Hibernate’s efficiency comes from the use of a component called the “second-level cache” or “L2 cache.” So what is the Hibernate second-level cache, and how should you use it?

redisson.org

level2 cache는 큰사이즈의 데이터를 저장하지만 level1 cache보다 느리다고 함.

level2 cache를 사용하기 위해선 설정이 필요하다.

설정은 아래 참고.

https://github.com/redisson/redisson/tree/master/redisson-hibernate

 

GitHub - redisson/redisson: Redisson - Redis Java client with features of In-Memory Data Grid. Over 50 Redis based Java objects

Redisson - Redis Java client with features of In-Memory Data Grid. Over 50 Redis based Java objects and services: Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map...

github.com

아래는 실서버 적용 설정

springboot, gradle

 

build.gradle

implementation 'org.hibernate:hibernate-core:5.6.14.Final'
implementation 'org.redisson:redisson-hibernate-6:3.18.1'

 

application.properties

#jpa hibernate cache
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.redisson.hibernate.RedissonRegionFactory
spring.jpa.properties.hibernate.cache.redisson.fallback=true
spring.jpa.properties.hibernate.cache.redisson.config=redisson.yml
spring.jpa.properties.hibernate.cache.redisson.entity.expiration.time_to_live=600000
spring.jpa.properties.hibernate.cache.redisson.entity.expiration.max_idle_time=300000
spring.jpa.properties.hibernate.cache.redisson.query.eviction.max_entries=10000
spring.jpa.properties.hibernate.cache.redisson.query.expiration.time_to_live=600000
spring.jpa.properties.hibernate.cache.redisson.query.expiration.max_idle_time=300000

 

redisson.yml

singleServerConfig:
  address: "redis://localhost:6379"
  password: "password"

 

Test.java Entity class

@Entity
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "test")
public class Test {
	.
    .
    .
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "test1")
    @One to Many
    private Test1 test1;
}

READ_WRITE 로 설정하면 DB의 해당 개체가 수정되면 즉시 수정이 반영되어 데이터 일관성이 유지된다고 함.

반응형