rewrite all remaining sync caches with Scaffeine, remove guava

dependency
This commit is contained in:
Thibault Duplessis 2017-01-26 21:09:16 +01:00
parent 4f3621ac1b
commit c70d6c71cc
8 changed files with 25 additions and 67 deletions

View file

@ -1,49 +0,0 @@
package lila.memo
import java.util.concurrent.TimeUnit
import scala.concurrent.duration.Duration
import com.google.common.base.Function
import com.google.common.cache._
object Builder {
private implicit def durationToMillis(d: Duration): Long = d.toMillis
/**
* A caching wrapper for a function (K => V),
* backed by a Cache from Google Collections.
*/
def cache[K, V](ttl: Duration, f: K => V): LoadingCache[K, V] =
cacheBuilder[K, V](ttl).build[K, V](f)
def expiry[K, V](ttl: Duration): Cache[K, V] =
cacheBuilder[K, V](ttl).build[K, V]
def size[K, V](max: Int): Cache[K, V] =
CacheBuilder.newBuilder()
.maximumSize(max)
.asInstanceOf[CacheBuilder[K, V]]
.build[K, V]
private def cacheBuilder[K, V](ttl: Duration): CacheBuilder[K, V] =
CacheBuilder.newBuilder()
.expireAfterWrite(ttl, TimeUnit.MILLISECONDS)
.asInstanceOf[CacheBuilder[K, V]]
implicit def functionToRemovalListener[K, V](f: (K, V) => Unit): RemovalListener[K, V] =
new RemovalListener[K, V] {
def onRemoval(notification: RemovalNotification[K, V]) =
f(notification.getKey, notification.getValue)
}
implicit def functionToGoogleFunction[T, R](f: T => R): Function[T, R] =
new Function[T, R] {
def apply(p1: T) = f(p1)
}
implicit def functionToGoogleCacheLoader[T, R](f: T => R): CacheLoader[T, R] =
new CacheLoader[T, R] {
def load(p1: T) = f(p1)
}
}

View file

@ -1,13 +1,16 @@
package lila.security
import com.github.blemale.scaffeine.{ LoadingCache, Scaffeine }
import com.sanoma.cda.geoip.{ MaxMindIpGeo, IpLocation }
import scala.concurrent.duration._
final class GeoIP(file: String, cacheTtl: Duration) {
final class GeoIP(file: String, cacheTtl: FiniteDuration) {
private val geoIp = MaxMindIpGeo(file, 0)
private val cache = lila.memo.Builder.cache(cacheTtl, compute)
private val cache: LoadingCache[String, Option[Location]] = Scaffeine()
.expireAfterAccess(cacheTtl)
.build(compute)
private def compute(ip: String): Option[Location] =
geoIp getLocation ip map Location.apply

View file

@ -1,15 +1,19 @@
package lila.socket
import scala.concurrent.duration.Duration
import com.github.blemale.scaffeine.{ Cache, Scaffeine }
import scala.concurrent.duration.FiniteDuration
import play.api.libs.json._
final class History[Metadata](ttl: Duration) {
final class History[Metadata](ttl: FiniteDuration) {
type Message = History.Message[Metadata]
private var privateVersion = 0
private val messages = lila.memo.Builder.expiry[Int, Message](ttl)
private val cache: Cache[Int, Message] = Scaffeine()
.expireAfterWrite(ttl)
.build[Int, Message]
def version = privateVersion
@ -23,12 +27,12 @@ final class History[Metadata](ttl: Duration) {
(msgs.size == version - v) option msgs
}
private def message(v: Int) = Option(messages getIfPresent v)
private def message(v: Int) = cache getIfPresent v
def +=(payload: JsObject, metadata: Metadata): Message = {
privateVersion = privateVersion + 1
val vmsg = History.Message(payload, privateVersion, metadata)
messages.put(privateVersion, vmsg)
cache.put(privateVersion, vmsg)
vmsg
}
}

View file

@ -2,6 +2,7 @@ package lila.study
import akka.actor._
import akka.pattern.ask
import com.github.blemale.scaffeine.{ LoadingCache, Scaffeine }
import com.typesafe.config.Config
import scala.concurrent.duration._
@ -9,6 +10,7 @@ import lila.common.PimpedConfig._
import lila.hub.actorApi.map.Ask
import lila.hub.{ ActorMap, Sequencer }
import lila.socket.actorApi.GetVersion
import lila.socket.AnaDests
import makeTimeout.short
final class Env(
@ -109,10 +111,9 @@ final class Env(
logger = logger)
}))
private lazy val destCache = {
import lila.socket.AnaDests
lila.memo.Builder.cache[AnaDests.Ref, AnaDests](1 minute, _.compute)
}
private lazy val destCache: LoadingCache[AnaDests.Ref, AnaDests] = Scaffeine()
.expireAfterAccess(1 minute)
.build(_.compute)
def cli = new lila.common.Cli {
def process = {

View file

@ -1,11 +1,11 @@
package lila.study
import akka.actor._
import chess.format.pgn.Glyphs
import com.google.common.cache.LoadingCache
import com.github.blemale.scaffeine.LoadingCache
import play.api.libs.json._
import scala.concurrent.duration._
import chess.format.pgn.Glyphs
import lila.hub.TimeBomb
import lila.socket.actorApi.{ Connected => _, _ }
import lila.socket.Socket.Uid

View file

@ -4,10 +4,10 @@ import scala.concurrent.duration._
import akka.actor._
import akka.pattern.ask
import chess.format.pgn.Glyph
import com.google.common.cache.LoadingCache
import com.github.blemale.scaffeine.LoadingCache
import play.api.libs.json._
import chess.format.pgn.Glyph
import lila.common.PimpedJson._
import lila.hub.actorApi.map._
import lila.socket.actorApi.{ Connected => _, _ }

View file

@ -36,7 +36,7 @@ object ApplicationBuild extends Build {
jgit, findbugs, reactivemongo.driver, reactivemongo.iteratees, akka.actor, akka.slf4j,
spray.caching, maxmind, prismic,
kamon.core, kamon.statsd, kamon.influxdb,
java8compat, semver, scrimage, configs, guava, scaffeine),
java8compat, semver, scrimage, configs, scaffeine),
TwirlKeys.templateImports ++= Seq(
"lila.game.{ Game, Player, Pov }",
"lila.tournament.Tournament",

View file

@ -29,7 +29,6 @@ object Dependencies {
val scalalib = "com.github.ornicar" %% "scalalib" % "5.7"
val config = "com.typesafe" % "config" % "1.3.1"
val apache = "org.apache.commons" % "commons-lang3" % "3.5"
val guava = "com.google.guava" % "guava" % "21.0"
val findbugs = "com.google.code.findbugs" % "jsr305" % "3.0.1"
val hasher = "com.roundeights" %% "hasher" % "1.2.0"
val jgit = "org.eclipse.jgit" % "org.eclipse.jgit" % "3.2.0.201312181205-r"