blemale / scaffeine

Thin Scala wrapper for Caffeine (https://github.com/ben-manes/caffeine)
https://github.com/blemale/scaffeine
Apache License 2.0
265 stars 24 forks source link

Cache refreshAfterWrite is not working ? #378

Closed khouloudsa closed 8 months ago

khouloudsa commented 8 months ago

Hi,

I am trying to understand why my reload function is not called after the refreshAfterWrite duration so, I implemented this simple test: ` import cats.implicits.catsSyntaxOptionId import com.github.blemale.scaffeine.{AsyncLoadingCache, Scaffeine} import com.typesafe.scalalogging.LazyLogging import org.mockito.ArgumentMatchersSugar import org.scalatest.concurrent.ScalaFutures.whenReady import org.scalatest.matchers.must.Matchers import org.scalatest.wordspec.AnyWordSpecLike

import java.util.concurrent.TimeUnit import scala.concurrent.Future import scala.concurrent.duration.FiniteDuration

class ScalaCaffeineSpec extends AnyWordSpecLike with Matchers with ArgumentMatchersSugar with LazyLogging {

val refreshAfter: FiniteDuration = FiniteDuration(100, TimeUnit.MILLISECONDS)

val asyncCache: AsyncLoadingCache[Int, Int] = Scaffeine() .maximumSize(1) .refreshAfterWrite(refreshAfter) .expireAfterAccess(FiniteDuration(1, TimeUnit.HOURS)) .recordStats() .buildAsyncFuture( loader = (: Int) => Future.successful(1), reloadLoader = ((: Int, _: Int) => reload).some )

private def reload: Future[Int] = Future.successful(2)

"Scala AsyncLoadingCache cache" must { "load" in { whenReady(asyncCache.get(1))(_ mustBe 1) }

"reload at least once" in {
  Thread.sleep(refreshAfter.toMillis + 100)
  whenReady(asyncCache.get(1))(_ mustBe 2)
}

}

} `

the reload fails, any idea ?