Skip to content

Commit

Permalink
move eh cache directory to ${user.home}/RECOMBackend/ehcache
Browse files Browse the repository at this point in the history
tidy up, did some tests, cache statistics ep
  • Loading branch information
svencc committed Apr 13, 2024
1 parent 362bf8e commit a48a14c
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 21 deletions.
5 changes: 3 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# TODO LIST

# 1
* Clustering

* Clustering Villages
* add names to villages
* add general map names
* Save Aspect in rad value for smoother shading!
* draw on pixel buffer
* text
* lines
* polygons ...
* Save Aspect in rad value for smoother shading!


* integrate pipeline in commander!
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.recom.dto.cache;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Set;

@Data
@Schema
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CacheInfoDto {

@Schema()
private String name;
@Schema()
private Long size;
@Schema
private Set<Object> keys;
@Schema
private String stats;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.recom.dto.cache;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@Schema
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CacheStatisticsDto {

@Schema()
private List<CacheInfoDto> caches;

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.recom.api;

import com.recom.api.commons.HttpCommons;
import com.recom.dto.cache.CacheStatisticsDto;
import com.recom.event.event.sync.cache.CacheResetSyncEvent;
import com.recom.service.cache.CacheStatisticsProvider;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
Expand All @@ -17,6 +19,7 @@
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -29,6 +32,8 @@ public class CacheController {

@NonNull
private final ApplicationEventPublisher applicationEventPublisher;
@NonNull
private final CacheStatisticsProvider cacheStatisticsProvider;


@Operation(
Expand All @@ -51,4 +56,22 @@ public ResponseEntity<Void> deleteCache() {
.build();
}

@Operation(
summary = "Get Cache Statistics",
description = "Provides caffeine cache statistics",
security = @SecurityRequirement(name = HttpCommons.BEARER_AUTHENTICATION_REQUIREMENT)
)
@ApiResponses(value = {
@ApiResponse(responseCode = HttpCommons.OK_CODE, description = HttpCommons.OK),
@ApiResponse(responseCode = HttpCommons.UNAUTHORIZED_CODE, description = HttpCommons.UNAUTHORIZED, content = @Content())
})
@GetMapping(path = "/statistics", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<CacheStatisticsDto> provide() {
log.debug("Requested GET /api/v1/cache/statistics");

return ResponseEntity.status(HttpStatus.OK)
.cacheControl(CacheControl.noCache())
.body(cacheStatisticsProvider.provide());
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.recom.event.listener;

import com.recom.entity.Account;
import com.recom.event.BaseRecomEventListener;
import com.recom.event.event.async.cache.CacheResetAsyncEvent;
import com.recom.event.event.sync.cache.CacheResetSyncEvent;
import com.recom.service.dbcached.DBCachedManager;
import jakarta.persistence.EntityManagerFactory;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -24,6 +26,9 @@ public class CacheResetEventListener extends BaseRecomEventListener {
private final CacheManager cacheManager;
@NonNull
private final DBCachedManager dbCachedManager;
@NonNull
private final EntityManagerFactory entityManagerFactory;


@Async("CacheResetExecutor")
@EventListener(classes = CacheResetAsyncEvent.class)
Expand All @@ -33,7 +38,15 @@ public void handleCacheResetAsyncEvent(@NonNull final CacheResetAsyncEvent event
}

private void clearAllCaches() {
// Clear Application Caches
cacheManager.getCacheNames().forEach(cacheName -> Optional.ofNullable(cacheManager.getCache(cacheName)).ifPresent(Cache::clear));

// Clear Hibernate Level 2 Entity Caches
entityManagerFactory.getCache().unwrap(org.hibernate.Cache.class).evictAllRegions(); // does not work?
// entityManagerFactory.getCache().evictAll();
// entityManagerFactory.getCache().evict(Account.class);

// Clear DBCached Caches
dbCachedManager.clearAll();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.recom.service.cache;

import com.recom.dto.cache.CacheInfoDto;
import com.recom.dto.cache.CacheStatisticsDto;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.SessionFactory;
import org.hibernate.stat.CacheRegionStatistics;
import org.hibernate.stat.Statistics;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
@RequiredArgsConstructor
public class CacheStatisticsProvider {

@NonNull
private final SessionFactory sessionFactory;


@NonNull
public CacheStatisticsDto provide() {
final Statistics statistics = sessionFactory.getStatistics();
final List<CacheInfoDto> caches = Arrays.stream(statistics.getSecondLevelCacheRegionNames())
.map(region -> {
try {
return statistics.getDomainDataRegionStatistics(region);
} catch (final Exception e) {
log.warn("Error getting cache statistics for region: {}", region);
return null;
}
})
.filter(Objects::nonNull)
.map(this::getCacheInfo)
.toList();

return CacheStatisticsDto.builder()
.caches(caches)
.build();
}

@NonNull
private CacheInfoDto getCacheInfo(@NonNull final CacheRegionStatistics regionStatistics) {
return CacheInfoDto.builder()
.name(regionStatistics.getRegionName())
.size(regionStatistics.getSizeInMemory())
.stats(regionStatistics.toString())
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.cache.annotation.CacheResult;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
Expand All @@ -29,7 +30,8 @@ public class MapRendererService {
private final ClusteringService clusteringService;

@NonNull
@Cacheable(value = MAP_RENDERER_CACHE_NAME)
// @Cacheable(value = MAP_RENDERER_CACHE_NAME)
@CacheResult(cacheName = MAP_RENDERER_CACHE_NAME)
public MapRenderResponseDto renderMap(@NonNull final GameMap gameMap) {
final List<MapRenderCommandDto> renderCommands = clusteringService.generateClusters(gameMap).getClusterList().stream()
.map(cluster -> MapRenderCommandDto.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ spring.jpa.hibernate.ddl-auto=update

logging.level.com.recom=WARN
logging.level.com.recom.api=INFO
logging.level.com.recom.api.map.StructureScannerController=DEBUG
logging.level.com.recom.event.listener=DEBUG
#logging.level.com.recom.api.map.StructureScannerController=DEBUG
#logging.level.com.recom.event.listener=DEBUG
logging.level.com.recom.runner=INFO
logging.level.com.recom.event=INFO
logging.level.com.recom.service.messagebus=DEBUG
logging.level.com.recom.service.gameMap.TopographyMapDataService=INFO
#logging.level.com.recom.service.messagebus=DEBUG
#logging.level.com.recom.service.gameMap.TopographyMapDataService=INFO
logging.level.com.recom.service.dbcached=DEBUG
logging.level.com.recom.persistence.dbcached=DEBUG

Expand All @@ -19,7 +19,7 @@ logging.level.com.recom.persistence.dbcached=DEBUG
logging.level.com.vladmihalcea.hibernate.type.util.LogUtils=DEBUG
#spring.jpa.properties.hibernate.format_sql=true
#spring.jpa.properties.hibernate.highlight_sql=true
#spring.jpa.properties.hibernate.generate_statistics=true
spring.jpa.properties.hibernate.generate_statistics=true
#logging.level.org.springframework.web.filter.CommonsRequestLoggingFilter=DEBUG


Expand Down
10 changes: 4 additions & 6 deletions services/recom-backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ server.port=80

spring.threads.virtual.enabled=true

spring.datasource.url=jdbc:h2:file:${recom.data-base-dir}/RECOMBackend/h2db/recomh2;AUTO_SERVER=TRUE;AUTO_SERVER_PORT=8034;USER=username;PASSWORD=password
spring.datasource.url=jdbc:h2:file:${recom.data-base-dir}/h2db/recomh2;AUTO_SERVER=TRUE;AUTO_SERVER_PORT=8034;USER=username;PASSWORD=password
spring.datasource.username=username
spring.datasource.password=password

Expand Down Expand Up @@ -35,14 +35,12 @@ spring.jpa.properties.hibernate.cache.use_query_cache=true
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.jcache.internal.JCacheRegionFactory

#https://stackoverflow.com/questions/76853497/porting-to-springboot-3-hibernate-6-ehcache-3
spring.jpa.properties.hibernate.javax.cache.provider=org.ehcache.jsr107.EhcacheCachingProvider
spring.jpa.properties.hibernate.javax.cache.uri=classpath:ehcache.xml
spring.jpa.properties.hibernate.javax.cache.missing_cache_strategy=fail
spring.jpa.properties.hibernate.javax.missing_cache_strategy=fail
spring.jpa.properties.hibernate.javax.cache.sharedMode=ENABLE_SELECTIVE

#https://stackoverflow.com/questions/76853497/porting-to-springboot-3-hibernate-6-ehcache-3

#spring.cache.jcache.config=classpath:ehcache.xml

spring.jpa.properties.hibernate.jdbc.batch_size=100
Expand All @@ -55,10 +53,10 @@ logging.level.org.hibernate.type=ERROR
logging.level.org.hibernate.SQL=ERROR
logging.level.com.vladmihalcea.hibernate.type.util.LogUtils=OFF

recom.data-base-dir=${user.home}
recom.data-base-dir=${user.home}/RECOMBackend
recom.security.jwt-issuer=RECOM DEV Backend
recom.security.jwt-expiration-time=60m
recom.security.key-path=${recom.data-base-dir}/RECOMBackend/RECOMKey
recom.security.key-path=${recom.data-base-dir}/RECOMKey

recom.async.core-pool-size=1
recom.async.max-pool-size=10
12 changes: 5 additions & 7 deletions services/recom-backend/src/main/resources/ehcache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
<!-- look: https://www.digitalocean.com/community/tutorials/hibernate-ehcache-hibernate-second-level-cache -->

<!-- Persistent cache directory -->
<persistence directory="ehcache/cache"/>
<persistence directory="${user.home}/RECOMBackend/ehcache"/>
<!-- Persistent cache directory -->



Expand Down Expand Up @@ -129,15 +130,12 @@



<!-- HYBERNATE -->
<!-- HIBERNATE -->
<cache alias="default-update-timestamps-region" uses-template="infinite">
</cache>
<cache alias="default-query-results-region">
<resources>
<heap unit="entries">10000</heap>
</resources>
<cache alias="default-query-results-region" uses-template="infinite">
</cache>
<!-- HYBERNATE -->
<!-- HIBERNATE -->



Expand Down

0 comments on commit a48a14c

Please sign in to comment.