From dbb757414b982757eacaafcdc96fb885bb2eb9f7 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Sat, 12 Feb 2011 10:52:04 +0100 Subject: [PATCH] improve cache api --- .../cache/{SimpleCache.java => Cache.java} | 12 +- .../java/sonia/scm/cache/CacheManager.java | 19 +- .../java/sonia/scm/cache/ExtendedCache.java | 78 ----- .../CacheRepositoryManagerDecorator.java | 311 ------------------ .../main/java/sonia/scm/cache/EhCache.java | 58 +--- .../java/sonia/scm/cache/EhCacheManager.java | 39 +-- .../scm/plugin/DefaultPluginManager.java | 8 +- .../security/ChainAuthenticatonManager.java | 9 +- 8 files changed, 36 insertions(+), 498 deletions(-) rename scm-core/src/main/java/sonia/scm/cache/{SimpleCache.java => Cache.java} (94%) delete mode 100644 scm-core/src/main/java/sonia/scm/cache/ExtendedCache.java delete mode 100644 scm-webapp/src/main/java/sonia/scm/cache/CacheRepositoryManagerDecorator.java diff --git a/scm-core/src/main/java/sonia/scm/cache/SimpleCache.java b/scm-core/src/main/java/sonia/scm/cache/Cache.java similarity index 94% rename from scm-core/src/main/java/sonia/scm/cache/SimpleCache.java rename to scm-core/src/main/java/sonia/scm/cache/Cache.java index bcaf0f9f22..3f1e582fda 100644 --- a/scm-core/src/main/java/sonia/scm/cache/SimpleCache.java +++ b/scm-core/src/main/java/sonia/scm/cache/Cache.java @@ -29,6 +29,8 @@ * */ + + package sonia.scm.cache; /** @@ -38,7 +40,7 @@ package sonia.scm.cache; * @param * @param */ -public interface SimpleCache +public interface Cache { /** @@ -47,6 +49,14 @@ public interface SimpleCache */ public void clear(); + /** + * Method description + * + * + * @param key + */ + public boolean contains(K key); + /** * Method description * diff --git a/scm-core/src/main/java/sonia/scm/cache/CacheManager.java b/scm-core/src/main/java/sonia/scm/cache/CacheManager.java index c7c2cd4135..39497985a7 100644 --- a/scm-core/src/main/java/sonia/scm/cache/CacheManager.java +++ b/scm-core/src/main/java/sonia/scm/cache/CacheManager.java @@ -29,6 +29,8 @@ * */ + + package sonia.scm.cache; /** @@ -50,21 +52,6 @@ public interface CacheManager * * @return */ - public ExtendedCache getExtendedCache(Class key, - Class value, String name); - - /** - * Method description - * - * - * @param key - * @param value - * @param name - * @param - * @param - * - * @return - */ - public SimpleCache getSimpleCache(Class key, Class value, + public Cache getCache(Class key, Class value, String name); } diff --git a/scm-core/src/main/java/sonia/scm/cache/ExtendedCache.java b/scm-core/src/main/java/sonia/scm/cache/ExtendedCache.java deleted file mode 100644 index b94e53fb5e..0000000000 --- a/scm-core/src/main/java/sonia/scm/cache/ExtendedCache.java +++ /dev/null @@ -1,78 +0,0 @@ -/** - * Copyright (c) 2010, Sebastian Sdorra - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of SCM-Manager; nor the names of its - * contributors may be used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * http://bitbucket.org/sdorra/scm-manager - * - */ - -package sonia.scm.cache; - -//~--- JDK imports ------------------------------------------------------------ - -import java.util.Collection; - -/** - * - * @author Sebastian Sdorra - * - * @param - * @param - */ -public interface ExtendedCache extends SimpleCache -{ - - /** - * Method description - * - * - * @param key - * @param value - */ - public void putCollection(K key, Collection value); - - /** - * Method description - * - * - * @param key - * - * @return - */ - public boolean removeCollection(K key); - - //~--- get methods ---------------------------------------------------------- - - /** - * Method description - * - * - * @param key - * - * @return - */ - public Collection getCollection(K key); -} diff --git a/scm-webapp/src/main/java/sonia/scm/cache/CacheRepositoryManagerDecorator.java b/scm-webapp/src/main/java/sonia/scm/cache/CacheRepositoryManagerDecorator.java deleted file mode 100644 index a5f9f2a74f..0000000000 --- a/scm-webapp/src/main/java/sonia/scm/cache/CacheRepositoryManagerDecorator.java +++ /dev/null @@ -1,311 +0,0 @@ -/** - * Copyright (c) 2010, Sebastian Sdorra - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of SCM-Manager; nor the names of its - * contributors may be used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * http://bitbucket.org/sdorra/scm-manager - * - */ - - - -package sonia.scm.cache; - -//~--- non-JDK imports -------------------------------------------------------- - -import com.google.inject.Inject; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import sonia.scm.ConfigChangedListener; -import sonia.scm.SCMContextProvider; -import sonia.scm.Type; -import sonia.scm.Undecorated; -import sonia.scm.repository.AbstractRepositoryManagerDecorator; -import sonia.scm.repository.Repository; -import sonia.scm.repository.RepositoryException; -import sonia.scm.repository.RepositoryManager; -import sonia.scm.util.AssertUtil; -import sonia.scm.util.Util; - -//~--- JDK imports ------------------------------------------------------------ - -import java.io.IOException; - -import java.util.Collection; - -/** - * - * @author Sebastian Sdorra - */ -public class CacheRepositoryManagerDecorator - extends AbstractRepositoryManagerDecorator - implements ConfigChangedListener -{ - - /** Field description */ - public static final String CACHE_KEY_ALL = "__all"; - - /** Field description */ - public static final String CACHE_REPOSITORY = "sonia.cache.repository"; - - /** the logger for CacheRepositoryManagerDecorator */ - private static final Logger logger = - LoggerFactory.getLogger(CacheRepositoryManagerDecorator.class); - - //~--- constructors --------------------------------------------------------- - - /** - * Constructs ... - * - * - * @param manager - * @param cacheManager - */ - @Inject - public CacheRepositoryManagerDecorator( - @Undecorated RepositoryManager manager, CacheManager cacheManager) - { - super(manager); - cache = cacheManager.getExtendedCache(String.class, Repository.class, - CACHE_REPOSITORY); - } - - //~--- methods -------------------------------------------------------------- - - /** - * Method description - * - * - * @param config - */ - @Override - public void configChanged(Object config) - { - if (logger.isDebugEnabled()) - { - logger.debug("config has changed, clear repository cache"); - } - - cache.clear(); - } - - /** - * Method description - * - * - * @param repository - * - * @throws IOException - * @throws RepositoryException - */ - @Override - public void create(Repository repository) - throws RepositoryException, IOException - { - orginal.create(repository); - putToCache(repository); - } - - /** - * Method description - * - * - * @param repository - * - * @throws IOException - * @throws RepositoryException - */ - @Override - public void delete(Repository repository) - throws RepositoryException, IOException - { - orginal.delete(repository); - removeFromCache(repository); - } - - /** - * Method description - * - * - * @param context - */ - @Override - public void init(SCMContextProvider context) - { - super.init(context); - - for (Type type : getTypes()) - { - getHandler(type.getName()).addListener(this); - } - } - - /** - * Method description - * - * - * @param repository - * - * @throws IOException - * @throws RepositoryException - */ - @Override - public void modify(Repository repository) - throws RepositoryException, IOException - { - cache.remove(repository.getId()); - orginal.modify(repository); - putToCache(repository); - } - - /** - * Method description - * - * - * @param repository - * - * @throws IOException - * @throws RepositoryException - */ - @Override - public void refresh(Repository repository) - throws RepositoryException, IOException - { - orginal.refresh(repository); - } - - //~--- get methods ---------------------------------------------------------- - - /** - * Method description - * - * - * @param id - * - * @return - */ - @Override - public Repository get(String id) - { - AssertUtil.assertIsNotEmpty(id); - - Repository repository = cache.get(id); - - if (repository == null) - { - repository = orginal.get(id); - - if (repository != null) - { - putToCache(repository); - } - } - - return repository; - } - - /** - * TODO cache result - * - * - * @param type - * @param name - * - * @return - */ - @Override - public Repository get(String type, String name) - { - return orginal.get(type, name); - } - - /** - * Method description - * - * - * @return - */ - @Override - public Collection getAll() - { - Collection repositories = cache.getCollection(CACHE_KEY_ALL); - - if (repositories == null) - { - repositories = orginal.getAll(); - cache.putCollection(CACHE_KEY_ALL, repositories); - } - - return repositories; - } - - //~--- methods -------------------------------------------------------------- - - /** - * Method description - * - * - * @param repository - */ - private void putToCache(Repository repository) - { - String id = repository.getId(); - - if (Util.isNotEmpty(id)) - { - if (logger.isDebugEnabled()) - { - logger.debug("put repository {} to cache", repository.getName()); - } - - cache.put(id, repository); - cache.removeCollection(CACHE_KEY_ALL); - } - } - - /** - * Method description - * - * - * @param repository - */ - private void removeFromCache(Repository repository) - { - if (logger.isDebugEnabled()) - { - logger.debug("remove repository {} from cache", repository.getName()); - } - - cache.remove(repository.getId()); - cache.removeCollection(CACHE_KEY_ALL); - } - - //~--- fields --------------------------------------------------------------- - - /** Field description */ - private ExtendedCache cache; -} diff --git a/scm-webapp/src/main/java/sonia/scm/cache/EhCache.java b/scm-webapp/src/main/java/sonia/scm/cache/EhCache.java index 3b16aec3c5..c234891ff2 100644 --- a/scm-webapp/src/main/java/sonia/scm/cache/EhCache.java +++ b/scm-webapp/src/main/java/sonia/scm/cache/EhCache.java @@ -29,17 +29,14 @@ * */ + + package sonia.scm.cache; //~--- non-JDK imports -------------------------------------------------------- -import net.sf.ehcache.Cache; import net.sf.ehcache.Element; -//~--- JDK imports ------------------------------------------------------------ - -import java.util.Collection; - /** * * @author Sebastian Sdorra @@ -47,7 +44,7 @@ import java.util.Collection; * @param * @param */ -public class EhCache implements ExtendedCache +public class EhCache implements Cache { /** @@ -56,7 +53,7 @@ public class EhCache implements ExtendedCache * * @param cache */ - public EhCache(Cache cache) + public EhCache(net.sf.ehcache.Cache cache) { this.cache = cache; } @@ -78,12 +75,13 @@ public class EhCache implements ExtendedCache * * * @param key - * @param value + * + * @return */ @Override - public void put(K key, V value) + public boolean contains(K key) { - cache.put(new Element(key, value)); + return cache.get(key) != null; } /** @@ -94,7 +92,7 @@ public class EhCache implements ExtendedCache * @param value */ @Override - public void putCollection(K key, Collection value) + public void put(K key, V value) { cache.put(new Element(key, value)); } @@ -113,20 +111,6 @@ public class EhCache implements ExtendedCache return cache.remove(key); } - /** - * Method description - * - * - * @param key - * - * @return - */ - @Override - public boolean removeCollection(K key) - { - return cache.remove(key); - } - //~--- get methods ---------------------------------------------------------- /** @@ -151,30 +135,8 @@ public class EhCache implements ExtendedCache return value; } - /** - * Method description - * - * - * @param key - * - * @return - */ - @Override - public Collection getCollection(K key) - { - Collection value = null; - Element el = cache.get(key); - - if (el != null) - { - value = (Collection) el.getObjectValue(); - } - - return value; - } - //~--- fields --------------------------------------------------------------- /** Field description */ - private Cache cache; + private net.sf.ehcache.Cache cache; } diff --git a/scm-webapp/src/main/java/sonia/scm/cache/EhCacheManager.java b/scm-webapp/src/main/java/sonia/scm/cache/EhCacheManager.java index a90e9c6b05..02aad0ec0c 100644 --- a/scm-webapp/src/main/java/sonia/scm/cache/EhCacheManager.java +++ b/scm-webapp/src/main/java/sonia/scm/cache/EhCacheManager.java @@ -29,6 +29,8 @@ * */ + + package sonia.scm.cache; //~--- non-JDK imports -------------------------------------------------------- @@ -73,42 +75,7 @@ public class EhCacheManager implements CacheManager * @return */ @Override - public ExtendedCache getExtendedCache(Class key, - Class value, String name) - { - return getCache(name); - } - - /** - * Method description - * - * - * @param key - * @param value - * @param name - * @param - * @param - * - * @return - */ - @Override - public SimpleCache getSimpleCache(Class key, Class value, - String name) - { - return getCache(name); - } - - /** - * Method description - * - * - * @param name - * @param - * @param - * - * @return - */ - private EhCache getCache(String name) + public Cache getCache(Class key, Class value, String name) { return new EhCache(cacheManager.getCache(name)); } diff --git a/scm-webapp/src/main/java/sonia/scm/plugin/DefaultPluginManager.java b/scm-webapp/src/main/java/sonia/scm/plugin/DefaultPluginManager.java index 54025180a1..dc465e535a 100644 --- a/scm-webapp/src/main/java/sonia/scm/plugin/DefaultPluginManager.java +++ b/scm-webapp/src/main/java/sonia/scm/plugin/DefaultPluginManager.java @@ -44,8 +44,8 @@ import org.slf4j.LoggerFactory; import sonia.scm.ConfigurationException; import sonia.scm.SCMContext; +import sonia.scm.cache.Cache; import sonia.scm.cache.CacheManager; -import sonia.scm.cache.SimpleCache; import sonia.scm.config.ScmConfiguration; import sonia.scm.security.SecurityContext; import sonia.scm.util.AssertUtil; @@ -111,8 +111,8 @@ public class DefaultPluginManager implements PluginManager { this.securityContextProvicer = securityContextProvicer; this.configuration = configuration; - this.cache = cacheManager.getSimpleCache(String.class, PluginCenter.class, - CACHE_NAME); + this.cache = cacheManager.getCache(String.class, PluginCenter.class, + CACHE_NAME); installedPlugins = new HashMap(); for (Plugin plugin : pluginLoader.getInstalledPlugins()) @@ -582,7 +582,7 @@ public class DefaultPluginManager implements PluginManager //~--- fields --------------------------------------------------------------- /** Field description */ - private SimpleCache cache; + private Cache cache; /** Field description */ private ScmConfiguration configuration; diff --git a/scm-webapp/src/main/java/sonia/scm/web/security/ChainAuthenticatonManager.java b/scm-webapp/src/main/java/sonia/scm/web/security/ChainAuthenticatonManager.java index 68d305cd55..b273b93d5e 100644 --- a/scm-webapp/src/main/java/sonia/scm/web/security/ChainAuthenticatonManager.java +++ b/scm-webapp/src/main/java/sonia/scm/web/security/ChainAuthenticatonManager.java @@ -42,8 +42,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import sonia.scm.SCMContextProvider; +import sonia.scm.cache.Cache; import sonia.scm.cache.CacheManager; -import sonia.scm.cache.SimpleCache; import sonia.scm.security.EncryptionHandler; import sonia.scm.user.User; import sonia.scm.util.AssertUtil; @@ -92,8 +92,9 @@ public class ChainAuthenticatonManager extends AbstractAuthenticationManager AssertUtil.assertIsNotNull(cacheManager); this.authenticationHandlerSet = authenticationHandlerSet; this.encryptionHandler = encryptionHandler; - this.cache = cacheManager.getSimpleCache(String.class, - AuthenticationCacheValue.class, CACHE_NAME); + this.cache = cacheManager.getCache(String.class, + AuthenticationCacheValue.class, + CACHE_NAME); } //~--- methods -------------------------------------------------------------- @@ -297,7 +298,7 @@ public class ChainAuthenticatonManager extends AbstractAuthenticationManager private Set authenticationHandlerSet; /** Field description */ - private SimpleCache cache; + private Cache cache; /** Field description */ private EncryptionHandler encryptionHandler;