Skip to content

Commit

Permalink
Added ehcache for hibernate L2 caching
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlau committed Jun 25, 2013
1 parent b557cbe commit 166a691
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 2 deletions.
6 changes: 6 additions & 0 deletions fixture-data/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<usedDependency>com.mchange:c3p0</usedDependency>
<usedDependency>io.fixture:fixture-annotation</usedDependency>
<usedDependency>io.fixture:fixture-data-test</usedDependency>
<usedDependency>org.hibernate:hibernate-ehcache</usedDependency>
<usedDependency>org.hibernate:hibernate-entitymanager</usedDependency>
<usedDependency>org.liquibase:liquibase-core</usedDependency>
</usedDependencies>
Expand Down Expand Up @@ -89,6 +90,11 @@
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.fixture.data.model

import java.util.UUID
import javax.persistence.Cacheable
import javax.persistence.Column
import javax.persistence.GeneratedValue
import javax.persistence.Id
Expand All @@ -10,6 +11,7 @@ import org.hibernate.annotations.GenericGenerator
import org.hibernate.annotations.Type
import org.springframework.data.domain.Persistable

[Cacheable]
[MappedSuperclass]
open class BasePersistable: Persistable<UUID> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,34 @@ import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param
import javax.persistence.QueryHint
import org.springframework.data.jpa.repository.QueryHints

public trait GroupRepository: JpaRepository<Group, UUID> {

[Modifying]
[Query("DELETE FROM Group g WHERE g.name = :name")]
[QueryHints(array(
QueryHint(name = "org.hibernate.cacheable", value = "true")
))]
fun delete([Param("name")] name: String)

[Query("SELECT g.name FROM Group g")]
[QueryHints(array(
QueryHint(name = "org.hibernate.cacheable", value = "true")
))]
fun findAllNames(): List<String>

[Query("SELECT u.username FROM User u INNER JOIN u.groups g WHERE g.name = :name")]
[QueryHints(array(
QueryHint(name = "org.hibernate.cacheable", value = "true")
))]
fun findAllUsernames([Param("name")] name: String): List<String>

[Query("SELECT g FROM Group g WHERE g.name = :name")]
[QueryHints(array(
QueryHint(name = "org.hibernate.cacheable", value = "true")
))]
fun findOne([Param("name")] name: String): Group?

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,22 @@ import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param
import javax.persistence.QueryHint
import org.springframework.data.jpa.repository.QueryHints

public trait PersistentLoginRepository: JpaRepository<PersistentLogin, UUID> {

[Modifying]
[Query("DELETE FROM PersistentLogin pl WHERE pl.user = (SELECT u FROM User u WHERE u.username = :username)")]
[QueryHints(array(
QueryHint(name = "org.hibernate.cacheable", value = "true")
))]
fun deleteAllForUsername([Param("username")] username: String)

[Query("SELECT pl FROM PersistentLogin pl WHERE pl.series = :series")]
[QueryHints(array(
QueryHint(name = "org.hibernate.cacheable", value = "true")
))]
fun findOne([Param("series")] series: String): PersistentLogin?

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,28 @@ import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.repository.query.Param
import javax.persistence.QueryHint
import org.springframework.data.jpa.repository.QueryHints

public trait UserRepository : JpaRepository<User, UUID> {

[Modifying]
[Query("DELETE FROM User u WHERE u.username = :username")]
[QueryHints(array(
QueryHint(name = "org.hibernate.cacheable", value = "true")
))]
fun delete([Param("username")] username: String)

[Query("SELECT CASE WHEN (count(u) > 0) THEN true ELSE false END FROM User u WHERE u.username = :username")]
[QueryHints(array(
QueryHint(name = "org.hibernate.cacheable", value = "true")
))]
fun exists([Param("username")] username: String): Boolean

[Query("SELECT u FROM User u WHERE u.username = :username")]
[QueryHints(array(
QueryHint(name = "org.hibernate.cacheable", value = "true")
))]
fun findOne([Param("username")] username: String): User?

}
78 changes: 78 additions & 0 deletions fixture-data/src/main/resources/META-INF/ehcache/ehcache.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<ehcache dynamicConfig="true"
monitoring="autodetect"
updateCheck="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

<diskStore path="java.io.tmpdir"/>

<defaultCache diskExpiryThreadIntervalSeconds="120"
diskPersistent="false"
eternal="false"
maxElementsInMemory="16384"
maxElementsOnDisk="1048576"
memoryStoreEvictionPolicy="LRU"
overflowToDisk="true"
timeToIdleSeconds="120"
timeToLiveSeconds="120"/>

<cache name="io.fixture.data.model.Group"
diskPersistent="false"
eternal="false"
maxElementsInMemory="16384"
memoryStoreEvictionPolicy="LRU"
overflowToDisk="false"
timeToIdleSeconds="240"
timeToLiveSeconds="480"/>

<cache name="io.fixture.data.model.PersistentLogin"
diskPersistent="false"
eternal="false"
maxElementsInMemory="16384"
memoryStoreEvictionPolicy="LRU"
overflowToDisk="false"
statistics="true"
timeToIdleSeconds="240"
timeToLiveSeconds="480"/>

<cache name="org.hibernate.cache.internal.StandardQueryCache"
diskPersistent="false"
eternal="false"
maxElementsInMemory="16384"
memoryStoreEvictionPolicy="LRU"
overflowToDisk="false"
statistics="true"
timeToIdleSeconds="240"
timeToLiveSeconds="480"/>

<cache name="org.hibernate.cache.spi.UpdateTimestampsCache"
diskPersistent="false"
eternal="false"
maxElementsInMemory="16384"
memoryStoreEvictionPolicy="LRU"
overflowToDisk="false"
statistics="true"
timeToIdleSeconds="240"
timeToLiveSeconds="480"/>

<cache name="io.fixture.data.model.User"
diskPersistent="false"
eternal="false"
maxElementsInMemory="16384"
memoryStoreEvictionPolicy="LRU"
overflowToDisk="false"
timeToIdleSeconds="240"
timeToLiveSeconds="480"/>

<cache name="io.fixture.data.model.UserProfile"
diskPersistent="false"
eternal="false"
maxElementsInMemory="16384"
memoryStoreEvictionPolicy="LRU"
overflowToDisk="false"
statistics="true"
timeToIdleSeconds="240"
timeToLiveSeconds="480"/>

</ehcache>
13 changes: 11 additions & 2 deletions fixture-data/src/main/resources/META-INF/spring/fixture-data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,17 @@
depends-on="liquibase"
p:dataSource-ref="dataSource"
p:jpaVendorAdapter-ref="jpaVendorAdapter"
p:packagesToScan="io.fixture.data.model"
p:jpaProperties="hibernate.hbm2ddl.auto=validate"/>
p:packagesToScan="io.fixture.data.model">
<property name="jpaProperties">
<props>
<prop key="net.sf.ehcache.configurationResourceName">/META-INF/ehcache/ehcache.xml</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
</props>
</property>
</bean>

<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager"
Expand Down
5 changes: 5 additions & 0 deletions fixture-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,11 @@
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
Expand Down

0 comments on commit 166a691

Please sign in to comment.