-
Notifications
You must be signed in to change notification settings - Fork 884
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement concurrent cache of generated classes, improve performance …
…of Enhancer
- Loading branch information
Showing
6 changed files
with
449 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package net.sf.cglib.core.internal; | ||
|
||
public interface Function<K, V> { | ||
V apply(K key); | ||
} |
86 changes: 86 additions & 0 deletions
86
cglib/src/main/java/net/sf/cglib/core/internal/LoadingCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package net.sf.cglib.core.internal; | ||
|
||
import java.util.concurrent.*; | ||
|
||
public class LoadingCache<K, KK, V> { | ||
protected final ConcurrentMap<KK, Object> map; | ||
protected final Function<K, V> loader; | ||
protected final Function<K, KK> keyMapper; | ||
|
||
public static final Function IDENTITY = new Function() { | ||
public Object apply(Object key) { | ||
return key; | ||
} | ||
}; | ||
|
||
public LoadingCache(Function<K, KK> keyMapper, Function<K, V> loader) { | ||
this.keyMapper = keyMapper; | ||
this.loader = loader; | ||
this.map = new ConcurrentHashMap<KK, Object>(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public static <K> Function<K, K> identity() { | ||
return IDENTITY; | ||
} | ||
|
||
public V get(K key) { | ||
final KK cacheKey = keyMapper.apply(key); | ||
Object v = map.get(cacheKey); | ||
if (v != null && !(v instanceof FutureTask)) { | ||
return (V) v; | ||
} | ||
|
||
return createEntry(key, cacheKey, v); | ||
} | ||
|
||
/** | ||
* Loads entry to the cache. | ||
* If entry is missing, put {@link FutureTask} first so other competing thread might wait for the result. | ||
* @param key original key that would be used to load the instance | ||
* @param cacheKey key that would be used to store the entry in internal map | ||
* @param v null or {@link FutureTask<V>} | ||
* @return newly created instance | ||
*/ | ||
protected V createEntry(final K key, KK cacheKey, Object v) { | ||
FutureTask<V> task; | ||
boolean creator = false; | ||
if (v != null) { | ||
// Another thread is already loading an instance | ||
task = (FutureTask<V>) v; | ||
} else { | ||
task = new FutureTask<V>(new Callable<V>() { | ||
public V call() throws Exception { | ||
return loader.apply(key); | ||
} | ||
}); | ||
Object prevTask = map.putIfAbsent(cacheKey, task); | ||
if (prevTask == null) { | ||
// creator does the load | ||
creator = true; | ||
task.run(); | ||
} else if (prevTask instanceof FutureTask) { | ||
task = (FutureTask<V>) prevTask; | ||
} else { | ||
return (V) prevTask; | ||
} | ||
} | ||
|
||
V result; | ||
try { | ||
result = task.get(); | ||
} catch (InterruptedException e) { | ||
throw new IllegalStateException("Interrupted while loading cache item", e); | ||
} catch (ExecutionException e) { | ||
Throwable cause = e.getCause(); | ||
if (cause instanceof RuntimeException) { | ||
throw ((RuntimeException) cause); | ||
} | ||
throw new IllegalStateException("Unable to load cache item", cause); | ||
} | ||
if (creator) { | ||
map.put(cacheKey, result); | ||
} | ||
return result; | ||
} | ||
} |
Oops, something went wrong.