Oct 07, 2016 Is there a way to clear cache only when user log out instead of each request? I want to store my static files(css files,js files) in cache and clear it only when user log out. Any advice is most welcome. Since Spring 3 a new abstraction layer for caching services has been introduced called Cache Abstraction.The idea is to provide a set of common features, mainly annotations, to activate and manage the caches.Cache Abstraction.
Guava cache with spring boot and clear cache method
CacheConfig.java
importcom.google.common.cache.CacheBuilder; |
importorg.slf4j.Logger; |
importorg.slf4j.LoggerFactory; |
importorg.springframework.cache.CacheManager; |
importorg.springframework.cache.annotation.CachingConfigurer; |
importorg.springframework.cache.annotation.EnableCaching; |
importorg.springframework.cache.guava.GuavaCache; |
importorg.springframework.cache.interceptor.CacheErrorHandler; |
importorg.springframework.cache.interceptor.CacheResolver; |
importorg.springframework.cache.interceptor.KeyGenerator; |
importorg.springframework.cache.interceptor.SimpleKeyGenerator; |
importorg.springframework.cache.support.SimpleCacheManager; |
importorg.springframework.context.annotation.Bean; |
importorg.springframework.context.annotation.Configuration; |
importjava.util.Arrays; |
importjava.util.concurrent.TimeUnit; |
@Configuration |
@EnableCaching |
publicclassCacheConfigimplementsCachingConfigurer { |
publicfinalstaticStringCACHE_ONE='cacheOne'; |
publicfinalstaticStringCACHE_TWO='cacheTwo'; |
privatefinalLogger log =LoggerFactory |
.getLogger(CacheConfig.class); |
@Bean |
@Override |
publicCacheManagercacheManager() { |
log.info('Initializing simple Guava Cache manager.'); |
SimpleCacheManager cacheManager =newSimpleCacheManager(); |
GuavaCache cache1 =newGuavaCache(CACHE_ONE, CacheBuilder.newBuilder() |
.expireAfterWrite(60, TimeUnit.MINUTES) |
.build()); |
GuavaCache cache2 =newGuavaCache(CACHE_TWO, CacheBuilder.newBuilder() |
.expireAfterWrite(60, TimeUnit.SECONDS) |
.build()); |
cacheManager.setCaches(Arrays.asList(cache1,cache2)); |
return cacheManager; |
} |
@Override |
publicCacheResolvercacheResolver() { |
returnnull; |
} |
@Bean |
@Override |
publicKeyGeneratorkeyGenerator() { |
returnnewSimpleKeyGenerator(); |
} |
@Override |
publicCacheErrorHandlererrorHandler() { |
returnnull; |
} |
} |
CacheController.java
//Used for manually clearing the cache |
@Controller |
@RequestMapping('/') |
publicclassCacheController { |
@CacheEvict(value=CacheConfig.CACHE_ONE, allEntries=true) |
@RequestMapping(value='/clearCache', method=RequestMethod.GET) |
publicResponseEntity<String>clearCache() { |
returnnewResponseEntity<String>('Cache Cleared', HttpStatus.OK); |
} |
} |
CachedService.java
@Service |
publicclassCachedServiceextendsWebServiceGatewaySupportimplementsCachedService { |
@Inject |
privateRestTemplate restTemplate; |
@Cacheable(CacheConfig.CACHE_ONE) |
publicStringgetCached() { |
HttpHeaders headers =newHttpHeaders(); |
headers.setContentType(MediaType.APPLICATION_JSON); |
HttpEntity<String> reqEntity =newHttpEntity<>('url', headers); |
ResponseEntity<String> response; |
String url ='url'; |
response = restTemplate.exchange( |
url, |
HttpMethod.GET, reqEntity, String.class); |
return response.getBody(); |
} |
} |
![Cache Cache](https://cdn1.howtodoinjava.com/wp-content/uploads/2018/09/Spring-boot-caching.png)
pom.xml
<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 freeto join this conversation on GitHub. Already have an account? Sign in to comment