节流或去抖方法调用

Seb*_*ber 4 scala

假设我有一个方法允许更新DB中的某些日期:

def updateLastConsultationDate(userId: String): Unit = ???
Run Code Online (Sandbox Code Playgroud)

如何轻松地对该方法进行节流/去抖动,以使每个用户每小时运行不超过一次.

我想要最简单的解决方案,而不是基于任何事件总线,actor lib或持久层.我想要一个内存解决方案(我知道风险).

我已经看到了基于Akka Throttler的 Scala限制解决方案,但这对我来说真的很难开始使用actor来进行限制方法调用.有没有一个非常简单的方法来做到这一点?

编辑:因为它似乎不够清晰,这里是我想要的直观表示,用JS实现.正如您所看到的,限制可能不仅仅是过滤后续调用,还可以推迟调用(也称为trailing eventsjs/lodash/underscore).我正在寻找的解决方案不能仅基于纯同步代码.

Has*_*tor 5

对于基于ReactiveX的解决方案来说,这听起来很棒.在Scala上,Monix是我最喜欢的.这是Ammonite REPL会议,说明了它:

import $ivy.`io.monix::monix:2.1.0` // I'm using Ammonite's magic imports, it's equivalent to adding "io.monix" %% "monix" % "2.1.0" into your libraryImports in SBT

import scala.concurrent.duration.DurationInt
import monix.reactive.subjects.ConcurrentSubject
import monix.reactive.Consumer
import monix.execution.Scheduler.Implicits.global
import monix.eval.Task

class DbUpdater {
  val publish = ConcurrentSubject.publish[String]
  val throttled = publish.throttleFirst(1 hour)
  val cancelHandle = throttled.consumeWith(
    Consumer.foreach(userId =>
      println(s"update your database with $userId here")))
    .runAsync

  def updateLastConsultationDate(userId: String): Unit = {
    publish.onNext(userId)
  }

  def stop(): Unit = cancelHandle.cancel()
}
Run Code Online (Sandbox Code Playgroud)

是的,并且使用Scala.js,如果这对您来说很重要,那么此代码也可以在浏览器中使用.