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