Skip to content

Instantly share code, notes, and snippets.

@ericacm
Created August 25, 2012 17:05
Show Gist options
  • Save ericacm/3467993 to your computer and use it in GitHub Desktop.
Save ericacm/3467993 to your computer and use it in GitHub Desktop.
Auto Updating Caching System - CacheActor.scala
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