mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-07 14:05:44 +01:00
improve cache api
This commit is contained in:
@@ -29,6 +29,8 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.cache;
|
||||
|
||||
/**
|
||||
@@ -38,7 +40,7 @@ package sonia.scm.cache;
|
||||
* @param <K>
|
||||
* @param <V>
|
||||
*/
|
||||
public interface SimpleCache<K, V>
|
||||
public interface Cache<K, V>
|
||||
{
|
||||
|
||||
/**
|
||||
@@ -47,6 +49,14 @@ public interface SimpleCache<K, V>
|
||||
*/
|
||||
public void clear();
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param key
|
||||
*/
|
||||
public boolean contains(K key);
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -29,6 +29,8 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.cache;
|
||||
|
||||
/**
|
||||
@@ -50,21 +52,6 @@ public interface CacheManager
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public <K, V> ExtendedCache<K, V> getExtendedCache(Class<K> key,
|
||||
Class<V> value, String name);
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @param name
|
||||
* @param <K>
|
||||
* @param <V>
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public <K, V> SimpleCache<K, V> getSimpleCache(Class<K> key, Class<V> value,
|
||||
public <K, V> Cache<K, V> getCache(Class<K> key, Class<V> value,
|
||||
String name);
|
||||
}
|
||||
|
||||
@@ -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 <K>
|
||||
* @param <V>
|
||||
*/
|
||||
public interface ExtendedCache<K, V> extends SimpleCache<K, V>
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
*/
|
||||
public void putCollection(K key, Collection<V> value);
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param key
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean removeCollection(K key);
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param key
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Collection<V> getCollection(K key);
|
||||
}
|
||||
@@ -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<Repository> getAll()
|
||||
{
|
||||
Collection<Repository> 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<String, Repository> cache;
|
||||
}
|
||||
@@ -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 <K>
|
||||
* @param <V>
|
||||
*/
|
||||
public class EhCache<K, V> implements ExtendedCache<K, V>
|
||||
public class EhCache<K, V> implements Cache<K, V>
|
||||
{
|
||||
|
||||
/**
|
||||
@@ -56,7 +53,7 @@ public class EhCache<K, V> implements ExtendedCache<K, V>
|
||||
*
|
||||
* @param cache
|
||||
*/
|
||||
public EhCache(Cache cache)
|
||||
public EhCache(net.sf.ehcache.Cache cache)
|
||||
{
|
||||
this.cache = cache;
|
||||
}
|
||||
@@ -78,12 +75,13 @@ public class EhCache<K, V> implements ExtendedCache<K, V>
|
||||
*
|
||||
*
|
||||
* @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<K, V> implements ExtendedCache<K, V>
|
||||
* @param value
|
||||
*/
|
||||
@Override
|
||||
public void putCollection(K key, Collection<V> value)
|
||||
public void put(K key, V value)
|
||||
{
|
||||
cache.put(new Element(key, value));
|
||||
}
|
||||
@@ -113,20 +111,6 @@ public class EhCache<K, V> implements ExtendedCache<K, V>
|
||||
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<K, V> implements ExtendedCache<K, V>
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param key
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Collection<V> getCollection(K key)
|
||||
{
|
||||
Collection<V> value = null;
|
||||
Element el = cache.get(key);
|
||||
|
||||
if (el != null)
|
||||
{
|
||||
value = (Collection<V>) el.getObjectValue();
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private Cache cache;
|
||||
private net.sf.ehcache.Cache cache;
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.cache;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
@@ -73,42 +75,7 @@ public class EhCacheManager implements CacheManager
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public <K, V> ExtendedCache<K, V> getExtendedCache(Class<K> key,
|
||||
Class<V> value, String name)
|
||||
{
|
||||
return getCache(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @param name
|
||||
* @param <K>
|
||||
* @param <V>
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public <K, V> SimpleCache<K, V> getSimpleCache(Class<K> key, Class<V> value,
|
||||
String name)
|
||||
{
|
||||
return getCache(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param name
|
||||
* @param <K>
|
||||
* @param <V>
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private <K, V> EhCache<K, V> getCache(String name)
|
||||
public <K, V> Cache<K, V> getCache(Class<K> key, Class<V> value, String name)
|
||||
{
|
||||
return new EhCache<K, V>(cacheManager.getCache(name));
|
||||
}
|
||||
|
||||
@@ -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<String, Plugin>();
|
||||
|
||||
for (Plugin plugin : pluginLoader.getInstalledPlugins())
|
||||
@@ -582,7 +582,7 @@ public class DefaultPluginManager implements PluginManager
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private SimpleCache<String, PluginCenter> cache;
|
||||
private Cache<String, PluginCenter> cache;
|
||||
|
||||
/** Field description */
|
||||
private ScmConfiguration configuration;
|
||||
|
||||
@@ -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<AuthenticationHandler> authenticationHandlerSet;
|
||||
|
||||
/** Field description */
|
||||
private SimpleCache<String, AuthenticationCacheValue> cache;
|
||||
private Cache<String, AuthenticationCacheValue> cache;
|
||||
|
||||
/** Field description */
|
||||
private EncryptionHandler encryptionHandler;
|
||||
|
||||
Reference in New Issue
Block a user