Skip to content

Commit

Permalink
replace spray caching with AsyncCache2 in many places
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Jan 26, 2017
1 parent c39f5d2 commit 1986cb3
Show file tree
Hide file tree
Showing 29 changed files with 154 additions and 110 deletions.
2 changes: 1 addition & 1 deletion app/controllers/Account.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ object Account extends LilaController {
relationEnv.api.countFollowing(me.id) zip
Env.pref.api.getPref(me) zip
lila.game.GameRepo.urgentGames(me) zip
Env.challenge.api.countInFor(me.id) map {
Env.challenge.api.countInFor.get(me.id) map {
case ((((nbFollowers, nbFollowing), prefs), povs), nbChallenges) =>
Env.current.bus.publish(lila.user.User.Active(me), 'userActive)
Ok {
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/LilaController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ private[controllers] trait LilaController
Env.user.lightUserApi preloadUser me
getOnlineFriends(me) zip
Env.team.api.nbRequests(me.id) zip
Env.challenge.api.countInFor(me.id) zip
Env.challenge.api.countInFor.get(me.id) zip
Env.notifyModule.api.unreadCount(Notifies(me.id)).map(_.value)
}
else fuccess {
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/Lobby.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ object Lobby extends LilaController {

private def renderCtx(implicit ctx: Context): Fu[Html] = Env.current.preloader(
posts = Env.forum.recent(ctx.me, Env.team.cached.teamIds),
tours = Env.tournament.cached promotable true,
events = Env.event.api promotable true,
simuls = Env.simul allCreatedFeaturable true
tours = Env.tournament.cached.promotable.get,
events = Env.event.api.promotable.get,
simuls = Env.simul.allCreatedFeaturable.get
) map (html.lobby.home.apply _).tupled

private def renderRequestKey(r: RequestKey): Fu[Html] = renderCtx {
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/Simul.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object Simul extends LilaController {
}

private def fetchSimuls =
env.allCreated(true) zip env.repo.allStarted zip env.repo.allFinished(30)
env.allCreated.get zip env.repo.allStarted zip env.repo.allFinished(30)

def show(id: String) = Open { implicit ctx =>
env.repo find id flatMap {
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/User.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ object User extends LilaController {
val max = 50
negotiate(
html = notFound,
api = _ => env.cached top50Online true map { list =>
api = _ => env.cached.top50Online.get map { list =>
Ok(Json.toJson(list.take(getInt("nb").fold(10)(_ min max)).map(env.jsonView(_))))
})
}
Expand Down Expand Up @@ -162,7 +162,7 @@ object User extends LilaController {
// }
// }
tourneyWinners Env.tournament.winners.all.map(_.top)
online env.cached top50Online true
online env.cached.top50Online.get
_ <- Env.user.lightUserApi preloadMany tourneyWinners.map(_.userId)
res <- negotiate(
html = fuccess(Ok(html.user.list(
Expand Down
12 changes: 7 additions & 5 deletions modules/blog/src/main/BlogApi.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package lila.blog

import io.prismic._
import lila.memo.AsyncCache
import scala.concurrent.duration._

final class BlogApi(prismicUrl: String, collection: String) {
final class BlogApi(
asyncCache: lila.memo.AsyncCache2.Builder,
prismicUrl: String,
collection: String) {

def recent(api: Api, ref: Option[String], nb: Int): Fu[Option[Response]] =
api.forms(collection).ref(resolveRef(api)(ref) | api.master.ref)
Expand Down Expand Up @@ -38,12 +40,12 @@ final class BlogApi(prismicUrl: String, collection: String) {
case _ => logger info message
}

private val fetchPrismicApi = AsyncCache.single[Api](
private val fetchPrismicApi = asyncCache.single[Api](
name = "blogApi.fetchPrismicApi",
f = Api.get(prismicUrl, cache = cache, logger = prismicLogger),
timeToLive = 10 seconds)
expireAfter = _.ExpireAfterWrite(15 seconds))

def prismicApi = fetchPrismicApi(true)
def prismicApi = fetchPrismicApi.get
}

object BlogApi {
Expand Down
3 changes: 3 additions & 0 deletions modules/blog/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import lila.common.PimpedConfig._
final class Env(
config: Config,
scheduler: lila.common.Scheduler,
asyncCache: lila.memo.AsyncCache2.Builder,
notifyApi: lila.notify.NotifyApi)(implicit system: akka.actor.ActorSystem) {

private val PrismicApiUrl = config getString "prismic.api_url"
Expand All @@ -17,6 +18,7 @@ final class Env(

lazy val api = new BlogApi(
prismicUrl = PrismicApiUrl,
asyncCache = asyncCache,
collection = PrismicCollection)

lazy val lastPostCache = new LastPostCache(api, LastPostCacheTtl, PrismicCollection)
Expand All @@ -38,6 +40,7 @@ object Env {
lazy val current: Env = "blog" boot new Env(
config = lila.common.PlayApp loadConfig "blog",
scheduler = lila.common.PlayApp.scheduler,
asyncCache = lila.memo.Env.current.asyncCache,
notifyApi = lila.notify.Env.current.api)(
lila.common.PlayApp.system)
}
39 changes: 20 additions & 19 deletions modules/challenge/src/main/ChallengeApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package lila.challenge

import akka.actor._
import org.joda.time.DateTime
import scala.concurrent.duration._

import lila.game.{ Game, Pov, GameRepo }
import lila.hub.actorApi.map.Tell
import lila.hub.actorApi.SendTo
import lila.memo.AsyncCache
import lila.user.{ User, UserRepo }

final class ChallengeApi(
Expand All @@ -17,6 +17,7 @@ final class ChallengeApi(
maxPlaying: Int,
socketHub: ActorRef,
userRegister: ActorSelection,
asyncCache: lila.memo.AsyncCache2.Builder,
lilaBus: lila.common.Bus) {

import Challenge._
Expand All @@ -29,38 +30,40 @@ final class ChallengeApi(
case true => fuccess(false)
case false => {
repo like c flatMap { _ ?? repo.cancel }
} >> (repo insert c) >> uncacheAndNotify(c) >>- {
} >> (repo insert c) >>- {
uncacheAndNotify(c)
lilaBus.publish(Event.Create(c), 'challenge)
} inject true
}

def byId = repo byId _

val countInFor = AsyncCache(
val countInFor = asyncCache.clearable(
name = "challenge.countInFor",
f = repo.countCreatedByDestId,
maxCapacity = 20000)
expireAfter = _.ExpireAfterAccess(12 minutes))

def createdByChallengerId = repo createdByChallengerId _

def createdByDestId = repo createdByDestId _

def cancel(c: Challenge) = (repo cancel c) >> uncacheAndNotify(c)
def cancel(c: Challenge) = (repo cancel c) >>- uncacheAndNotify(c)

private def offline(c: Challenge) = (repo offline c) >> uncacheAndNotify(c)
private def offline(c: Challenge) = (repo offline c) >>- uncacheAndNotify(c)

private[challenge] def ping(id: Challenge.ID): Funit = repo statusById id flatMap {
case Some(Status.Created) => repo setSeen id
case Some(Status.Offline) => (repo setSeenAgain id) >> byId(id).flatMap { _ ?? uncacheAndNotify }
case Some(Status.Offline) => (repo setSeenAgain id) >> byId(id).map { _ foreach uncacheAndNotify }
case _ => fuccess(socketReload(id))
}

def decline(c: Challenge) = (repo decline c) >> uncacheAndNotify(c)
def decline(c: Challenge) = (repo decline c) >>- uncacheAndNotify(c)

def accept(c: Challenge, user: Option[User]): Fu[Option[Pov]] =
joiner(c, user).flatMap {
case None => fuccess(None)
case Some(pov) => (repo accept c) >> uncacheAndNotify(c) >>- {
case Some(pov) => (repo accept c) >>- {
uncacheAndNotify(c)
lilaBus.publish(Event.Accept(c, user.map(_.id)), 'challenge)
} inject pov.some
}
Expand Down Expand Up @@ -111,22 +114,20 @@ final class ChallengeApi(
}

private def remove(c: Challenge) =
repo.remove(c.id) >> uncacheAndNotify(c)
repo.remove(c.id) >>- uncacheAndNotify(c)

private def uncacheAndNotify(c: Challenge) = {
(c.destUserId ?? countInFor.remove) >>-
(c.destUserId ?? notify) >>-
(c.challengerUserId ?? notify) >>-
socketReload(c.id)
private def uncacheAndNotify(c: Challenge): Unit = {
c.destUserId ?? countInFor.invalidate
c.destUserId ?? notify
c.challengerUserId ?? notify
socketReload(c.id)
}

private def socketReload(id: Challenge.ID) {
private def socketReload(id: Challenge.ID): Unit =
socketHub ! Tell(id, Socket.Reload)
}

private def notify(userId: User.ID) {
private def notify(userId: User.ID): Unit =
allFor(userId) foreach { all =>
userRegister ! SendTo(userId, lila.socket.Socket.makeMessage("challenges", jsonView(all)))
}
}
}
3 changes: 3 additions & 0 deletions modules/challenge/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ final class Env(
isOnline: lila.user.User.ID => Boolean,
hub: lila.hub.Env,
db: lila.db.Env,
asyncCache: lila.memo.AsyncCache2.Builder,
scheduler: lila.common.Scheduler) {

private val settings = new {
Expand Down Expand Up @@ -58,6 +59,7 @@ final class Env(
maxPlaying = MaxPlaying,
socketHub = socketHub,
userRegister = hub.actor.userRegister,
asyncCache = asyncCache,
lilaBus = system.lilaBus)

private lazy val repo = new ChallengeRepo(
Expand All @@ -82,5 +84,6 @@ object Env {
lightUser = lila.user.Env.current.lightUserSync,
isOnline = lila.user.Env.current.isOnline,
db = lila.db.Env.current,
asyncCache = lila.memo.Env.current.asyncCache,
scheduler = lila.common.PlayApp.scheduler)
}
22 changes: 11 additions & 11 deletions modules/coach/src/main/CoachApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import org.joda.time.DateTime
import scala.concurrent.duration._

import lila.db.dsl._
import lila.memo.AsyncCache
import lila.notify.{ Notification, NotifyApi }
import lila.security.Granter
import lila.user.{ User, UserRepo }
Expand All @@ -13,16 +12,17 @@ final class CoachApi(
coachColl: Coll,
reviewColl: Coll,
photographer: Photographer,
asyncCache: lila.memo.AsyncCache2.Builder,
notifyApi: NotifyApi) {

import BsonHandlers._

private val cache = AsyncCache.single[List[Coach]](
private val cache = asyncCache.single[List[Coach]](
name = "coach.list",
f = coachColl.find($empty).list[Coach](),
timeToLive = 10 minutes)
expireAfter = _.ExpireAfterWrite(30 minutes))

private def all = cache(true)
private def all = cache.get

def byId(id: Coach.Id): Fu[Option[Coach]] = all.map(_.find(_.id == id))

Expand All @@ -36,7 +36,7 @@ final class CoachApi(
def findOrInit(user: User): Fu[Option[Coach.WithUser]] = Granter(_.Coach)(user) ?? {
find(user) orElse {
val c = Coach.WithUser(Coach make user, user)
coachColl.insert(c.coach) >> cache.remove(true) inject c.some
coachColl.insert(c.coach) >>- cache.refresh inject c.some
}
}

Expand All @@ -59,27 +59,27 @@ final class CoachApi(
$id(c.coach.id),
data(c.coach),
upsert = true
).void >> cache.clear
).void >>- cache.refresh

def setNbReviews(id: Coach.Id, nb: Int): Funit =
coachColl.update($id(id), $set("nbReviews" -> nb)).void >> cache.clear
coachColl.update($id(id), $set("nbReviews" -> nb)).void >>- cache.refresh

private[coach] def toggleApproved(username: String, value: Boolean): Fu[String] =
find(username) flatMap {
case None => fuccess("No such coach")
case Some(c) => coachColl.update(
$id(c.coach.id),
$set("approved" -> value)
) >> cache.clear inject "Done!"
) >>- cache.refresh inject "Done!"
}

def uploadPicture(c: Coach.WithUser, picture: Photographer.Uploaded): Funit =
photographer(c.coach.id, picture).flatMap { pic =>
coachColl.update($id(c.coach.id), $set("picturePath" -> pic.path))
} >> cache.clear
coachColl.update($id(c.coach.id), $set("picturePath" -> pic.path)).void
} >>- cache.refresh

def deletePicture(c: Coach.WithUser): Funit =
coachColl.update($id(c.coach.id), $unset("picturePath")) >> cache.clear
coachColl.update($id(c.coach.id), $unset("picturePath")).void >>- cache.refresh

private def withUser(user: User)(coach: Coach): Coach.WithUser =
Coach.WithUser(coach, user)
Expand Down
3 changes: 3 additions & 0 deletions modules/coach/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.typesafe.config.Config
final class Env(
config: Config,
notifyApi: lila.notify.NotifyApi,
asyncCache: lila.memo.AsyncCache2.Builder,
db: lila.db.Env) {

private val CollectionCoach = config getString "collection.coach"
Expand All @@ -21,6 +22,7 @@ final class Env(
coachColl = coachColl,
reviewColl = reviewColl,
photographer = photographer,
asyncCache = asyncCache,
notifyApi = notifyApi)

lazy val pager = new CoachPager(api)
Expand All @@ -38,5 +40,6 @@ object Env {
lazy val current: Env = "coach" boot new Env(
config = lila.common.PlayApp loadConfig "coach",
notifyApi = lila.notify.Env.current.api,
asyncCache = lila.memo.Env.current.asyncCache,
db = lila.db.Env.current)
}
4 changes: 3 additions & 1 deletion modules/event/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ import com.typesafe.config.Config
final class Env(
config: Config,
db: lila.db.Env,
asyncCache: lila.memo.AsyncCache2.Builder,
system: ActorSystem) {

private val CollectionEvent = config getString "collection.event"

private lazy val eventColl = db(CollectionEvent)

lazy val api = new EventApi(coll = eventColl)
lazy val api = new EventApi(coll = eventColl, asyncCache = asyncCache)
}

object Env {

lazy val current = "event" boot new Env(
config = lila.common.PlayApp loadConfig "event",
db = lila.db.Env.current,
asyncCache = lila.memo.Env.current.asyncCache,
system = lila.common.PlayApp.system)
}
13 changes: 8 additions & 5 deletions modules/event/src/main/EventApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import scala.concurrent.duration._
import lila.db.dsl._
import lila.memo._

final class EventApi(coll: Coll) {
final class EventApi(
coll: Coll,
asyncCache: lila.memo.AsyncCache2.Builder) {

import BsonHandlers._

val promotable = AsyncCache.single(
val promotable = asyncCache.single(
name = "event.promotable",
fetchPromotable, timeToLive = 5 minutes)
fetchPromotable,
expireAfter = _.ExpireAfterWrite(5 minutes))

def fetchPromotable: Fu[List[Event]] = coll.find($doc(
"enabled" -> true,
Expand All @@ -37,12 +40,12 @@ final class EventApi(coll: Coll) {
}

def update(old: Event, data: EventForm.Data) =
coll.update($id(old.id), data update old) >> promotable.clear
coll.update($id(old.id), data update old) >>- promotable.refresh

def createForm = EventForm.form

def create(data: EventForm.Data, userId: String): Fu[Event] = {
val event = data make userId
coll.insert(event) >> promotable.clear inject event
coll.insert(event) >>- promotable.refresh inject event
}
}
Loading

0 comments on commit 1986cb3

Please sign in to comment.