modify cache api to match the one from shiro

This commit is contained in:
Sebastian Sdorra
2014-02-19 19:59:44 +01:00
parent ffc12f2fa1
commit 5715851ff1
6 changed files with 197 additions and 55 deletions

View File

@@ -37,10 +37,14 @@ package sonia.scm.cache;
import com.google.common.base.Predicate;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
//~--- JDK imports ------------------------------------------------------------
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
/**
*
@@ -50,7 +54,8 @@ import java.util.Map;
* @param <K>
* @param <V>
*/
public class MapCache<K, V> implements Cache<K, V>
public class MapCache<K, V>
implements Cache<K, V>, org.apache.shiro.cache.Cache<K, V>
{
/**
@@ -81,13 +86,27 @@ public class MapCache<K, V> implements Cache<K, V>
* Method description
*
*
* @param key
* @param value
* @return
*/
@Override
public void put(K key, V value)
public Set<K> keys()
{
map.put(key, value);
return Collections.unmodifiableSet(map.keySet());
}
/**
* Method description
*
*
* @param key
* @param value
*
* @return
*/
@Override
public V put(K key, V value)
{
return map.put(key, value);
}
/**
@@ -99,9 +118,9 @@ public class MapCache<K, V> implements Cache<K, V>
* @return
*/
@Override
public boolean remove(K key)
public V remove(K key)
{
return map.remove(key) != null;
return map.remove(key);
}
/**
@@ -113,22 +132,43 @@ public class MapCache<K, V> implements Cache<K, V>
* @return
*/
@Override
public boolean removeAll(Predicate<K> filter)
public Iterable<V> removeAll(Predicate<K> filter)
{
boolean result = false;
Set<V> values = Sets.newHashSet();
for (K key : map.keySet())
{
if (filter.apply(key))
{
if (remove(key))
{
result = true;
}
values.add(remove(key));
}
}
return result;
return values;
}
/**
* Method description
*
*
* @return
*/
@Override
public int size()
{
return map.size();
}
/**
* Method description
*
*
* @return
*/
@Override
public Collection<V> values()
{
return Collections.unmodifiableCollection(map.values());
}
//~--- get methods ----------------------------------------------------------

View File

@@ -30,6 +30,7 @@
*/
package sonia.scm.cache;
//~--- non-JDK imports --------------------------------------------------------
@@ -47,7 +48,8 @@ import java.util.Map;
* @author Sebastian Sdorra
* @since 1.17
*/
public class MapCacheManager implements CacheManager
public class MapCacheManager
implements CacheManager, org.apache.shiro.cache.CacheManager
{
/**
@@ -76,9 +78,9 @@ public class MapCacheManager implements CacheManager
* @return
*/
@Override
public <K, V> Cache<K, V> getCache(String name)
public synchronized <K, V> MapCache<K, V> getCache(String name)
{
Cache<K, V> cache = cacheMap.get(name);
MapCache<K, V> cache = cacheMap.get(name);
if (cache == null)
{
@@ -89,10 +91,9 @@ public class MapCacheManager implements CacheManager
return cache;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
@SuppressWarnings("unchecked")
private final Map<String, Cache> cacheMap = Maps.newHashMap();
private final Map<String, MapCache> cacheMap = Maps.newHashMap();
}