mix sync and async caching for light users

This commit is contained in:
Thibault Duplessis 2014-04-17 22:12:35 +02:00
parent 253e61168e
commit e972b50362
2 changed files with 11 additions and 6 deletions

View file

@ -31,4 +31,11 @@ object AsyncCache {
timeToLive: Duration = Duration.Inf) = new AsyncCache[Boolean, V](
cache = LruCache(timeToLive = timeToLive),
f = _ => f)
def mixed[K, V](
f: K => Fu[V],
timeToLive: Duration = Duration.Inf): com.google.common.cache.LoadingCache[K, V] = {
val asyncCache = apply(f, maxCapacity = 10000, timeToLive = 1 minute)
Builder.cache[K, V](timeToLive, (k: K) => asyncCache(k) await 1.second)
}
}

View file

@ -15,7 +15,7 @@ final class LightUserApi(coll: Coll) {
catch {
case e: java.util.concurrent.ExecutionException =>
play.api.Logger("light user").warn(s"$id ${e.getMessage}")
None
LightUser(id, id, None).some
}
def invalidate = cache invalidate _
@ -28,9 +28,7 @@ final class LightUserApi(coll: Coll) {
title = doc.getAs[String](title))
}
private val cache =
lila.memo.Builder.cache[String, Option[LightUser]](3 hours, fetch)
private def fetch(id: String) =
coll.find(BSONDocument(User.BSONFields.id -> id)).one[LightUser] await 1.second
private val cache = lila.memo.AsyncCache.mixed[String, Option[LightUser]](
(id: String) => coll.find(BSONDocument(User.BSONFields.id -> id)).one[LightUser],
timeToLive = 3 hours)
}