项目作者: blemale

项目描述 :
Thin Scala wrapper for Caffeine (https://github.com/ben-manes/caffeine)
高级语言: Scala
项目地址: git://github.com/blemale/scaffeine.git
创建时间: 2016-03-05T13:22:36Z
项目社区:https://github.com/blemale/scaffeine

开源协议:Apache License 2.0

下载


CI
Coverage Status
scaffeine Scala version support
License
Known Vulnerabilities

Scaffeine

A thin Scala wrapper for Caffeine (https://github.com/ben-manes/caffeine).

Browse the API docs for the latest release.

Motivations

Caffeine is an awesome Java caching library.
It has an impressive performance and a neat Java 8 API.

However the API does not play very well with Scala.
So this is the thinner wrapper we can came with to make Caffeine easy and idiomatic to use in Scala.

API

Cache

  1. "Cache" should "be created from Scaffeine builder" in {
  2. import com.github.blemale.scaffeine.{ Cache, Scaffeine }
  3. import scala.concurrent.duration._
  4. val cache: Cache[Int, String] =
  5. Scaffeine()
  6. .recordStats()
  7. .expireAfterWrite(1.hour)
  8. .maximumSize(500)
  9. .build[Int, String]()
  10. cache.put(1, "foo")
  11. cache.getIfPresent(1) should be(Some("foo"))
  12. cache.getIfPresent(2) should be(None)
  13. }

LoadingCache

  1. "LoadingCache" should "be created from Scaffeine builder" in {
  2. import com.github.blemale.scaffeine.{ LoadingCache, Scaffeine }
  3. import scala.concurrent.duration._
  4. val cache: LoadingCache[Int, String] =
  5. Scaffeine()
  6. .recordStats()
  7. .expireAfterWrite(1.hour)
  8. .maximumSize(500)
  9. .build((i: Int) => s"foo$i")
  10. cache.get(1) should be("foo1")
  11. }

AsyncLoadingCache

  1. "AsyncLoadingCache" should "be created from Scaffeine builder with synchronous loader" in {
  2. import com.github.blemale.scaffeine.{ AsyncLoadingCache, Scaffeine }
  3. import scala.concurrent.duration._
  4. val cache: AsyncLoadingCache[Int, String] =
  5. Scaffeine()
  6. .recordStats()
  7. .expireAfterWrite(1.hour)
  8. .maximumSize(500)
  9. .buildAsync((i: Int) => s"foo$i")
  10. whenReady(cache.get(1)) { value =>
  11. value should be("foo1")
  12. }
  13. }
  14. "AsyncLoadingCache" should "be created from Scaffeine builder with asynchronous loader" in {
  15. import com.github.blemale.scaffeine.{ AsyncLoadingCache, Scaffeine }
  16. import scala.concurrent.duration._
  17. val cache: AsyncLoadingCache[Int, String] =
  18. Scaffeine()
  19. .recordStats()
  20. .expireAfterWrite(1.hour)
  21. .maximumSize(500)
  22. .buildAsyncFuture((i: Int) => Future.successful(s"foo$i"))
  23. whenReady(cache.get(1)) { value =>
  24. value should be("foo1")
  25. }
  26. }

Download

Download from Maven Central or depend via SBT:

  1. "com.github.blemale" %% "scaffeine" % "<version>" % "compile"

Snapshots of the development version are available in
Sonatype’s snapshots repository.