diff --git a/src/main/java/com/uci/utils/BotService.java b/src/main/java/com/uci/utils/BotService.java index 1831ebf..419698c 100644 --- a/src/main/java/com/uci/utils/BotService.java +++ b/src/main/java/com/uci/utils/BotService.java @@ -4,7 +4,6 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; -import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; import com.inversoft.rest.ClientResponse; import com.uci.utils.bot.util.BotUtil; @@ -21,10 +20,13 @@ import org.checkerframework.checker.nullness.qual.NonNull; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.Cacheable; +import org.springframework.cache.caffeine.CaffeineCacheManager; import org.springframework.stereotype.Service; import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.util.UriBuilder; +import org.springframework.cache.Cache; import reactor.core.publisher.Mono; import reactor.core.publisher.Signal; @@ -53,7 +55,7 @@ public class BotService { public WebClient webClient; public FusionAuthClient fusionAuthClient; - private Cache cache; + private CacheManager cacheManager; // public BotService(WebClient webClient, FusionAuthClient fusionAuthClient, Caffeine caffeineCacheBuilder) { // this.webClient = webClient; @@ -79,7 +81,9 @@ public class BotService { */ public Mono getCampaignFromStartingMessage(String startingMessage) { String cacheKey = "bot-name-for-starting-message:" + startingMessage; - return CacheMono.lookup(key -> Mono.justOrEmpty(cache.getIfPresent(cacheKey) != null ? cache.getIfPresent(key).toString() : null) + Cache cache = cacheManager.getCache(cacheKey); + + return CacheMono.lookup(key -> Mono.justOrEmpty(cache != null && cache.get(cacheKey) != null ? cache.get(cacheKey).get().toString() : null) .map(Signal::next), cacheKey) .onCacheMissResume(() -> webClient.get() .uri(builder -> builder.path("admin/v1/bot/getByParam/").queryParam("startingMessage", startingMessage).build()) @@ -112,7 +116,9 @@ public Mono getCampaignFromStartingMessage(String startingMessage) { public Mono getCurrentAdapter(String botName) { String cacheKey = "adpater-of-bot: " + botName; - return CacheMono.lookup(key -> Mono.justOrEmpty(cache.getIfPresent(cacheKey) != null ? cache.getIfPresent(key).toString() : null) + Cache cache = cacheManager.getCache(cacheKey); + + return CacheMono.lookup(key -> Mono.justOrEmpty(cache != null && cache.get(cacheKey) != null ? cache.get(cacheKey).get().toString() : null) .map(Signal::next), cacheKey) .onCacheMissResume(() -> webClient.get() .uri(builder -> builder.path("admin/v1/bot/getByParam/").queryParam("name", botName).build()).retrieve() @@ -147,7 +153,9 @@ public Mono getCurrentAdapter(String botName) { public Mono getBotIDFromBotName(String botName) { String cacheKey = "Bot-id-for-bot-name: " + botName; - return CacheMono.lookup(key -> Mono.justOrEmpty(cache.getIfPresent(cacheKey) != null ? cache.getIfPresent(key).toString() : null) + Cache cache = cacheManager.getCache(cacheKey); + + return CacheMono.lookup(key -> Mono.justOrEmpty(cache != null && cache.get(cacheKey) != null ? cache.get(cacheKey).get().toString() : null) .map(Signal::next), cacheKey) .onCacheMissResume(() -> webClient.get().uri(new Function() { @Override @@ -195,7 +203,9 @@ public String apply(String response) { */ public Mono> getGupshupAdpaterCredentials(String adapterID) { String cacheKey = "gupshup-credentials-for-adapter: " + adapterID; - return CacheMono.lookup(key -> Mono.justOrEmpty((Map) cache.getIfPresent(cacheKey)) + Cache cache = cacheManager.getCache(cacheKey); + + return CacheMono.lookup(key -> Mono.justOrEmpty(cache != null && cache.get(cacheKey) != null ? (Map) cache.get(cacheKey).get() : null) .map(Signal::next), cacheKey) .onCacheMissResume(() -> webClient.get().uri(builder -> builder.path("admin/v1/adapter/getCredentials/" + adapterID).build()) .retrieve().bodyToMono(String.class).map(response -> { diff --git a/src/main/java/com/uci/utils/CampaignService.java b/src/main/java/com/uci/utils/CampaignService.java index 3937be0..cd01b5f 100644 --- a/src/main/java/com/uci/utils/CampaignService.java +++ b/src/main/java/com/uci/utils/CampaignService.java @@ -4,7 +4,6 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; -import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; import com.inversoft.rest.ClientResponse; import com.uci.utils.bot.util.BotUtil; @@ -15,6 +14,8 @@ import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; +import org.springframework.cache.CacheManager; +import org.springframework.cache.Cache; import org.springframework.stereotype.Service; import org.springframework.web.reactive.function.client.WebClient; @@ -36,7 +37,7 @@ public class CampaignService { public WebClient webClient; public FusionAuthClient fusionAuthClient; - private Cache cache; + private CacheManager cacheManager; // private static final Cache cache = Caffeine.newBuilder().maximumSize(1000) // .expireAfterWrite(Duration.ofSeconds(300)) @@ -50,7 +51,9 @@ public class CampaignService { */ public Mono getCampaignFromID(String campaignID) { String cacheKey = "campaign-node-by-id:" + campaignID; - return CacheMono.lookup(key -> Mono.justOrEmpty((JsonNode) cache.getIfPresent(cacheKey)) + Cache cache = cacheManager.getCache(cacheKey); + + return CacheMono.lookup(key -> Mono.justOrEmpty(cache != null && cache.get(cacheKey) != null ? (JsonNode)cache.get(cacheKey).get() : null) .map(Signal::next), cacheKey) .onCacheMissResume(() -> webClient.get() .uri(builder -> builder.path("admin/v1/bot/get/" + campaignID).build()) @@ -116,7 +119,9 @@ public Application getCampaignFromName(String campaignName) throws Exception { */ public Mono getCampaignFromNameTransformer(String campaignName) { String cacheKey = "campaign-node-by-name:" + campaignName; - return CacheMono.lookup(key -> Mono.justOrEmpty((JsonNode) cache.getIfPresent(cacheKey)) + Cache cache = cacheManager.getCache(cacheKey); + + return CacheMono.lookup(key -> Mono.justOrEmpty(cache != null && cache.get(cacheKey) != null ? (JsonNode)cache.get(cacheKey).get() : null) .map(Signal::next), cacheKey) .onCacheMissResume(() -> webClient.get() .uri(builder -> builder.path("admin/v1/bot/search/").queryParam("name", campaignName).queryParam("match", true).build()) @@ -161,7 +166,9 @@ public JsonNode apply(String response) { */ public Mono getFirstFormByBotID(String botID) { String cacheKey = "form-by-bot-name:" + botID; - return CacheMono.lookup(key -> Mono.justOrEmpty(cache.getIfPresent(cacheKey) != null ? cache.getIfPresent(cacheKey).toString() : null) + Cache cache = cacheManager.getCache(cacheKey); + + return CacheMono.lookup(key -> Mono.justOrEmpty(cache != null && cache.get(cacheKey) != null ? cache.get(cacheKey).get().toString() : null) .map(Signal::next), cacheKey) .onCacheMissResume(() -> webClient.get() .uri(builder -> builder.path("admin/v1/bot/get/" + botID).build()) @@ -197,7 +204,9 @@ public String apply(String response) { public Mono getBotNameByBotID(String botID) { String cacheKey = "bot-name-by-id:" + botID; - return CacheMono.lookup(key -> Mono.justOrEmpty(cache.getIfPresent(cacheKey) != null ? cache.getIfPresent(cacheKey).toString() : null) + Cache cache = cacheManager.getCache(cacheKey); + + return CacheMono.lookup(key -> Mono.justOrEmpty(cache != null && cache.get(cacheKey) != null ? cache.get(cacheKey).get().toString() : null) .map(Signal::next), cacheKey) .onCacheMissResume(() -> webClient.get() .uri(builder -> builder.path("admin/v1/bot/get/" + botID).build()) diff --git a/src/main/java/com/uci/utils/UtilAppConfiguration.java b/src/main/java/com/uci/utils/UtilAppConfiguration.java index 6b5de67..ca11a18 100644 --- a/src/main/java/com/uci/utils/UtilAppConfiguration.java +++ b/src/main/java/com/uci/utils/UtilAppConfiguration.java @@ -5,15 +5,22 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.caffeine.CaffeineCacheManager; +import org.springframework.cache.support.NoOpCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; import org.springframework.web.reactive.function.client.WebClient; import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; +import lombok.extern.slf4j.Slf4j; + +@Slf4j @Configuration +@EnableCaching @EnableAutoConfiguration public class UtilAppConfiguration { @@ -29,17 +36,30 @@ public class UtilAppConfiguration { @Value("${caffeine.cache.exprie.duration.seconds}") public Integer cacheExpireDuration; + @Bean public Caffeine caffeineCacheBuilder() { return Caffeine.newBuilder() .maximumSize(cacheMaxSize) .expireAfterWrite(Duration.ofSeconds(cacheExpireDuration)) .recordStats(); } - + @Bean - public Cache cache() { - return caffeineCacheBuilder().build(); - } + @Profile("!dev") + public CacheManager getRealCacheManager(Caffeine caffeine) { + log.info("Real cache enabled"); + CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager(); + caffeineCacheManager.setCaffeine(caffeine); + return caffeineCacheManager; + } + + @Bean + @Profile("dev") + public CacheManager getNoOpCacheManager() { + log.info("No-op cache enabled"); + NoOpCacheManager caffeineCacheManager = new NoOpCacheManager(); + return caffeineCacheManager; + } @Bean public WebClient getWebClient() { diff --git a/src/main/java/com/uci/utils/cache/controller/CacheController.java b/src/main/java/com/uci/utils/cache/controller/CacheController.java index d7a4950..20ea3c1 100644 --- a/src/main/java/com/uci/utils/cache/controller/CacheController.java +++ b/src/main/java/com/uci/utils/cache/controller/CacheController.java @@ -14,6 +14,7 @@ import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import com.github.benmanes.caffeine.cache.Cache; @@ -24,7 +25,7 @@ @RequestMapping(value = "/cache") public class CacheController { @Autowired - private Cache cache; + private CacheManager cacheManager; /** * call this to invalidate all cache instances @@ -36,10 +37,12 @@ public ResponseEntity getAll() { try { jsonNode = mapper.readTree("{\"id\":\"api.content.cache\",\"ver\":\"3.0\",\"ts\":\"2021-06-26T22:47:05Z+05:30\",\"responseCode\":\"OK\",\"result\":{}}"); JsonNode resultNode = mapper.createObjectNode(); - cache.asMap().keySet().forEach(key -> { - String cacheName = key.toString(); - ((ObjectNode) resultNode).put(cacheName, cache.getIfPresent(cacheName).toString()); - }); + Collection cacheNames = cacheManager.getCacheNames(); + for (String name : cacheNames) { + ((ObjectNode) resultNode).put(name, (cacheManager.getCache(name) != null + && cacheManager.getCache(name).get(name) != null + ? cacheManager.getCache(name).get(name).get().toString() : null)); + } ((ObjectNode) jsonNode).put("result", resultNode); return ResponseEntity.ok(jsonNode); } catch (JsonMappingException e) { @@ -57,14 +60,16 @@ public ResponseEntity getAll() { */ @DeleteMapping(path = "/removeAll") public void removeAll() { - cache.asMap().keySet().forEach(key -> { - removeCache(key.toString()); - }); - } - - private void removeCache(final String cacheName) { - if (cache.getIfPresent(cacheName) != null) { - cache.invalidate(cacheName); + Collection cacheNames = cacheManager.getCacheNames(); + for (String name : cacheNames) { + cacheManager.getCache(name).clear(); +// removeCache(key.toString()); } } + +// private void removeCache(final String cacheName) { +// if (cache.getIfPresent(cacheName) != null) { +// cache.invalidate(cacheName); +// } +// } }