aboutsummaryrefslogtreecommitdiff
path: root/stage1/KeyLockedLazyCache.scala
diff options
context:
space:
mode:
authorChristopher Vogt <oss.nsp@cvogt.org>2016-03-14 23:00:55 -0400
committerChristopher Vogt <oss.nsp@cvogt.org>2016-03-19 21:13:49 -0400
commit957ac4a3080383135738a9ce90790ef7692bb3c0 (patch)
tree4215e3b959df3c2b292aec6bd9d75ddf246eb1f1 /stage1/KeyLockedLazyCache.scala
parent48780e341b8c094548641ba986f8a2b77f10d1da (diff)
downloadcbt-957ac4a3080383135738a9ce90790ef7692bb3c0.tar.gz
cbt-957ac4a3080383135738a9ce90790ef7692bb3c0.tar.bz2
cbt-957ac4a3080383135738a9ce90790ef7692bb3c0.zip
move KeyLockedLazyCache to its own file
Diffstat (limited to 'stage1/KeyLockedLazyCache.scala')
-rw-r--r--stage1/KeyLockedLazyCache.scala47
1 files changed, 47 insertions, 0 deletions
diff --git a/stage1/KeyLockedLazyCache.scala b/stage1/KeyLockedLazyCache.scala
new file mode 100644
index 0000000..50c6f9e
--- /dev/null
+++ b/stage1/KeyLockedLazyCache.scala
@@ -0,0 +1,47 @@
+package cbt
+
+import java.util.concurrent.ConcurrentHashMap
+
+private[cbt] class LockableKey
+/**
+A cache 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],
+ logger: Option[Logger]
+){
+ def get( key: Key, value: => Value ): Value = {
+ val lockableKey = keys.synchronized{
+ if( ! (keys containsKey key) ){
+ val lockableKey = new LockableKey
+ //logger.foreach(_.resolver("CACHE MISS: " ++ key.toString))
+ keys.put( key, lockableKey )
+ lockableKey
+ } else {
+ val lockableKey = keys get key
+ //logger.foreach(_.resolver("CACHE HIT: " ++ lockableKey.toString ++ " -> " ++ key.toString))
+ lockableKey
+ }
+ }
+ import collection.JavaConversions._
+ //logger.resolver("CACHE: \n" ++ keys.mkString("\n"))
+ // synchronizing on key only, so asking for a particular key does
+ // not block the whole cache, but just that cache entry
+ key.synchronized{
+ if( ! (values containsKey lockableKey) ){
+ values.put( lockableKey, value )
+ }
+ values get lockableKey
+ }
+ }
+ def remove( key: Key ) = keys.synchronized{
+ assert(keys containsKey key)
+ val lockableKey = keys get key
+ keys.remove( key )
+ assert(values containsKey lockableKey)
+ values.remove( lockableKey )
+ }
+}