scala - scalacache memoization asynchronous refresh -


i'd ttl based memoization active refresh asynchronously in scala.

scalacache example in documentation allows ttl based memoization follows:

import scalacache._ import memoization._  implicit val scalacache = scalacache(new mycache())  def getuser(id: int): user = memoize(60 seconds) {   // db lookup here...   user(id, s"user${id}") } 

curious whether db lookup gets triggered after ttl expires existing value, synchronously , lazily during next getuser invocation, or if refresh happens aggressively , asynchronously - before next getuser call.

if scalacache implementation synchronous, there alternate library provides ability refresh cache actively , asynchronously ?

expiration , refresh closely related different mechanisms. expired entry considered stale , cannot used, must discarded , refetched. entry eligible being refreshed means content still valid use, data should refetched may out of date. guava provides these ttl policies under names expireafterwrite , refreshafterwrite, may used if refresh time smaller expiration time.

the design of caches prefer discarding unused content. active refresh require dedicated thread reloads entries regardless of whether have been used. therefore caching libraries not provide active refresh themselves, make easy applications add customization on top.

when read in guava detects entry eligible refresh, caller perform operation. subsequent reads while refresh in progress obtain current value. means refresh performed synchronously on user's thread triggered it, , asynchronously other threads reading value. refresh may asynchronous if cacheloader.reload overridden perform work on executor.

caffeine rewrite of guava's cache , differs performing refresh asynchronously user's thread. cache delegates operation executor, default forkjoinpool.commonpool jvm-wide executor. policy api provides means of inspecting runtime state of cache, such age of entry, adding application-specific custom behavior.

for other scalacache backends support mixed. ehcache has refreshaheadcache decorator refreshes lazily using own threadpool. redis , memcached not refresh not aware of system of record. lrumap has expiration support grafted on , not have refresh capabilities.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -