Skip to content

Commit

Permalink
(fix) fix caching based on Redis
Browse files Browse the repository at this point in the history
Cache names were not used, then a single key was created for all caches.
  • Loading branch information
jonathanlermitage committed Apr 15, 2018
1 parent bf333e8 commit ae6e0e6
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 34 deletions.
3 changes: 3 additions & 0 deletions src/main/java/manon/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.extern.slf4j.Slf4j;
import manon.app.info.service.InfoService;
import manon.app.trace.service.AppTraceService;
import manon.game.world.service.WorldService;
import manon.user.err.UserExistsException;
import manon.user.service.RegistrationService;
import org.springframework.boot.SpringApplication;
Expand All @@ -30,6 +31,7 @@ public class Application extends SpringBootServletInitializer {

private final AppTraceService appTraceService;
private final InfoService infoService;
private final WorldService worldService;
private final RegistrationService registrationService;

public static void main(String[] args) {
Expand All @@ -44,6 +46,7 @@ protected SpringApplicationBuilder configure(SpringApplicationBuilder applicatio
@PostConstruct
public void initApp() throws UserExistsException {
infoService.evictCaches();
worldService.evictCaches();
String initAppEvent = "Admin username is " + registrationService.ensureAdmin().getUsername();
appTraceService.log(INFO, APP_START, initAppEvent);
}
Expand Down
28 changes: 0 additions & 28 deletions src/main/java/manon/app/cache/CommonCacheConfig.java

This file was deleted.

11 changes: 9 additions & 2 deletions src/main/java/manon/app/cache/RedisCacheConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package manon.app.cache;

import manon.app.info.service.InfoServiceImpl;
import manon.game.world.service.WorldServiceImpl;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -9,6 +11,8 @@
import org.springframework.data.redis.core.RedisTemplate;

import java.time.Duration;
import java.util.HashSet;
import java.util.Set;

import static manon.app.config.SpringProfiles.REDIS_CACHE;

Expand All @@ -19,10 +23,13 @@ public class RedisCacheConfig extends CachingConfigurerSupport {
@Bean
public RedisCacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofDays(1))
.prefixKeysWith("manon_");
.entryTtl(Duration.ofDays(1));
Set<String> cacheNames = new HashSet<>();
cacheNames.addAll(InfoServiceImpl.CACHES);
cacheNames.addAll(WorldServiceImpl.CACHES);
return RedisCacheManager.builder(redisTemplate.getConnectionFactory())
.cacheDefaults(config)
.initialCacheNames(cacheNames)
.build();
}
}
8 changes: 7 additions & 1 deletion src/main/java/manon/app/info/service/InfoServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

import java.util.Collection;

import static java.util.Collections.singletonList;
import static java.util.Collections.unmodifiableCollection;

@Service
@RequiredArgsConstructor
@PropertySource(value = "classpath:info.properties")
public class InfoServiceImpl implements InfoService {

public static final String CACHE_GET_APPVERSION = "CACHE_GET_APPVERSION";
private static final String CACHE_GET_APPVERSION = "CACHE_GET_APPVERSION";
public static final Collection<String> CACHES = unmodifiableCollection(singletonList(CACHE_GET_APPVERSION));

@Value("${version}")
private String version;
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/manon/game/world/service/WorldServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand All @@ -30,6 +32,7 @@
import java.util.stream.IntStream;

import static java.lang.System.currentTimeMillis;
import static java.util.Collections.unmodifiableCollection;
import static manon.game.world.model.WorldKey.POINT_NAME;
import static manon.game.world.model.WorldKey.SECTOR_NAME;
import static manon.game.world.model.WorldKey.key;
Expand All @@ -43,9 +46,10 @@ public class WorldServiceImpl implements WorldService {
private final WorldSectorService worldSectorService;
private final WorldPointService worldPointService;

public static final String CACHE_GET_WORLD = "CACHE_GET_SINGLE_WORLD";
public static final String CACHE_GET_WORLD_SUMMARY = "CACHE_GET_WORLD_SUMMARY";
public static final String CACHE_GET_WORLD_SUMMARIES = "CACHE_GET_WORLD_SUMMARIES";
private static final String CACHE_GET_WORLD = "CACHE_GET_SINGLE_WORLD";
private static final String CACHE_GET_WORLD_SUMMARY = "CACHE_GET_WORLD_SUMMARY";
private static final String CACHE_GET_WORLD_SUMMARIES = "CACHE_GET_WORLD_SUMMARIES";
public static final Collection<String> CACHES = unmodifiableCollection(Arrays.asList(CACHE_GET_WORLD, CACHE_GET_WORLD_SUMMARY, CACHE_GET_WORLD_SUMMARIES));

private static WorldPointType[] worldPointTypes = WorldPointType.values();
private static int nbWorldPointTypes = worldPointTypes.length;
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/manon/game/world/api/WorldWSTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,14 @@ public void shouldFindAllWorldViews(Rs rs) throws WorldExistsException {
WorldSummaryList webWorldSummaries = readValue(res, WorldSummaryList.class);
assertThat(webWorldSummaries).containsExactlyInAnyOrderElementsOf(expectedWorldSummaries);
}

@Test(dataProvider = DP_AUTHENTICATED)
public void shouldFindZeroWorldViews(Rs rs) {
Response res = rs.getRequestSpecification()
.get(API_WORLD + "/summary/all");
res.then()
.statusCode(SC_OK);
WorldSummaryList webWorldSummaries = readValue(res, WorldSummaryList.class);
assertThat(webWorldSummaries).isEmpty();
}
}

0 comments on commit ae6e0e6

Please sign in to comment.