You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shiro.apache.org by lh...@apache.org on 2009/12/22 18:38:57 UTC
svn commit: r893244 - in
/incubator/shiro/trunk/core/src/main/java/org/apache/shiro/cache:
DefaultCacheManager.java SoftHashMapCache.java
Author: lhazlewood
Date: Tue Dec 22 17:38:56 2009
New Revision: 893244
URL: http://svn.apache.org/viewvc?rev=893244&view=rev
Log:
Changed DefaultCacheManager to use a ConcurrentMap to manage Cache instances. SoftHashMapCache now implements Destroyable, clearing out its memory at shutdown.
Modified:
incubator/shiro/trunk/core/src/main/java/org/apache/shiro/cache/DefaultCacheManager.java
incubator/shiro/trunk/core/src/main/java/org/apache/shiro/cache/SoftHashMapCache.java
Modified: incubator/shiro/trunk/core/src/main/java/org/apache/shiro/cache/DefaultCacheManager.java
URL: http://svn.apache.org/viewvc/incubator/shiro/trunk/core/src/main/java/org/apache/shiro/cache/DefaultCacheManager.java?rev=893244&r1=893243&r2=893244&view=diff
==============================================================================
--- incubator/shiro/trunk/core/src/main/java/org/apache/shiro/cache/DefaultCacheManager.java (original)
+++ incubator/shiro/trunk/core/src/main/java/org/apache/shiro/cache/DefaultCacheManager.java Tue Dec 22 17:38:56 2009
@@ -18,20 +18,21 @@
*/
package org.apache.shiro.cache;
-import java.util.HashMap;
-import java.util.Map;
-
import org.apache.shiro.util.Destroyable;
import org.apache.shiro.util.LifecycleUtils;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
/**
* Default memory-only based {@link CacheManager CacheManager} implementation usable in production environments.
* <p/>
* This implementation does not offer any enterprise-level features such as cache coherency, optimistic locking,
- * failover or other similar features. It relies on memory-based {@link java.util.Map Map} caches. For more
- * enterprise features, consider using an {@link org.apache.shiro.cache.ehcache.EhCacheManager EhCacheManager} or other
- * similar implementation that wraps an enterprise-grade Caching solution.
+ * failover or other similar features. It relies on memory-based {@link SoftHashMapCache} instances to ensure there
+ * are no memory leaks. For more enterprise features, consider using an
+ * {@code org.apache.shiro.cache.ehcache.EhCacheManager} or other similar implementation that wraps an enterprise-grade
+ * Caching solution.
*
* @author Les Hazlewood
* @since 1.0
@@ -41,7 +42,7 @@
/**
* Retains all Cache objects maintained by this cache manager.
*/
- private final Map<String, Cache> caches = new HashMap<String, Cache>();
+ private final ConcurrentMap<String, Cache> caches = new ConcurrentHashMap<String, Cache>();
public Cache getCache(String name) throws CacheException {
if (name == null) {
@@ -50,11 +51,12 @@
Cache cache;
- synchronized (caches) {
- cache = caches.get(name);
- if (cache == null) {
- cache = new SoftHashMapCache(name);
- caches.put(name, cache);
+ cache = caches.get(name);
+ if (cache == null) {
+ cache = new SoftHashMapCache(name);
+ Cache existing = caches.putIfAbsent(name, cache);
+ if (existing != null) {
+ cache = existing;
}
}
Modified: incubator/shiro/trunk/core/src/main/java/org/apache/shiro/cache/SoftHashMapCache.java
URL: http://svn.apache.org/viewvc/incubator/shiro/trunk/core/src/main/java/org/apache/shiro/cache/SoftHashMapCache.java?rev=893244&r1=893243&r2=893244&view=diff
==============================================================================
--- incubator/shiro/trunk/core/src/main/java/org/apache/shiro/cache/SoftHashMapCache.java (original)
+++ incubator/shiro/trunk/core/src/main/java/org/apache/shiro/cache/SoftHashMapCache.java Tue Dec 22 17:38:56 2009
@@ -18,6 +18,7 @@
*/
package org.apache.shiro.cache;
+import org.apache.shiro.util.Destroyable;
import org.apache.shiro.util.SoftHashMap;
/**
@@ -29,7 +30,7 @@
* @author Les Hazlewood
* @since 1.0
*/
-public class SoftHashMapCache extends MapCache {
+public class SoftHashMapCache extends MapCache implements Destroyable {
/**
* Creates a new <code>SoftHashMapCache</code> instance with the specified name.
@@ -41,4 +42,11 @@
public SoftHashMapCache(String name) {
super(name, new SoftHashMap());
}
+
+ /**
+ * Calls {@link #clear} to remove all entries.
+ */
+ public void destroy() {
+ clear();
+ }
}