Replace buggy redis cache plugin with custom mongo cache

This commit is contained in:
Thibault Duplessis 2012-05-25 18:24:42 +02:00
parent 75aed100b3
commit b288df8797
8 changed files with 65 additions and 16 deletions

View file

@ -11,7 +11,6 @@ import play.api._
import play.api.mvc._
import play.api.data._
import play.api.http._
import play.api.cache.Cache
import scala.io.Codec
import com.codahale.jerkson.Json
@ -26,6 +25,7 @@ trait LilaController
with Auth {
lazy val env = Global.env
lazy val cache = env.mongoCache
implicit val current = env.app
@ -178,11 +178,11 @@ trait LilaController
private def restoreUser[A](req: RequestHeader): Option[UserModel] = for {
sessionId req.session.get("sessionId")
userId Cache.getAs[Id](sessionId + ":sessionId")(current, idManifest)
userId cache.getAs[Id](sessionId + ":sessionId")(idManifest)
user resolveUser(userId)
} yield {
Cache.set(sessionId + ":sessionId", userId, sessionTimeoutInSeconds)
Cache.set(userId.toString + ":userId", sessionId, sessionTimeoutInSeconds)
cache.set(sessionId + ":sessionId", userId, sessionTimeoutInSeconds)
cache.set(userId.toString + ":userId", sessionId, sessionTimeoutInSeconds)
user
}
}

View file

@ -90,6 +90,8 @@ final class CoreEnv private (application: Application, val settings: Settings) {
messageRepo = lobby.messageRepo,
entryRepo = timeline.entryRepo)
lazy val mongoCache = new MongoCache(mongodb(MongoCollectionCache))
lazy val mongodb = MongoConnection(
new MongoServer(MongoHost, MongoPort),
mongoOptions

30
app/core/MongoCache.scala Normal file
View file

@ -0,0 +1,30 @@
package lila
package core
import com.mongodb.casbah.MongoCollection
import com.mongodb.casbah.Imports._
final class MongoCache(collection: MongoCollection) {
private val field = "v"
def set(key: String, value: Any) {
collection += DBObject("_id" -> key, field -> value)
}
def get(key: String): Option[Any] = for {
o collection.findOne(select(key))
v Option(o get field)
} yield v
def getAs[T](key: String)(implicit m: ClassManifest[T]): Option[T] = for {
v get(key)
typed (m.erasure.isAssignableFrom(v.getClass)) option v.asInstanceOf[T]
} yield typed
def remove(key: String) {
collection remove select(key)
}
private def select(key: String) = DBObject("_id" -> key)
}

View file

@ -0,0 +1,23 @@
package lila
package core
import play.api._
import play.api.cache._
class MongoCachePlugin(app: Application) extends CachePlugin {
lazy val mongoCache = Global.env.mongoCache
lazy val api = new CacheAPI {
def set(key: String, value: Any, expiration: Int) {
mongoCache.set(key, value)
}
def get(key: String): Option[Any] = mongoCache get key
def remove(key: String) {
mongoCache remove key
}
}
}

View file

@ -58,6 +58,7 @@ final class Settings(config: Config) {
val MongoCollectionHistory = getString("mongo.collection.history")
val MongoCollectionRoom = getString("mongo.collection.room")
val MongoCollectionConfig = getString("mongo.collection.config")
val MongoCollectionCache = getString("mongo.collection.cache")
val ActorReporting = "reporting"
val ActorSiteHub = "site_hub"

View file

@ -11,12 +11,14 @@ mongo {
history = user_history
room = room
config = config
cache = cache
}
connectionsPerHost = 100
autoConnectRetry = true
connectTimeout = 15 seconds
threadsAllowedToBlockForConnectionMultiplier = 500
}
monitor {
timeout = 1 second
}
@ -72,16 +74,9 @@ application {
global="lila.core.Global"
}
# Define the Router object to use for this application.
# application.router=my.application.Routes
evolutionplugin = disabled
# Use redis cache instead
ehcacheplugin = disabled
# Use mongodb instead
dbplugin = disabled
evolutionplugin = disabled
ehcacheplugin = disabled
logger {
root=INFO

View file

@ -1 +1 @@
550:com.typesafe.plugin.RedisPlugin
500:lila.core.MongoCachePlugin

View file

@ -26,7 +26,6 @@ trait Dependencies {
val slf4jNop = "org.slf4j" % "slf4j-nop" % "1.6.4"
val dispatch = "net.databinder" %% "dispatch-http" % "0.8.7"
val auth = "jp.t2v" %% "play20.auth" % "0.3-SNAPSHOT"
val plugins = "com.typesafe" %% "play-plugins-redis" % "2.0.1-hack2"
val paginator = "com.github.ornicar" %% "paginator-core" % "1.5"
val paginatorSalat = "com.github.ornicar" %% "paginator-salat-adapter" % "1.4"
}
@ -58,7 +57,6 @@ object ApplicationBuild extends Build with Resolvers with Dependencies {
scalaTime,
dispatch,
auth,
plugins,
paginator,
paginatorSalat),
templatesImport ++= Seq(