Created
August 25, 2012 17:05
-
-
Save ericacm/3467993 to your computer and use it in GitHub Desktop.
Auto Updating Caching System - CacheActor.scala
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
abstract class CacheActor[V](cacheSystem: CacheSystem) | |
extends Actor with Logging { | |
def findValueReceive: Receive = { | |
case FindValue(params) => findValueForSender(params, sender) | |
} | |
def findValueForSender(params: Params, sender: ActorRef) { | |
val key = params.cacheKey | |
val elem = cache.get(key) | |
if (elem != null) { | |
sender ! elem.getObjectValue.asInstanceOf[V] | |
} else { | |
// Implicit ExecutionContext for future | |
import CacheActor._ | |
Future { findObject(params) } pipeTo sender | |
} | |
} | |
def findObject(params: Params): Option[V] = { | |
cacheSystem.findObjectForCache(params.cacheKey, cache, | |
finder(params)) | |
} | |
// Provided by subclasses | |
val cache: Cache | |
def finder(params: Params): () => V | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment