You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using CaffeineCache for local in-memory cache, and I want to achieve this:
TTL is 10 seconds
To get a new value, it is a long process, say cost 5 seconds
Provide a REST service, to retrieve the value, if it is older than 10 seconds, I just want to trigger the refreshing of the new value, but I don't want to wait for the new value, in the mean time, I want to just return the old value instead
Once the new value is retrieved, I should update the old value
So roughly it is like code here:
importscalacache._importscalacache.caffeine._importscalacache.memoization._implicitvalec:ExecutionContext= scala.concurrent.ExecutionContext.global
vall1Cache:Cache[Future[Cat]] =CaffeineCache[Future[Cat]]
vall2Cache:Cache[Cat] =CaffeineCache[Cat]
valflags:Flags=Flags.defaultFlags
defgetCat(id: Int) = {
// to trigger refreshing if necessaryvall1CacheResult:Future[Cat] = memoizeSync[Future[Cat]](Some(10 seconds)){
Future {
Thread.sleep(5000)
valnewCat=Cat(id, s"cat $id", s"${Timestamp.now()}")
// update l2Cache
put(id)(newCat)(l2Cache, modes.scalaFuture.mode, flags)
newCat
}
}(l1Cache, modes.sync.mode, flags)
// get the result from l2Cachevall2CacheResult:Try[Option[Cat]] = get(id)(l2Cache, modes.try_.mode, flags)
l2CacheResult.toString
}
while (true) {
println(s"${Timestamp.now()}${getCat(1)}")
Thread.sleep(1000)
}
}
The point is in the l1Cache future, when the new value is retrieved, I want update the l2Cache, that would be in a background thread, but I read them from another thread, would that be thread-safe?
The text was updated successfully, but these errors were encountered:
I am using
CaffeineCache
for local in-memory cache, and I want to achieve this:So roughly it is like code here:
The point is in the l1Cache future, when the new value is retrieved, I want update the l2Cache, that would be in a background thread, but I read them from another thread, would that be thread-safe?
The text was updated successfully, but these errors were encountered: