View Javadoc
1   package fr.ifremer.quadrige3.core.service.technical;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 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  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   * <p>CacheServiceImpl class.</p>
37   */
38  @Component(value = "cacheService")
39  @Lazy
40  public class CacheServiceImpl implements CacheService {
41      /**
42       * Logger.
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       * {@inheritDoc}
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       * {@inheritDoc}
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       * {@inheritDoc}
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  }