Skip to content

Instantly share code, notes, and snippets.

@codeomnitrix
Last active April 11, 2019 04:53
Show Gist options
  • Save codeomnitrix/a1b01f96daf5bc8c3ec086904818568f to your computer and use it in GitHub Desktop.
Save codeomnitrix/a1b01f96daf5bc8c3ec086904818568f to your computer and use it in GitHub Desktop.
import com.github.benmanes.caffeine.cache.Ticker
import com.github.blemale.scaffeine.Scaffeine
import scala.concurrent.duration.Duration
import scala.concurrent.{ExecutionContext, Future}
/**
*
* @param initialCapacity minimum total size for the internal hash tables
* @param maximumSize the maximum size of the cache
* @param expireAfterWrite Specifies that each entry should be automatically removed from the cache once a fixed duration has elapsed after the entry's creation, or the most recent replacement of its value.
* @param expireAfterAccess Specifies that each entry should be automatically removed from the cache once a fixed duration has elapsed after the entry's creation, the most recent replacement of its value, or its last read.
* @param refreshAfterWrite Specifies that active entries are eligible for automatic refresh once a fixed duration has elapsed after the entry's creation, or the most recent replacement of its value.
* @param statsCounter Enables the accumulation of [[com.github.benmanes.caffeine.cache.stats.CacheStats]] during the operation of the cache.
*/
case class NimbusCacheConfig(initialCapacity: Int,
maximumSize: Option[Int],
expireAfterWrite: Option[Duration],
expireAfterAccess: Option[Duration],
refreshAfterWrite: Option[Duration],
statsCounter: Option[NimbusStatsCounter])
case class AsyncCacheOperations[K, E](get: K=> Future[E], invalidateAll: () => Unit)
case class SyncCacheOperations[K, E](get: K => E, invalidateAll: () => Unit)
object NimbusCache {
/**
*
* @param f Function that produces Entity Future[E] given Key K
* @param config Cache config
* @return Memoize Function that remembers last value it produced for a given input
*/
def memoizeAsync[K, E](f: K => Future[E])(config: NimbusCacheConfig, ticker: Ticker = Ticker.systemTicker())(
implicit ec: ExecutionContext): AsyncCacheOperations[K, E] = {
val cacheBuilder = buildCache(config, ticker)
val cache = cacheBuilder.buildAsyncFuture[K, E](f)
config.statsCounter.foreach(_.registerEstimatedSize(() => cache.underlying.synchronous().estimatedSize()))
AsyncCacheOperations(cache.get, cache.synchronous().invalidateAll)
}
private def buildCache(config: NimbusCacheConfig, ticker: Ticker): Scaffeine[Any, Any] = {
val builder = Scaffeine().initialCapacity(config.initialCapacity)
config.maximumSize.foreach(builder.maximumSize(_))
config.expireAfterWrite.foreach(builder.expireAfterWrite)
config.expireAfterAccess.foreach(builder.expireAfterAccess)
config.refreshAfterWrite.foreach(builder.refreshAfterWrite)
config.statsCounter.foreach(sc => builder.recordStats(() => sc))
builder.ticker(ticker)
builder
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment