Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. import com.github.benmanes.caffeine.cache.Ticker
  2. import com.github.blemale.scaffeine.Scaffeine
  3.  
  4. import scala.concurrent.duration.Duration
  5. import scala.concurrent.{ExecutionContext, Future}
  6.  
  7. /**
  8. *
  9. * @param initialCapacity minimum total size for the internal hash tables
  10. * @param maximumSize the maximum size of the cache
  11. * @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.
  12. * @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.
  13. * @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.
  14. * @param statsCounter Enables the accumulation of [[com.github.benmanes.caffeine.cache.stats.CacheStats]] during the operation of the cache.
  15. */
  16. case class NimbusCacheConfig(initialCapacity: Int,
  17. maximumSize: Option[Int],
  18. expireAfterWrite: Option[Duration],
  19. expireAfterAccess: Option[Duration],
  20. refreshAfterWrite: Option[Duration],
  21. statsCounter: Option[NimbusStatsCounter])
  22.  
  23. case class AsyncCacheOperations[K, E](get: K=> Future[E], invalidateAll: () => Unit)
  24.  
  25. case class SyncCacheOperations[K, E](get: K => E, invalidateAll: () => Unit)
  26.  
  27. object NimbusCache {
  28.  
  29. /**
  30. *
  31. * @param f Function that produces Entity Future[E] given Key K
  32. * @param config Cache config
  33. * @return Memoize Function that remembers last value it produced for a given input
  34. */
  35. def memoizeAsync[K, E](f: K => Future[E])(config: NimbusCacheConfig, ticker: Ticker = Ticker.systemTicker())(
  36. implicit ec: ExecutionContext): AsyncCacheOperations[K, E] = {
  37. val cacheBuilder = buildCache(config, ticker)
  38. val cache = cacheBuilder.buildAsyncFuture[K, E](f)
  39. config.statsCounter.foreach(_.registerEstimatedSize(() => cache.underlying.synchronous().estimatedSize()))
  40. AsyncCacheOperations(cache.get, cache.synchronous().invalidateAll)
  41. }
  42.  
  43. /**
  44. *
  45. * @param f Function that produces Entity E given Key K
  46. * @param config Cache config
  47. * @return Memoize Function that remembers last value it produced for a given input
  48. */
  49. def memoize[K, E](f: K => E)(config: NimbusCacheConfig, ticker: Ticker = Ticker.systemTicker()): SyncCacheOperations[K, E] = {
  50. val cacheBuilder = buildCache(config, ticker)
  51. val cache = cacheBuilder.build[K, E](f)
  52. config.statsCounter.foreach(sc => sc.registerEstimatedSize(() => cache.underlying.estimatedSize()))
  53. SyncCacheOperations(cache.get, cache.invalidateAll)
  54. }
  55.  
  56. private def buildCache(config: NimbusCacheConfig, ticker: Ticker): Scaffeine[Any, Any] = {
  57. val builder = Scaffeine().initialCapacity(config.initialCapacity)
  58. config.maximumSize.foreach(builder.maximumSize(_))
  59. config.expireAfterWrite.foreach(builder.expireAfterWrite)
  60. config.expireAfterAccess.foreach(builder.expireAfterAccess)
  61. config.refreshAfterWrite.foreach(builder.refreshAfterWrite)
  62. config.statsCounter.foreach(sc => builder.recordStats(() => sc))
  63. builder.ticker(ticker)
  64. builder
  65. }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement