implement RateLimit with Scaffeine
This commit is contained in:
parent
5807044640
commit
2400f08f68
|
@ -1,5 +1,7 @@
|
|||
package lila.memo
|
||||
|
||||
import com.github.blemale.scaffeine.{ Cache, Scaffeine }
|
||||
|
||||
import ornicar.scalalib.Zero
|
||||
import scala.concurrent.duration.Duration
|
||||
|
||||
|
@ -15,7 +17,9 @@ final class RateLimit(
|
|||
private type Cost = Int
|
||||
private type ClearAt = Long
|
||||
|
||||
private val storage = Builder.expiry[String, (Cost, ClearAt)](ttl = duration)
|
||||
private val storage = Scaffeine()
|
||||
.expireAfterWrite(duration)
|
||||
.build[String, (Cost, ClearAt)]()
|
||||
|
||||
private def makeClearAt = nowMillis + duration.toMillis
|
||||
|
||||
|
@ -25,7 +29,7 @@ final class RateLimit(
|
|||
logger.info(s"[start] $name ($credits/$duration)")
|
||||
|
||||
def apply[A](k: String, cost: Cost = 1, msg: => String = "")(op: => A)(implicit default: Zero[A]): A =
|
||||
Option(storage getIfPresent k) match {
|
||||
storage getIfPresent k match {
|
||||
case None =>
|
||||
storage.put(k, cost -> makeClearAt)
|
||||
op
|
||||
|
|
|
@ -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, scalaffeine),
|
||||
java8compat, semver, scrimage, configs, guava, scaffeine),
|
||||
TwirlKeys.templateImports ++= Seq(
|
||||
"lila.game.{ Game, Player, Pov }",
|
||||
"lila.tournament.Tournament",
|
||||
|
@ -133,7 +133,7 @@ object ApplicationBuild extends Build {
|
|||
)
|
||||
|
||||
lazy val memo = project("memo", Seq(common, db)).settings(
|
||||
libraryDependencies ++= Seq(findbugs, spray.caching, configs) ++ provided(play.api, reactivemongo.driver)
|
||||
libraryDependencies ++= Seq(findbugs, spray.caching, scaffeine, configs) ++ provided(play.api, reactivemongo.driver)
|
||||
)
|
||||
|
||||
lazy val search = project("search", Seq(common, hub)).settings(
|
||||
|
|
|
@ -41,7 +41,7 @@ object Dependencies {
|
|||
val semver = "com.gilt" %% "gfc-semver" % "0.0.5"
|
||||
val scrimage = "com.sksamuel.scrimage" %% "scrimage-core" % "2.1.8"
|
||||
val configs = "com.github.kxbmap" %% "configs" % "0.4.4"
|
||||
val scalaffeine = "com.github.blemale" %% "scaffeine" % "2.0.0" % "compile"
|
||||
val scaffeine = "com.github.blemale" %% "scaffeine" % "2.0.0" % "compile"
|
||||
|
||||
object reactivemongo {
|
||||
val version = "0.12.0"
|
||||
|
|
Loading…
Reference in a new issue