001package fr.ifremer.adagio.core.service.technical; 002 003/* 004 * #%L 005 * SIH-Adagio Core Shared 006 * $Id: CacheServiceImpl.java 11975 2014-02-12 17:56:29Z bl05b3e $ 007 * $HeadURL: https://forge.ifremer.fr/svn/sih-adagio/tags/adagio-3.5.6/core-shared/src/main/java/fr/ifremer/adagio/core/service/technical/CacheServiceImpl.java $ 008 * %% 009 * Copyright (C) 2012 - 2013 Ifremer 010 * %% 011 * This program is free software: you can redistribute it and/or modify 012 * it under the terms of the GNU Affero General Public License as published by 013 * the Free Software Foundation, either version 3 of the License, or 014 * (at your option) any later version. 015 * 016 * This program is distributed in the hope that it will be useful, 017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 019 * GNU General Public License for more details. 020 * 021 * You should have received a copy of the GNU Affero General Public License 022 * along with this program. If not, see <http://www.gnu.org/licenses/>. 023 * #L% 024 */ 025 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.springframework.cache.Cache; 029import org.springframework.cache.CacheManager; 030import org.springframework.context.annotation.Lazy; 031import org.springframework.stereotype.Component; 032 033import javax.annotation.Resource; 034 035@Component(value = "cacheService") 036@Lazy 037public class CacheServiceImpl implements CacheService { 038 /** Logger. */ 039 private static final Log logger = 040 LogFactory.getLog(CacheServiceImpl.class); 041 042 @Resource(name = "cacheManager") 043 private CacheManager cacheManager; 044 045 @Resource(name = "ehcache") 046 private net.sf.ehcache.CacheManager ehCacheManager; 047 048 @Override 049 public void clearAllCaches() { 050 logger.info("Clearing all caches..."); 051 for (String cacheName : cacheManager.getCacheNames()) { 052 Cache cache = cacheManager.getCache(cacheName); 053 cache.clear(); 054 } 055 056 ehCacheManager.clearAll(); 057 } 058 059 @Override 060 public void clearCache(String cacheName) { 061 logger.info("Clearing cache " + cacheName + "..."); 062 Cache cache = cacheManager.getCache(cacheName); 063 if (cache != null) { 064 cache.clear(); 065 return; 066 } 067 068 net.sf.ehcache.Cache ehCache = ehCacheManager.getCache(cacheName); 069 if (ehCache != null) { 070 ehCache.removeAll(); 071 return; 072 } 073 074 logger.warn("Unable to clear cache. Cache with name '" + cacheName + "' could not found."); 075 } 076 077 @Override 078 public Cache getCache(String cacheName) { 079 Cache cache = cacheManager.getCache(cacheName); 080 if (cache == null) { 081 net.sf.ehcache.Cache ehCache = ehCacheManager.getCache(cacheName); 082 if (ehCache != null) { 083 logger.warn("Asking for a spring cache with name '" + cacheName + "', but correspond to a EhCache instance. WIll return null."); 084 } 085 } 086 return cache; 087 } 088}