Author: mcucchiara
Date: Thu Oct 20 15:36:22 2011
New Revision: 1186855
URL: http://svn.apache.org/viewvc?rev=1186855&view=rev
Log:
New HashMap Cache
Added:
commons/proper/ognl/branches/new-cache-approach/src/main/java/org/apache/commons/ognl/internal/HashMapCache.java
- copied, changed from r1185552,
commons/proper/ognl/branches/new-cache-approach/src/main/java/org/apache/commons/ognl/internal/ConcurrentHashMapCache.java
Copied:
commons/proper/ognl/branches/new-cache-approach/src/main/java/org/apache/commons/ognl/internal/HashMapCache.java
(from r1185552,
commons/proper/ognl/branches/new-cache-approach/src/main/java/org/apache/commons/ognl/internal/ConcurrentHashMapCache.java)
URL:
http://svn.apache.org/viewvc/commons/proper/ognl/branches/new-cache-approach/src/main/java/org/apache/commons/ognl/internal/HashMapCache.java?p2=commons/proper/ognl/branches/new-cache-approach/src/main/java/org/apache/commons/ognl/internal/HashMapCache.java&p1=commons/proper/ognl/branches/new-cache-approach/src/main/java/org/apache/commons/ognl/internal/ConcurrentHashMapCache.java&r1=1185552&r2=1186855&rev=1186855&view=diff
==============================================================================
---
commons/proper/ognl/branches/new-cache-approach/src/main/java/org/apache/commons/ognl/internal/ConcurrentHashMapCache.java
(original)
+++
commons/proper/ognl/branches/new-cache-approach/src/main/java/org/apache/commons/ognl/internal/HashMapCache.java
Thu Oct 20 15:36:22 2011
@@ -23,41 +23,52 @@ package org.apache.commons.ognl.internal
import org.apache.commons.ognl.internal.entry.CacheEntryFactory;
-import java.util.concurrent.ConcurrentHashMap;
+import java.util.HashMap;
+import java.util.Map;
-public class ConcurrentHashMapCache<K, V>
+public class HashMapCache<K, V>
implements Cache<K, V>
{
- private ConcurrentHashMap<K, V> cache = new ConcurrentHashMap<K, V>( );
+ private final Map<K, V> cache = new HashMap<K, V>( 512 );
private CacheEntryFactory<K, V> cacheEntryFactory;
- public ConcurrentHashMapCache( )
- {
- }
-
- public ConcurrentHashMapCache( CacheEntryFactory<K, V> cacheEntryFactory )
+ public HashMapCache( CacheEntryFactory<K, V> cacheEntryFactory )
{
this.cacheEntryFactory = cacheEntryFactory;
}
public void clear( )
{
- cache.clear( );
+ synchronized ( cache )
+ {
+ cache.clear( );
+ }
}
public int getSize( )
{
- return cache.size( );
+ synchronized ( cache )
+ {
+ return cache.size( );
+ }
}
- public V get( K key)
+ public V get( K key )
throws CacheException
{
V v = cache.get( key );
if ( shouldCreate( cacheEntryFactory, v ) )
{
- return put( key, cacheEntryFactory.create( key ) );
+ synchronized ( cache )
+ {
+ v = cache.get( key );
+ if ( v != null )
+ {
+ return v;
+ }
+ return put( key, cacheEntryFactory.create( key ) );
+ }
}
return v;
}
@@ -77,16 +88,16 @@ public class ConcurrentHashMapCache<K, V
public V put( K key, V value )
{
- V collision = cache.putIfAbsent( key, value );
- if ( collision != null )
+ synchronized ( cache )
{
- return collision;
+ cache.put( key, value );
+ return value;
}
- return value;
}
- public boolean contains( K key)
+
+ public boolean contains( K key )
{
- return this.cache.contains( key );
+ return this.cache.containsKey( key );
}
}