-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
67 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-rc-2-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-rc-3-bin.zip | ||
networkTimeout=10000 | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
The [Hibernate ORM] can be configured to use a [second-level cache] to reduce the number of accesses | ||
to the database by caching data in memory to be shared between sessions. | ||
|
||
In [hibernate.conf](src/main/resources/hibernate.properties) specify the JCache provider and enable | ||
the second-level cache. | ||
|
||
```ini | ||
hibernate.cache.use_second_level_cache=true | ||
hibernate.cache.region.factory_class=jcache | ||
hibernate.javax.cache.provider=com.github.benmanes.caffeine.jcache.spi.CaffeineCachingProvider | ||
``` | ||
|
||
The caches are configured in Caffeine's [application.conf](src/main/resources/application.conf) | ||
following the [reference format](../../jcache/src/main/resources/reference.conf). The default file | ||
path may be overridden by setting the `hibernate.javax.cache.uri` in the previous step. | ||
|
||
```hocon | ||
caffeine.jcache { | ||
default { | ||
policy.maximum.size = 500 | ||
} | ||
# Hibernate framework caches | ||
default-query-results-region {} | ||
default-update-timestamps-region {} | ||
# Hibernate application caches | ||
com.github.benmanes.caffeine.examples.hibernate.User {} | ||
} | ||
``` | ||
|
||
Hibernate will then manage the cache to transparently avoid database calls. | ||
|
||
```java | ||
// miss on first access | ||
sessionFactory.fromSession(session -> session.get(User.class, id)); | ||
assertThat(sessionFactory.getStatistics().getSecondLevelCacheMissCount()).isEqualTo(1); | ||
|
||
// hit on lookup | ||
sessionFactory.fromSession(session -> session.get(User.class, id)); | ||
assertThat(sessionFactory.getStatistics().getSecondLevelCacheHitCount()).isEqualTo(1); | ||
``` | ||
|
||
[Hibernate ORM]: https://hibernate.org/orm/ | ||
[second-level cache]: https://docs.jboss.org/hibernate/orm/6.3/introduction/html_single/Hibernate_Introduction.html#second-level-cache-configuration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-rc-2-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-rc-3-bin.zip | ||
networkTimeout=10000 | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters