Skip to content

Instantly share code, notes, and snippets.

@anataliocs
Last active September 17, 2020 10:30
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save anataliocs/1074e7004b2c99320b7a to your computer and use it in GitHub Desktop.
Guava cache with spring boot and clear cache method
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;
}
}
//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);
}
}
@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();
}
}
<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>
@hendisantika
Copy link

I am upgrading into Spring Boot 2.x.x, then it's broken. How I can resolve the broken class?
Thank You

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment