Last active
August 4, 2024 04:24
-
-
Save anataliocs/1074e7004b2c99320b7a to your computer and use it in GitHub Desktop.
Guava cache with spring boot and clear cache method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.common.cache.CacheBuilder; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.cache.CacheManager; | |
import org.springframework.cache.annotation.CachingConfigurer; | |
import org.springframework.cache.annotation.EnableCaching; | |
import org.springframework.cache.guava.GuavaCache; | |
import org.springframework.cache.interceptor.CacheErrorHandler; | |
import org.springframework.cache.interceptor.CacheResolver; | |
import org.springframework.cache.interceptor.KeyGenerator; | |
import org.springframework.cache.interceptor.SimpleKeyGenerator; | |
import org.springframework.cache.support.SimpleCacheManager; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import java.util.Arrays; | |
import java.util.concurrent.TimeUnit; | |
@Configuration | |
@EnableCaching | |
public class CacheConfig implements CachingConfigurer { | |
public final static String CACHE_ONE = "cacheOne"; | |
public final static String CACHE_TWO = "cacheTwo"; | |
private final Logger log = LoggerFactory | |
.getLogger(CacheConfig.class); | |
@Bean | |
@Override | |
public CacheManager cacheManager() { | |
log.info("Initializing simple Guava Cache manager."); | |
SimpleCacheManager cacheManager = new SimpleCacheManager(); | |
GuavaCache cache1 = new GuavaCache(CACHE_ONE, CacheBuilder.newBuilder() | |
.expireAfterWrite(60, TimeUnit.MINUTES) | |
.build()); | |
GuavaCache cache2 = new GuavaCache(CACHE_TWO, CacheBuilder.newBuilder() | |
.expireAfterWrite(60, TimeUnit.SECONDS) | |
.build()); | |
cacheManager.setCaches(Arrays.asList(cache1,cache2)); | |
return cacheManager; | |
} | |
@Override | |
public CacheResolver cacheResolver() { | |
return null; | |
} | |
@Bean | |
@Override | |
public KeyGenerator keyGenerator() { | |
return new SimpleKeyGenerator(); | |
} | |
@Override | |
public CacheErrorHandler errorHandler() { | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Used for manually clearing the cache | |
@Controller | |
@RequestMapping("/") | |
public class CacheController { | |
@CacheEvict(value = CacheConfig.CACHE_ONE, allEntries = true) | |
@RequestMapping(value = "/clearCache", method = RequestMethod.GET) | |
public ResponseEntity<String> clearCache() { | |
return new ResponseEntity<String>("Cache Cleared", HttpStatus.OK); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Service | |
public class CachedService extends WebServiceGatewaySupport implements CachedService { | |
@Inject | |
private RestTemplate restTemplate; | |
@Cacheable(CacheConfig.CACHE_ONE) | |
public String getCached() { | |
HttpHeaders headers = new HttpHeaders(); | |
headers.setContentType(MediaType.APPLICATION_JSON); | |
HttpEntity<String> reqEntity = new HttpEntity<>("url", headers); | |
ResponseEntity<String> response; | |
String url = "url"; | |
response = restTemplate.exchange( | |
url, | |
HttpMethod.GET, reqEntity, String.class); | |
return response.getBody(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>com.google.guava</groupId> | |
<artifactId>guava</artifactId> | |
<version>18.0</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-context-support</artifactId> | |
<version>4.1.7.RELEASE</version> | |
</dependency> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am upgrading into Spring Boot 2.x.x, then it's broken. How I can resolve the broken class?
Thank You