1 package fr.ifremer.quadrige3.core.service.technical;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.springframework.cache.Cache;
29 import org.springframework.cache.CacheManager;
30 import org.springframework.context.annotation.Lazy;
31 import org.springframework.stereotype.Component;
32
33 import javax.annotation.Resource;
34
35
36
37
38 @Component(value = "cacheService")
39 @Lazy
40 public class CacheServiceImpl implements CacheService {
41
42
43
44 private static final Log logger = LogFactory.getLog(CacheServiceImpl.class);
45
46 @Resource(name = "cacheManager")
47 private CacheManager cacheManager;
48
49 @Resource(name = "ehcache")
50 private net.sf.ehcache.CacheManager ehCacheManager;
51
52
53
54
55 @Override
56 public void clearAllCaches() {
57 logger.info("Clearing all caches...");
58 for (String cacheName : cacheManager.getCacheNames()) {
59 Cache cache = cacheManager.getCache(cacheName);
60 cache.clear();
61 }
62 ehCacheManager.clearAll();
63 }
64
65
66
67
68 @Override
69 public void clearCache(String cacheName) {
70 logger.info("Clearing cache " + cacheName + "...");
71 Cache cache = cacheManager.getCache(cacheName);
72 if (cache != null) {
73 cache.clear();
74 return;
75 }
76 net.sf.ehcache.Cache ehCache = ehCacheManager.getCache(cacheName);
77 if (ehCache != null) {
78 ehCache.removeAll();
79 return;
80 }
81 logger.warn("Unable to clear cache. Cache with name '" + cacheName + "' could not found.");
82 }
83
84
85
86
87 @Override
88 public Cache getCache(String cacheName) {
89 Cache cache = cacheManager.getCache(cacheName);
90 if (cache == null) {
91 net.sf.ehcache.Cache ehCache = ehCacheManager.getCache(cacheName);
92 if (ehCache != null) {
93 logger.warn("Asking for a spring cache with name '" + cacheName + "', but correspond to a EhCache instance. WIll return null.");
94 }
95 }
96 return cache;
97 }
98 }