Use an actor to prevent cache refresh concurrency

This commit is contained in:
Thibault Duplessis 2012-05-30 23:13:32 +02:00
parent 1c2b5f125c
commit ece8a0d130

View file

@ -1,6 +1,18 @@
package lila
package game
import akka.actor._
import akka.pattern.ask
import akka.util.duration._
import akka.util.Timeout
import akka.dispatch.{ Future, Await }
import akka.pattern.ask
import akka.util.duration._
import akka.util.Timeout
import play.api.Play.current
import play.api.libs.concurrent._
import memo.Builder
final class Cached(
@ -9,14 +21,27 @@ final class Cached(
import Cached._
private val nbCache = Builder.cache[Key, Int](nbTtl, {
case NbGames gameRepo.count(_.all).unsafePerformIO
case NbMates gameRepo.count(_.mate).unsafePerformIO
})
val atMost = 5 seconds
implicit val timeout = Timeout(atMost)
def nbGames: Int = nbCache get NbGames
def nbGames: Int = get(NbGames)
def nbMates: Int = nbCache get NbMates
def nbMates: Int = get(NbMates)
private def get(key: Key): Int =
Await.result(actor ? key mapTo manifest[Int], atMost)
private val actor = Akka.system.actorOf(Props(new Actor {
private val cache = Builder.cache[Key, Int](nbTtl, {
case NbGames gameRepo.count(_.all).unsafePerformIO
case NbMates gameRepo.count(_.mate).unsafePerformIO
})
def receive = {
case key: Key sender ! (cache get key)
}
}))
}
object Cached {