From ae6e0e69ac37dbe44b51f449600943e09b9b149b Mon Sep 17 00:00:00 2001 From: Jonathan Lermitage Date: Sun, 15 Apr 2018 21:07:08 +0200 Subject: [PATCH] (fix) fix caching based on Redis Cache names were not used, then a single key was created for all caches. --- src/main/java/manon/Application.java | 3 ++ .../manon/app/cache/CommonCacheConfig.java | 28 ------------------- .../manon/app/cache/RedisCacheConfig.java | 11 ++++++-- .../app/info/service/InfoServiceImpl.java | 8 +++++- .../game/world/service/WorldServiceImpl.java | 10 +++++-- .../manon/game/world/api/WorldWSTest.java | 10 +++++++ 6 files changed, 36 insertions(+), 34 deletions(-) delete mode 100644 src/main/java/manon/app/cache/CommonCacheConfig.java diff --git a/src/main/java/manon/Application.java b/src/main/java/manon/Application.java index bee94c74..9d233703 100644 --- a/src/main/java/manon/Application.java +++ b/src/main/java/manon/Application.java @@ -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; @@ -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) { @@ -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); } diff --git a/src/main/java/manon/app/cache/CommonCacheConfig.java b/src/main/java/manon/app/cache/CommonCacheConfig.java deleted file mode 100644 index 98d742e4..00000000 --- a/src/main/java/manon/app/cache/CommonCacheConfig.java +++ /dev/null @@ -1,28 +0,0 @@ -package manon.app.cache; - -import org.jetbrains.annotations.NotNull; -import org.springframework.cache.interceptor.KeyGenerator; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class CommonCacheConfig { - - /** Key generator name: generate a unique key of the class name, the method name, and all method parameters appended. */ - public static final String KEY_GENERATOR_FULL = "KEY_GENERATOR_FULL"; - - /** Generate a unique key of the class name, the method name, and all method parameters appended. */ - @NotNull - @Bean(name = KEY_GENERATOR_FULL) - public KeyGenerator keyGenerator() { - return (o, method, objects) -> { - StringBuilder sb = new StringBuilder(); - sb.append(o.getClass().getName()); - sb.append(method.getName()); - for (Object obj : objects) { - sb.append(obj.toString()); - } - return sb.toString(); - }; - } -} diff --git a/src/main/java/manon/app/cache/RedisCacheConfig.java b/src/main/java/manon/app/cache/RedisCacheConfig.java index 34bf2f4e..32e7bd9f 100644 --- a/src/main/java/manon/app/cache/RedisCacheConfig.java +++ b/src/main/java/manon/app/cache/RedisCacheConfig.java @@ -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; @@ -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; @@ -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 cacheNames = new HashSet<>(); + cacheNames.addAll(InfoServiceImpl.CACHES); + cacheNames.addAll(WorldServiceImpl.CACHES); return RedisCacheManager.builder(redisTemplate.getConnectionFactory()) .cacheDefaults(config) + .initialCacheNames(cacheNames) .build(); } } diff --git a/src/main/java/manon/app/info/service/InfoServiceImpl.java b/src/main/java/manon/app/info/service/InfoServiceImpl.java index 15affb23..eac058aa 100644 --- a/src/main/java/manon/app/info/service/InfoServiceImpl.java +++ b/src/main/java/manon/app/info/service/InfoServiceImpl.java @@ -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 CACHES = unmodifiableCollection(singletonList(CACHE_GET_APPVERSION)); @Value("${version}") private String version; diff --git a/src/main/java/manon/game/world/service/WorldServiceImpl.java b/src/main/java/manon/game/world/service/WorldServiceImpl.java index 81fd4ae1..6eeadb02 100644 --- a/src/main/java/manon/game/world/service/WorldServiceImpl.java +++ b/src/main/java/manon/game/world/service/WorldServiceImpl.java @@ -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; @@ -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; @@ -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 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; diff --git a/src/test/java/manon/game/world/api/WorldWSTest.java b/src/test/java/manon/game/world/api/WorldWSTest.java index a75d8e48..9f24df50 100644 --- a/src/test/java/manon/game/world/api/WorldWSTest.java +++ b/src/test/java/manon/game/world/api/WorldWSTest.java @@ -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(); + } }