Skip to content

Commit c028c40

Browse files
authored
Merge pull request eugenp#5391 from tinomthomas/master
BAEL -2251
2 parents 018e00c + 0d4fa7c commit c028c40

4 files changed

Lines changed: 246 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.baeldung.caching.eviction.controllers;
2+
3+
import org.baeldung.caching.eviction.service.CachingService;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@RestController
9+
public class CachingController {
10+
11+
@Autowired
12+
CachingService cachingService;
13+
14+
@GetMapping("clearAllCaches")
15+
public void clearAllCaches() {
16+
cachingService.evictAllCaches();
17+
}
18+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.baeldung.caching.eviction.service;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.cache.CacheManager;
5+
import org.springframework.cache.annotation.CacheEvict;
6+
import org.springframework.scheduling.annotation.Scheduled;
7+
import org.springframework.stereotype.Component;
8+
9+
@Component
10+
public class CachingService {
11+
12+
@Autowired
13+
CacheManager cacheManager;
14+
15+
public void putToCache(String cacheName, String key, String value) {
16+
cacheManager.getCache(cacheName).put(key, value);
17+
}
18+
19+
public String getFromCache(String cacheName, String key) {
20+
String value = null;
21+
if (cacheManager.getCache(cacheName).get(key) != null) {
22+
value = cacheManager.getCache(cacheName).get(key).get().toString();
23+
}
24+
return value;
25+
}
26+
27+
@CacheEvict(value = "first", key = "#cacheKey")
28+
public void evictSingleCacheValue(String cacheKey) {
29+
}
30+
31+
@CacheEvict(value = "first", allEntries = true)
32+
public void evictAllCacheValues() {
33+
}
34+
35+
public void evictSingleCacheValue(String cacheName, String cacheKey) {
36+
cacheManager.getCache(cacheName).evict(cacheKey);
37+
}
38+
39+
public void evictAllCacheValues(String cacheName) {
40+
cacheManager.getCache(cacheName).clear();
41+
}
42+
43+
public void evictAllCaches() {
44+
cacheManager.getCacheNames()
45+
.parallelStream()
46+
.forEach(cacheName -> cacheManager.getCache(cacheName).clear());
47+
}
48+
49+
@Scheduled(fixedRate = 6000)
50+
public void evictAllcachesAtIntervals() {
51+
evictAllCaches();
52+
}
53+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package org.baeldung.caching.test;
2+
3+
import static org.hamcrest.CoreMatchers.is;
4+
import static org.hamcrest.CoreMatchers.nullValue;
5+
import static org.junit.Assert.assertThat;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
import org.baeldung.caching.eviction.service.CachingService;
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
import org.junit.runner.RunWith;
14+
import org.springframework.beans.factory.annotation.Autowired;
15+
import org.springframework.cache.Cache;
16+
import org.springframework.cache.CacheManager;
17+
import org.springframework.cache.annotation.EnableCaching;
18+
import org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean;
19+
import org.springframework.cache.support.SimpleCacheManager;
20+
import org.springframework.context.annotation.Bean;
21+
import org.springframework.context.annotation.Configuration;
22+
import org.springframework.test.context.ContextConfiguration;
23+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
24+
25+
@RunWith(SpringJUnit4ClassRunner.class)
26+
@ContextConfiguration
27+
public class CacheEvictAnnotationIntegrationTest {
28+
29+
@Configuration
30+
@EnableCaching
31+
static class ContextConfiguration {
32+
33+
@Bean
34+
public CachingService cachingService() {
35+
return new CachingService();
36+
}
37+
38+
@Bean
39+
public CacheManager cacheManager(){
40+
SimpleCacheManager cacheManager = new SimpleCacheManager();
41+
List<Cache> caches = new ArrayList<>();
42+
caches.add(cacheBean().getObject());
43+
cacheManager.setCaches(caches );
44+
return cacheManager;
45+
}
46+
47+
@Bean
48+
public ConcurrentMapCacheFactoryBean cacheBean(){
49+
ConcurrentMapCacheFactoryBean cacheFactoryBean = new ConcurrentMapCacheFactoryBean();
50+
cacheFactoryBean.setName("first");
51+
return cacheFactoryBean;
52+
}
53+
}
54+
55+
@Autowired
56+
CachingService cachingService;
57+
58+
@Before
59+
public void before() {
60+
cachingService.putToCache("first", "key1", "Baeldung");
61+
cachingService.putToCache("first", "key2", "Article");
62+
}
63+
64+
@Test
65+
public void givenFirstCache_whenSingleCacheValueEvictRequested_thenEmptyCacheValue() {
66+
cachingService.evictSingleCacheValue("key1");
67+
String key1 = cachingService.getFromCache("first", "key1");
68+
assertThat(key1, is(nullValue()));
69+
}
70+
71+
@Test
72+
public void givenFirstCache_whenAllCacheValueEvictRequested_thenEmptyCache() {
73+
cachingService.evictAllCacheValues();
74+
String key1 = cachingService.getFromCache("first", "key1");
75+
String key2 = cachingService.getFromCache("first", "key2");
76+
assertThat(key1, is(nullValue()));
77+
assertThat(key2, is(nullValue()));
78+
}
79+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package org.baeldung.caching.test;
2+
3+
import static org.hamcrest.CoreMatchers.is;
4+
import static org.hamcrest.CoreMatchers.nullValue;
5+
import static org.junit.Assert.assertThat;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
import org.baeldung.caching.eviction.service.CachingService;
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
import org.junit.runner.RunWith;
14+
import org.springframework.beans.factory.annotation.Autowired;
15+
import org.springframework.cache.Cache;
16+
import org.springframework.cache.CacheManager;
17+
import org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean;
18+
import org.springframework.cache.support.SimpleCacheManager;
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.test.context.ContextConfiguration;
22+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
23+
24+
@RunWith(SpringJUnit4ClassRunner.class)
25+
@ContextConfiguration
26+
public class CacheManagerEvictIntegrationTest {
27+
28+
@Configuration
29+
static class ContextConfiguration {
30+
31+
@Bean
32+
public CachingService cachingService() {
33+
return new CachingService();
34+
}
35+
36+
@Bean
37+
public CacheManager cacheManager(){
38+
SimpleCacheManager cacheManager = new SimpleCacheManager();
39+
List<Cache> caches = new ArrayList<>();
40+
caches.add(cacheBeanFirst().getObject());
41+
caches.add(cacheBeanSecond().getObject());
42+
cacheManager.setCaches(caches );
43+
return cacheManager;
44+
}
45+
46+
@Bean
47+
public ConcurrentMapCacheFactoryBean cacheBeanFirst(){
48+
ConcurrentMapCacheFactoryBean cacheFactoryBean = new ConcurrentMapCacheFactoryBean();
49+
cacheFactoryBean.setName("first");
50+
return cacheFactoryBean;
51+
}
52+
53+
@Bean
54+
public ConcurrentMapCacheFactoryBean cacheBeanSecond(){
55+
ConcurrentMapCacheFactoryBean cacheFactoryBean = new ConcurrentMapCacheFactoryBean();
56+
cacheFactoryBean.setName("second");
57+
return cacheFactoryBean;
58+
}
59+
}
60+
61+
@Autowired
62+
CachingService cachingService;
63+
64+
@Before
65+
public void before() {
66+
cachingService.putToCache("first", "key1", "Baeldung");
67+
cachingService.putToCache("first", "key2", "Article");
68+
cachingService.putToCache("second", "key", "Article");
69+
}
70+
71+
@Test
72+
public void givenFirstCache_whenSingleCacheValueEvictRequested_thenEmptyCacheValue() {
73+
cachingService.evictSingleCacheValue("first", "key1");
74+
String key1 = cachingService.getFromCache("first", "key1");
75+
assertThat(key1, is(nullValue()));
76+
}
77+
78+
@Test
79+
public void givenFirstCache_whenAllCacheValueEvictRequested_thenEmptyCache() {
80+
cachingService.evictAllCacheValues("first");
81+
String key1 = cachingService.getFromCache("first", "key1");
82+
String key2 = cachingService.getFromCache("first", "key2");
83+
assertThat(key1, is(nullValue()));
84+
assertThat(key2, is(nullValue()));
85+
}
86+
87+
@Test
88+
public void givenAllCaches_whenAllCacheEvictRequested_thenEmptyAllCaches() {
89+
cachingService.evictAllCaches();
90+
String key1 = cachingService.getFromCache("first", "key1");
91+
assertThat(key1, is(nullValue()));
92+
93+
String key = cachingService.getFromCache("second", "key");
94+
assertThat(key, is(nullValue()));
95+
}
96+
}

0 commit comments

Comments
 (0)