Skip to content

Commit 56c51ed

Browse files
committed
Implement caching
1 parent 134e6ba commit 56c51ed

5 files changed

Lines changed: 34 additions & 0 deletions

File tree

data-jpa-hibernate-cache/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ repositories {
1818
}
1919

2020
dependencies {
21+
implementation("org.ehcache:ehcache::jakarta")
22+
implementation("org.hibernate.orm:hibernate-jcache")
2123
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
2224
runtimeOnly("org.postgresql:postgresql")
2325
testImplementation("org.springframework.boot:spring-boot-starter-test")

data-jpa-hibernate-cache/src/main/java/zin/rashidi/datajpa/hibernatecache/customer/Customer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
import jakarta.persistence.Entity;
44
import jakarta.persistence.GeneratedValue;
55
import jakarta.persistence.Id;
6+
import org.hibernate.annotations.Cache;
7+
8+
import static org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE;
69

710
/**
811
* @author Rashidi Zin
912
*/
1013
@Entity
14+
@Cache(usage = READ_WRITE, region = "customer")
1115
class Customer {
1216

1317
@Id
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
spring.application.name=data-jpa-hibernate-cache
2+
spring.jpa.properties.hibernate.cache.region.factory_class=jcache
3+
spring.jpa.properties.hibernate.javax.cache.uri=/ehcache.xml
4+
spring.jpa.properties.hibernate.javax.cache.provider=org.ehcache.jsr107.EhcacheCachingProvider
5+
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<config xmlns='http://www.ehcache.org/v3'>
2+
<cache alias="customer">
3+
<resources>
4+
<offheap unit="MB">10</offheap>
5+
</resources>
6+
</cache>
7+
</config>

data-jpa-hibernate-cache/src/test/java/zin/rashidi/datajpa/hibernatecache/customer/CustomerRepositoryTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package zin.rashidi.datajpa.hibernatecache.customer;
22

3+
import jakarta.persistence.EntityManagerFactory;
4+
import org.junit.jupiter.api.DisplayName;
5+
import org.junit.jupiter.api.Test;
36
import org.springframework.beans.factory.annotation.Autowired;
47
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
58
import org.springframework.context.annotation.Import;
9+
import org.springframework.test.context.jdbc.Sql;
610
import zin.rashidi.datajpa.hibernatecache.TestcontainersConfiguration;
711

12+
import static org.assertj.core.api.Assertions.assertThat;
13+
814
/**
915
* @author Rashidi Zin
1016
*/
@@ -15,4 +21,15 @@ class CustomerRepositoryTests {
1521
@Autowired
1622
private CustomerRepository customers;
1723

24+
@Autowired
25+
private EntityManagerFactory entityManagerFactory;
26+
27+
@Test
28+
@DisplayName("Given Customer is configured for caching When I make first retrieval Then its cache is available")
29+
@Sql(statements = "INSERT INTO customer (id, name) VALUES (1, 'Rashidi Zin')")
30+
void findById() {
31+
customers.findById(1L).orElseThrow();
32+
33+
assertThat(entityManagerFactory.getCache().contains(Customer.class, 1L)).isTrue();
34+
}
1835
}

0 commit comments

Comments
 (0)