mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 16:35:45 +01:00
created adapter between scm and shiro caches, see issue #781
This commit is contained in:
113
scm-webapp/src/main/java/sonia/scm/cache/GuavaBaseCache.java
vendored
Normal file
113
scm-webapp/src/main/java/sonia/scm/cache/GuavaBaseCache.java
vendored
Normal file
@@ -0,0 +1,113 @@
|
||||
/**
|
||||
* Copyright (c) 2014, 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;
|
||||
|
||||
import com.google.common.cache.Cache;
|
||||
import com.google.common.collect.Sets;
|
||||
import java.util.Set;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.Filter;
|
||||
|
||||
/**
|
||||
* Implementation of basic cache methods for guava based caches.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*
|
||||
* @since 1.52
|
||||
*
|
||||
* @param <K> key
|
||||
* @param <V> value
|
||||
*/
|
||||
public abstract class GuavaBaseCache<K, V> {
|
||||
|
||||
/**
|
||||
* the logger for GuavaBaseCache
|
||||
*/
|
||||
private static final Logger logger = LoggerFactory.getLogger(GuavaCache.class);
|
||||
|
||||
protected com.google.common.cache.Cache<K, V> cache;
|
||||
protected CopyStrategy copyStrategy = CopyStrategy.NONE;
|
||||
private final String name;
|
||||
|
||||
GuavaBaseCache(com.google.common.cache.Cache<K, V> cache, CopyStrategy copyStrategy, String name) {
|
||||
this.cache = cache;
|
||||
this.name = name;
|
||||
|
||||
if (copyStrategy != null) {
|
||||
this.copyStrategy = copyStrategy;
|
||||
}
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
public void clear() {
|
||||
logger.debug("clear cache {}", name);
|
||||
cache.invalidateAll();
|
||||
}
|
||||
|
||||
public boolean contains(K key) {
|
||||
return cache.getIfPresent(key) != null;
|
||||
}
|
||||
|
||||
public boolean removeAll(Filter<K> filter) {
|
||||
Set<K> keysToRemove = Sets.newHashSet();
|
||||
|
||||
for (K key : cache.asMap().keySet()) {
|
||||
if (filter.accept(key)) {
|
||||
keysToRemove.add(key);
|
||||
}
|
||||
}
|
||||
|
||||
boolean result = false;
|
||||
|
||||
if (!keysToRemove.isEmpty()) {
|
||||
cache.invalidateAll(keysToRemove);
|
||||
result = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public V get(K key) {
|
||||
V value = cache.getIfPresent(key);
|
||||
|
||||
if (value != null) {
|
||||
value = copyStrategy.copyOnRead(value);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public Cache<K, V> getWrappedCache() {
|
||||
return cache;
|
||||
}
|
||||
}
|
||||
@@ -34,18 +34,6 @@ package sonia.scm.cache;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.Filter;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
@@ -53,185 +41,22 @@ import java.util.Set;
|
||||
* @param <K>
|
||||
* @param <V>
|
||||
*/
|
||||
public class GuavaCache<K, V> implements Cache<K, V>
|
||||
{
|
||||
public class GuavaCache<K, V> extends GuavaBaseCache<K, V> implements Cache<K, V> {
|
||||
|
||||
/**
|
||||
* the logger for GuavaCache
|
||||
*/
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(GuavaCache.class);
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param configuration
|
||||
*/
|
||||
public GuavaCache(GuavaNamedCacheConfiguration configuration)
|
||||
{
|
||||
this(configuration, configuration.getName());
|
||||
GuavaCache(com.google.common.cache.Cache<K, V> cache, CopyStrategy copyStrategy, String name) {
|
||||
super(cache, copyStrategy, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param configuration
|
||||
* @param name
|
||||
*/
|
||||
public GuavaCache(GuavaCacheConfiguration configuration, String name)
|
||||
{
|
||||
this(GuavaCaches.create(configuration, name),
|
||||
configuration.getCopyStrategy(), name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param cache
|
||||
* @param copyStrategy
|
||||
* @param name
|
||||
*/
|
||||
@VisibleForTesting
|
||||
protected GuavaCache(com.google.common.cache.Cache<K, V> cache,
|
||||
CopyStrategy copyStrategy, String name)
|
||||
{
|
||||
this.cache = cache;
|
||||
this.name = name;
|
||||
|
||||
if (copyStrategy != null)
|
||||
{
|
||||
this.copyStrategy = copyStrategy;
|
||||
}
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void clear()
|
||||
{
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("clear cache {}", name);
|
||||
}
|
||||
|
||||
cache.invalidateAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param key
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean contains(K key)
|
||||
{
|
||||
return cache.getIfPresent(key) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
*/
|
||||
@Override
|
||||
public void put(K key, V value)
|
||||
{
|
||||
public void put(K key, V value) {
|
||||
cache.put(key, copyStrategy.copyOnWrite(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param key
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean remove(K key)
|
||||
{
|
||||
public boolean remove(K key) {
|
||||
cache.invalidate(key);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param filter
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean removeAll(Filter<K> filter)
|
||||
{
|
||||
Set<K> keysToRemove = Sets.newHashSet();
|
||||
|
||||
for (K key : cache.asMap().keySet())
|
||||
{
|
||||
if (filter.accept(key))
|
||||
{
|
||||
keysToRemove.add(key);
|
||||
}
|
||||
}
|
||||
|
||||
boolean result = false;
|
||||
|
||||
if (!keysToRemove.isEmpty())
|
||||
{
|
||||
cache.invalidateAll(keysToRemove);
|
||||
result = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param key
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public V get(K key)
|
||||
{
|
||||
V value = cache.getIfPresent(key);
|
||||
|
||||
if (value != null)
|
||||
{
|
||||
value = copyStrategy.copyOnRead(value);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private com.google.common.cache.Cache<K, V> cache;
|
||||
|
||||
/** Field description */
|
||||
private CopyStrategy copyStrategy = CopyStrategy.NONE;
|
||||
|
||||
/** Field description */
|
||||
private String name;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -47,66 +47,52 @@ import java.io.IOException;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.shiro.cache.CacheException;
|
||||
|
||||
/**
|
||||
*
|
||||
* Guava based implementation of {@link CacheManager} and {@link org.apache.shiro.cache.CacheManager}.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@Singleton
|
||||
public class GuavaCacheManager implements CacheManager
|
||||
public class GuavaCacheManager implements CacheManager, org.apache.shiro.cache.CacheManager
|
||||
{
|
||||
|
||||
/**
|
||||
* the logger for GuavaCacheManager
|
||||
*/
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(GuavaCacheManager.class);
|
||||
private static final Logger logger = LoggerFactory.getLogger(GuavaCacheManager.class);
|
||||
|
||||
private volatile Map<String, CacheWithConfiguration> cacheMap = Maps.newHashMap();
|
||||
|
||||
private GuavaCacheConfiguration defaultConfiguration;
|
||||
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*/
|
||||
public GuavaCacheManager()
|
||||
{
|
||||
public GuavaCacheManager() {
|
||||
this(GuavaCacheConfigurationReader.read());
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param config
|
||||
*/
|
||||
@VisibleForTesting
|
||||
protected GuavaCacheManager(GuavaCacheManagerConfiguration config)
|
||||
{
|
||||
protected GuavaCacheManager(GuavaCacheManagerConfiguration config) {
|
||||
defaultConfiguration = config.getDefaultCache();
|
||||
|
||||
for (GuavaNamedCacheConfiguration ncc : config.getCaches())
|
||||
{
|
||||
logger.debug("create cache {} from configured configuration {}",
|
||||
ncc.getName(), ncc);
|
||||
cacheMap.put(ncc.getName(), new GuavaCache(ncc));
|
||||
for (GuavaNamedCacheConfiguration ncc : config.getCaches()) {
|
||||
logger.debug("create cache {} from configured configuration {}", ncc.getName(), ncc);
|
||||
cacheMap.put(ncc.getName(), new CacheWithConfiguration(
|
||||
GuavaCaches.create(defaultConfiguration, ncc.getName()),
|
||||
defaultConfiguration)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Override
|
||||
public void close() throws IOException
|
||||
{
|
||||
public void close() throws IOException {
|
||||
logger.info("close guava cache manager");
|
||||
|
||||
for (Cache c : cacheMap.values())
|
||||
{
|
||||
c.clear();
|
||||
for (CacheWithConfiguration c : cacheMap.values()) {
|
||||
c.cache.invalidateAll();
|
||||
}
|
||||
|
||||
cacheMap.clear();
|
||||
@@ -114,43 +100,44 @@ public class GuavaCacheManager implements CacheManager
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @param name
|
||||
* @param <K>
|
||||
* @param <V>
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public synchronized <K, V> GuavaCache<K, V> getCache(Class<K> key,
|
||||
Class<V> value, String name)
|
||||
{
|
||||
private synchronized <K, V> CacheWithConfiguration<K, V> getCacheWithConfiguration(String name) {
|
||||
logger.trace("try to retrieve cache {}", name);
|
||||
|
||||
GuavaCache<K, V> cache = cacheMap.get(name);
|
||||
CacheWithConfiguration<K, V> cache = cacheMap.get(name);
|
||||
|
||||
if (cache == null)
|
||||
{
|
||||
if (cache == null) {
|
||||
logger.debug(
|
||||
"cache {} does not exists, creating a new instance from default configuration: {}",
|
||||
name, defaultConfiguration);
|
||||
cache = new GuavaCache<K, V>(defaultConfiguration, name);
|
||||
name, defaultConfiguration
|
||||
);
|
||||
cache = new CacheWithConfiguration(GuavaCaches.create(defaultConfiguration, name), defaultConfiguration);
|
||||
cacheMap.put(name, cache);
|
||||
}
|
||||
|
||||
|
||||
return cache;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <K, V> GuavaCache<K, V> getCache(Class<K> key, Class<V> value, String name) {
|
||||
CacheWithConfiguration<K, V> cw = getCacheWithConfiguration(name);
|
||||
return new GuavaCache<>(cw.cache, cw.configuration.getCopyStrategy(), name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <K, V> GuavaSecurityCache<K, V> getCache(String name) throws CacheException {
|
||||
CacheWithConfiguration<K, V> cw = getCacheWithConfiguration(name);
|
||||
return new GuavaSecurityCache<>(cw.cache, cw.configuration.getCopyStrategy(), name);
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
private static class CacheWithConfiguration<K,V> {
|
||||
|
||||
private final com.google.common.cache.Cache<K,V> cache;
|
||||
private final GuavaCacheConfiguration configuration;
|
||||
|
||||
/** Field description */
|
||||
private volatile Map<String, GuavaCache> cacheMap = Maps.newHashMap();
|
||||
|
||||
/** Field description */
|
||||
private GuavaCacheConfiguration defaultConfiguration;
|
||||
private CacheWithConfiguration(com.google.common.cache.Cache<K, V> cache, GuavaCacheConfiguration configuration) {
|
||||
this.cache = cache;
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
83
scm-webapp/src/main/java/sonia/scm/cache/GuavaSecurityCache.java
vendored
Normal file
83
scm-webapp/src/main/java/sonia/scm/cache/GuavaSecurityCache.java
vendored
Normal file
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
* Copyright (c) 2014, 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;
|
||||
|
||||
import com.google.common.cache.Cache;
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
import org.apache.shiro.cache.CacheException;
|
||||
|
||||
/**
|
||||
* Guava based implementation of {@link org.apache.shiro.cache.Cache}.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*
|
||||
* @since 1.52
|
||||
*
|
||||
* @param <K>
|
||||
* @param <V>
|
||||
*/
|
||||
public class GuavaSecurityCache<K, V> extends GuavaBaseCache<K, V> implements org.apache.shiro.cache.Cache<K, V> {
|
||||
|
||||
GuavaSecurityCache(Cache<K, V> cache, CopyStrategy copyStrategy, String name) {
|
||||
super(cache, copyStrategy, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V put(K key, V value) throws CacheException {
|
||||
V previousValue = cache.getIfPresent(key);
|
||||
cache.put(key, value);
|
||||
return previousValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V remove(K key) throws CacheException {
|
||||
V previousValue = cache.getIfPresent(key);
|
||||
cache.invalidate(key);
|
||||
return previousValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return (int) cache.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<K> keys() {
|
||||
return cache.asMap().keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<V> values() {
|
||||
return cache.asMap().values();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user