-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get returns nothing when TTL set #522
Comments
The updated code confirms my suspicions. The value is only available after it expires. import cats.effect.{ExitCode, IO, IOApp}
import scalacache.caffeine.CaffeineCache
import scalacache.{Cache, get, put}
object Main extends IOApp {
val cache: IO[CaffeineCache[IO, String]] = CaffeineCache[IO, String]
private def testCache(implicit c: Cache[IO, String]): IO[Unit] = for {
_ <- put("foo")("bar", Some(scala.concurrent.duration.FiniteDuration(7, "seconds")))
result <- get("foo")
_ <- IO(println(result))
_ <- IO(Thread.sleep(10000))
result2 <- get("foo")
_ <- IO(println(result2))
} yield ()
override def run(args: List[String]): IO[ExitCode] = cache.flatMap(implicit c => testCache.map(_ => ExitCode.Success))
} |
Hi, I think I'm running into similar issue. Here's a snippet to reproduce import com.github.benmanes.caffeine.cache.Caffeine
import scala.concurrent.duration._
val underlyingCache = Caffeine.newBuilder().build[String, scalacache.Entry[String]]()
val cache: CaffeineCache[IO, String] = CaffeineCache[IO, String](underlyingCache)
val cacheWithTtl = cache.cachingF("keyTtl")(Some(1 hour))(IO(println("fetching value")).as("v1"))
scala> cacheWithTtl.unsafeRunSync()
fetching value
val res9: String = v1
scala> cacheWithTtl.unsafeRunSync()
fetching value
val res10: String = v1
scala> underlyingCache.asMap()
val res11: java.util.concurrent.ConcurrentMap[String,scalacache.Entry[String]] = {k1=Entry(v1,None), keyTtl=Entry(v1,Some(1970-01-08T02:23:22.244Z))} Note "fetching value" gets printed out twice, which means the function is run twice to fetch the value. I think it's because expiration isn't set properly, somehow expiresAt is |
Might be related to the CE3 migration. I'll look at it when I have a sec |
I think my problem would be resolved by PR #562. Regarding date |
Merged #562, should be published soon in https://github.com/cb372/scalacache/actions/runs/1217511186 @PawelJ-PL if you can check that snapshot (might need to add a Sonatype snaphots resolver) and let me know it's working for you, I'll publish another milestone :) |
With version |
Gonna play around with it some more, then |
Caffeine has native support for variable expiration and the |
I started using this approach when I came across this bug (and incidentally discovered that scalacache does not delete expired entries but only filters them out) |
I'll take a PR that moves to the caffeine-native expiration methods, unless I remember to take care of it myself when I have a few moments 😅 |
https://github.com/cb372/scalacache/pull/562/files: clock.realTimeInstant.map(_.isBefore(expiration)) expiration is in monotonic time, |
Caught up on the caffeine API...
@ben-manes I'm not a Caffeine user, do you know how likely it is that a user-provided cache would fall into that case? (forcing us to fall back to our own expiration mechanism) In the meantime, I'll just use |
BTW the |
It could be quite common as users decide what features to enable at construction. That dictates what data structures are allocated and the work to perform. There is no requirement for any policy (size, time, reference), so accepting a raw I suspect that ScalaCache tried to abstract away too many concepts (local, remote, memoization, expiration, vendor agnostic, etc). In this case you were coerced into reinventing the feature for your API, and at best redirect to an optimized wrapper if the expiration support was enabled on the cache. Of potential interest is the opposite approach of simply making Caffeine idiomatic in Scala as done in this library. |
Hi,
It looks, that it's impossible to get value with
CaffeineCache
, if it was inserted with TTL.Sample code:
It prints
None
but it should beSome(bar)
. If I remove TTL, it works fine. My suspicion is that the following condition can be incorrect:scalacache/modules/caffeine/src/main/scala/scalacache/caffeine/CaffeineCache.scala
Line 34 in 1fb5879
The text was updated successfully, but these errors were encountered: