parent
da4e0f7bd7
commit
382a3e789b
|
@ -2,8 +2,12 @@ package com.baeldung.springbootmvc;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cache.annotation.EnableCaching;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@EnableCaching
|
||||||
|
@EnableScheduling
|
||||||
public class SpringBootMvcApplication {
|
public class SpringBootMvcApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
package com.baeldung.springbootmvc.caching;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class CachingController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
CachingService cachingService;
|
||||||
|
|
||||||
|
@GetMapping("clearAllCaches")
|
||||||
|
public void clearAllCaches() {
|
||||||
|
cachingService.evictAllCaches();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,53 @@
|
||||||
|
package com.baeldung.springbootmvc.caching;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.cache.CacheManager;
|
||||||
|
import org.springframework.cache.annotation.CacheEvict;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class CachingService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
CacheManager cacheManager;
|
||||||
|
|
||||||
|
public void putToCache(String cacheName, String key, String value) {
|
||||||
|
cacheManager.getCache(cacheName).put(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFromCache(String cacheName, String key) {
|
||||||
|
String value = null;
|
||||||
|
if (cacheManager.getCache(cacheName).get(key) != null) {
|
||||||
|
value = cacheManager.getCache(cacheName).get(key).get().toString();
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@CacheEvict(value = "first", key = "#cacheKey")
|
||||||
|
public void evictSingleCacheValue(String cacheKey) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@CacheEvict(value = "first", allEntries = true)
|
||||||
|
public void evictAllCacheValues() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void evictSingleCacheValue(String cacheName, String cacheKey) {
|
||||||
|
cacheManager.getCache(cacheName).evict(cacheKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void evictAllCacheValues(String cacheName) {
|
||||||
|
cacheManager.getCache(cacheName).clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void evictAllCaches() {
|
||||||
|
cacheManager.getCacheNames()
|
||||||
|
.parallelStream()
|
||||||
|
.forEach(cacheName -> cacheManager.getCache(cacheName).clear());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Scheduled(fixedRate = 6000)
|
||||||
|
public void evictAllcachesAtIntervals() {
|
||||||
|
evictAllCaches();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
package com.baeldung.caching;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.hamcrest.CoreMatchers.nullValue;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import com.baeldung.springbootmvc.SpringBootMvcApplication;
|
||||||
|
import com.baeldung.springbootmvc.caching.CachingService;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(classes = {SpringBootMvcApplication.class, CachingService.class})
|
||||||
|
public class CacheEvictAnnotationIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired CachingService cachingService;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void before() {
|
||||||
|
cachingService.putToCache("first", "key1", "Baeldung");
|
||||||
|
cachingService.putToCache("first", "key2", "Article");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFirstCache_whenSingleCacheValueEvictRequested_thenEmptyCacheValue() {
|
||||||
|
cachingService.evictSingleCacheValue("key1");
|
||||||
|
String key1 = cachingService.getFromCache("first", "key1");
|
||||||
|
assertThat(key1, is(nullValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFirstCache_whenAllCacheValueEvictRequested_thenEmptyCache() {
|
||||||
|
cachingService.evictAllCacheValues();
|
||||||
|
String key1 = cachingService.getFromCache("first", "key1");
|
||||||
|
String key2 = cachingService.getFromCache("first", "key2");
|
||||||
|
assertThat(key1, is(nullValue()));
|
||||||
|
assertThat(key2, is(nullValue()));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,55 @@
|
||||||
|
package com.baeldung.caching;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.hamcrest.CoreMatchers.nullValue;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import com.baeldung.springbootmvc.SpringBootMvcApplication;
|
||||||
|
import com.baeldung.springbootmvc.caching.CachingService;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(classes = {SpringBootMvcApplication.class, CachingService.class})
|
||||||
|
public class CacheManagerEvictIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired CachingService cachingService;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void before() {
|
||||||
|
cachingService.putToCache("first", "key1", "Baeldung");
|
||||||
|
cachingService.putToCache("first", "key2", "Article");
|
||||||
|
cachingService.putToCache("second", "key", "Article");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFirstCache_whenSingleCacheValueEvictRequested_thenEmptyCacheValue() {
|
||||||
|
cachingService.evictSingleCacheValue("first","key1");
|
||||||
|
String key1 = cachingService.getFromCache("first", "key1");
|
||||||
|
assertThat(key1, is(nullValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFirstCache_whenAllCacheValueEvictRequested_thenEmptyCache() {
|
||||||
|
cachingService.evictAllCacheValues("first");
|
||||||
|
String key1 = cachingService.getFromCache("first", "key1");
|
||||||
|
String key2 = cachingService.getFromCache("first", "key2");
|
||||||
|
assertThat(key1, is(nullValue()));
|
||||||
|
assertThat(key2, is(nullValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAllCaches_whenAllCacheEvictRequested_thenEmptyAllCaches() {
|
||||||
|
cachingService.evictAllCaches();
|
||||||
|
String key1 = cachingService.getFromCache("first", "key1");
|
||||||
|
assertThat(key1, is(nullValue()));
|
||||||
|
|
||||||
|
String key = cachingService.getFromCache("second", "key");
|
||||||
|
assertThat(key, is(nullValue()));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue