View Javadoc
1   package fr.ifremer.quadrige2.core.service.technical;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 Core Shared
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
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   * <p>CacheServiceImpl class.</p>
38   *
39   */
40  @Component(value = "cacheService")
41  @Lazy
42  public class CacheServiceImpl implements CacheService {
43      /** Logger. */
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      /** {@inheritDoc} */
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      /** {@inheritDoc} */
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      /** {@inheritDoc} */
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  }