aboutsummaryrefslogtreecommitdiff
path: root/stage1/KeyLockedLazyCache.scala
diff options
context:
space:
mode:
authorChristopher Vogt <oss.nsp@cvogt.org>2016-11-25 16:48:28 -0500
committerChristopher Vogt <oss.nsp@cvogt.org>2017-02-01 23:10:48 -0500
commit00d9485f5597fdecc58461bd81df635fafbe494f (patch)
tree026f7f143d8cf5ae69e7afaa452d03180d3e04a8 /stage1/KeyLockedLazyCache.scala
parent8939ebef01ae7a665781d99331e4d13e7b875a96 (diff)
downloadcbt-00d9485f5597fdecc58461bd81df635fafbe494f.tar.gz
cbt-00d9485f5597fdecc58461bd81df635fafbe494f.tar.bz2
cbt-00d9485f5597fdecc58461bd81df635fafbe494f.zip
Merge separate hashmaps for persistent cache into one
This isn’t type-safe, but re-using that same hashmap for both keys and classloaders allows to reduce the number of members in Context. Also we can re-use the same hashMap for other things as well in the coming commits, e.g. timestamps.
Diffstat (limited to 'stage1/KeyLockedLazyCache.scala')
-rw-r--r--stage1/KeyLockedLazyCache.scala51
1 files changed, 25 insertions, 26 deletions
diff --git a/stage1/KeyLockedLazyCache.scala b/stage1/KeyLockedLazyCache.scala
index 4eff5b2..2602523 100644
--- a/stage1/KeyLockedLazyCache.scala
+++ b/stage1/KeyLockedLazyCache.scala
@@ -4,59 +4,58 @@ import java.util.concurrent.ConcurrentHashMap
private[cbt] class LockableKey
/**
-A cache that lazily computes values if needed during lookup.
+A hashMap that lazily computes values if needed during lookup.
Locking occurs on the key, so separate keys can be looked up
simultaneously without a deadlock.
*/
-final private[cbt] class KeyLockedLazyCache[Key <: AnyRef,Value <: AnyRef](
- val keys: ConcurrentHashMap[Key,AnyRef],
- val values: ConcurrentHashMap[AnyRef,Value],
+final private[cbt] class KeyLockedLazyCache[T <: AnyRef](
+ val hashMap: ConcurrentHashMap[AnyRef,AnyRef],
logger: Option[Logger]
){
- def get( key: Key, value: => Value ): Value = {
- val lockableKey = keys.synchronized{
- if( ! (keys containsKey key) ){
+ def get( key: AnyRef, value: => T ): T = {
+ val lockableKey = hashMap.synchronized{
+ if( ! (hashMap containsKey key) ){
val lockableKey = new LockableKey
//logger.foreach(_.resolver("CACHE MISS: " ++ key.toString))
- keys.put( key, lockableKey )
+ hashMap.put( key, lockableKey )
lockableKey
} else {
- val lockableKey = keys get key
+ val lockableKey = hashMap get key
//logger.foreach(_.resolver("CACHE HIT: " ++ lockableKey.toString ++ " -> " ++ key.toString))
lockableKey
}
}
import collection.JavaConversions._
- //logger.resolver("CACHE: \n" ++ keys.mkString("\n"))
+ //logger.resolver("CACHE: \n" ++ hashMap.mkString("\n"))
// synchronizing on key only, so asking for a particular key does
- // not block the whole cache, but just that cache entry
+ // not block the whole hashMap, but just that hashMap entry
lockableKey.synchronized{
- if( ! (values containsKey lockableKey) ){
- values.put( lockableKey, value )
+ if( ! (hashMap containsKey lockableKey) ){
+ hashMap.put( lockableKey, value )
}
- values get lockableKey
+ (hashMap get lockableKey).asInstanceOf[T]
}
}
- def update( key: Key, value: Value ): Value = {
- val lockableKey = keys get key
+ def update( key: AnyRef, value: T ): T = {
+ val lockableKey = hashMap get key
lockableKey.synchronized{
- values.put( lockableKey, value )
+ hashMap.put( lockableKey, value )
value
}
}
- def remove( key: Key ) = keys.synchronized{
- assert(keys containsKey key)
- val lockableKey = keys get key
+ def remove( key: AnyRef ) = hashMap.synchronized{
+ assert(hashMap containsKey key)
+ val lockableKey = hashMap get key
lockableKey.synchronized{
- if(values containsKey lockableKey){
- // this is so values in the process of being replaced (which mean they have a key but no value)
+ if(hashMap containsKey lockableKey){
+ // this is so hashMap in the process of being replaced (which mean they have a key but no value)
// are not being removed
- keys.remove( key )
- values.remove( lockableKey )
+ hashMap.remove( key )
+ hashMap.remove( lockableKey )
}
}
}
- def containsKey( key: Key ) = keys.synchronized{
- keys containsKey key
+ def containsKey( key: AnyRef ) = hashMap.synchronized{
+ hashMap containsKey key
}
}