Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#376 matrixansicht laedt sehr langsam introduce caching #383

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,18 @@
<version>0.2.0</version>
<scope>provided</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-jcache -->
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-jcache</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.ehcache/ehcache -->
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<classifier>jakarta</classifier>
</dependency>
</dependencies>

<build>
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/tb/SalatApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableJpaRepositories
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.tb.common.configuration;

import org.hibernate.cache.jcache.ConfigSettings;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.orm.jpa.HibernatePropertiesCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;

import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.spi.CachingProvider;
import java.io.IOException;
import java.net.URI;


@Configuration
public class HibernateSecondLevelCacheConfiguration {

@Bean
public HibernatePropertiesCustomizer hibernateSecondLevelCacheCustomizer(ResourceLoader resourceLoader,
@Value("${salat.cache.max-entries}") String maxCacheEntries,
@Value("${salat.cache.expiry-tti}") String expiryTti) {
return (properties) -> {
try {
URI uri = resourceLoader.getResource("classpath:ehcache.xml").getURI();
CachingProvider cachingProvider = Caching.getCachingProvider();
System.setProperty("EHCACHE_EXPIRY_TTI", expiryTti);
System.setProperty("EHCACHE_RESOURCES_HEAP_ENTRIES", maxCacheEntries);
CacheManager cacheManager = cachingProvider.getCacheManager(uri, cachingProvider.getDefaultClassLoader());
properties.put(ConfigSettings.CACHE_MANAGER, cacheManager);
} catch (IOException e) {
throw new RuntimeException(e);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,40 @@
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

import jakarta.persistence.QueryHint;
import org.hibernate.jpa.HibernateHints;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import org.tb.dailyreport.domain.Timereport;

@Repository
public interface TimereportRepository extends CrudRepository<Timereport, Long>, JpaSpecificationExecutor<Timereport> {

@QueryHints(value = {
@QueryHint(name = HibernateHints.HINT_CACHEABLE, value = "true"),
@QueryHint(name = HibernateHints.HINT_CACHE_REGION, value = "TimereportRepository.findAllByEmployeecontractIdAndReferencedayRefdate")
}
)
List<Timereport> findAllByEmployeecontractIdAndReferencedayRefdate(long employeecontractId, LocalDate refDate);

List<Timereport> findAllByEmployeecontractIdAndReferencedayRefdateIsGreaterThanEqual(long employeecontractId, LocalDate refDate);

List<Timereport> findAllByEmployeecontractIdAndStatusAndReferencedayRefdateIsLessThanEqual(long employeecontractId, String status, LocalDate date);

@QueryHints(value = {
@QueryHint(name = HibernateHints.HINT_CACHEABLE, value = "true"),
@QueryHint(name = HibernateHints.HINT_CACHE_REGION, value = "TimereportRepository.findAllByEmployeecontractIdAndReferencedayBetween")
}
)
@Query("""
select t from Timereport t
where t.employeecontract.id = :employeecontractId and
t.referenceday.refdate >= :begin and t.referenceday.refdate <= :end
where t.employeecontract.id = :employeecontractId
and t.referenceday.refdate >= :begin
and t.referenceday.refdate <= :end
order by t.employeecontract.employee.sign asc,
t.referenceday.refdate asc,
t.employeeorder.suborder.customerorder.sign asc,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@
import java.util.List;
import java.util.Optional;

import jakarta.persistence.QueryHint;
import org.hibernate.jpa.HibernateHints;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import org.tb.dailyreport.domain.Workingday;

@Repository
public interface WorkingdayRepository extends CrudRepository<Workingday, Long> {

@QueryHints(value = {
@QueryHint(name = HibernateHints.HINT_CACHEABLE, value = "true"),
@QueryHint(name = HibernateHints.HINT_CACHE_REGION, value = "WorkingdayRepository.findByRefdayAndEmployeecontractId")
}
)
Optional<Workingday> findByRefdayAndEmployeecontractId(LocalDate refday, long employeecontractId);

List<Workingday> findAllByEmployeecontractId(long employeeContractId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

import jakarta.persistence.QueryHint;
import org.hibernate.jpa.HibernateHints;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;
Expand All @@ -17,6 +21,11 @@ public interface EmployeecontractRepository extends PagingAndSortingRepository<E
@Query("select e from Employeecontract e where e.employee.id = :employeeId and e.validFrom <= :validAt and (e.validUntil >= :validAt or e.validUntil is null)")
Optional<Employeecontract> findByEmployeeIdAndValidAt(long employeeId, LocalDate validAt);

@QueryHints(value = {
@QueryHint(name = HibernateHints.HINT_CACHEABLE, value = "true"),
@QueryHint(name = HibernateHints.HINT_CACHE_REGION, value = "EmployeecontractRepository.findAllValidAtAndNotHidden")
}
)
@Query("""
select e from Employeecontract e where (e.hide = false or e.hide is null)
or (e.validFrom <= :date and (e.validUntil >= :date or e.validUntil is null))
Expand Down
14 changes: 13 additions & 1 deletion src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,22 @@ spring:
hibernate:
format_sql: true
show_sql: false

use_sql_comments: true
generate_statistics: false
cache:
use_second_level_cache: true
use_query_cache: true
region:
factory_class: org.hibernate.cache.jcache.JCacheRegionFactory
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
ddl-auto: validate
open-in-view: true
cache:
jcache:
config: classpath:ehcache.xml
web:
resources:
chain:
Expand Down Expand Up @@ -67,6 +76,9 @@ salat:
mail-host: localhost
auth-service:
cache-expiry: 1s
cache:
max-entries: 1000
expiry-tti: 10
springdoc:
swagger-ui:
path: /api/doc/
Expand Down
22 changes: 22 additions & 0 deletions src/main/resources/ehcache.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<config
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3'
xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
xsi:schemaLocation="
http://www.ehcache.org/v3 https://www.ehcache.org/schema/ehcache-core-3.9.xsd
http://www.ehcache.org/v3/jsr107 https://www.ehcache.org/schema/ehcache-107-ext-3.9.xsd">

<service>
<jsr107:defaults enable-management="true" enable-statistics="true" default-template="defaultTemplate"/>
</service>

<cache-template name="defaultTemplate">
<expiry>
<tti unit="minutes">${EHCACHE_EXPIRY_TTI}</tti>
</expiry>
<resources>
<heap unit="entries">${EHCACHE_RESOURCES_HEAP_ENTRIES}</heap>
</resources>
</cache-template>
</config>
Loading