-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from ChristopherDavenport/caffeineIntegration
Include Caffeine Integration
- Loading branch information
Showing
7 changed files
with
161 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
modules/caffeine/src/main/scala/io/chrisdavenport/mules/caffeine/CaffeineCache.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.chrisdavenport.mules.caffeine | ||
|
||
|
||
import cats.implicits._ | ||
import io.chrisdavenport.mules.Cache | ||
import com.github.benmanes.caffeine.cache.{Caffeine, Cache => CCache} | ||
import cats.effect._ | ||
import io.chrisdavenport.mules.TimeSpec | ||
import java.util.concurrent.TimeUnit | ||
|
||
private class CaffeineCache[F[_], K, V](cc: CCache[K, V])(implicit sync: Sync[F]) extends Cache[F, K, V]{ | ||
// Members declared in io.chrisdavenport.mules.Delete | ||
def delete(k: K): F[Unit] = sync.delay(cc.invalidate(k)) | ||
|
||
// Members declared in io.chrisdavenport.mules.Insert | ||
def insert(k: K, v: V): F[Unit] = sync.delay(cc.put(k, v)) | ||
|
||
// Members declared in io.chrisdavenport.mules.Lookup | ||
def lookup(k: K): F[Option[V]] = | ||
sync.delay(Option(cc.getIfPresent(k))) | ||
|
||
} | ||
|
||
|
||
object CaffeineCache { | ||
|
||
/** | ||
* insertWithTimeout does not operate as the underlying cache is fully responsible for these values | ||
**/ | ||
def build[F[_]: Sync, K, V]( | ||
defaultTimeout: Option[TimeSpec], | ||
accessTimeout: Option[TimeSpec], | ||
maxSize: Option[Long] | ||
): F[Cache[F, K, V]] = { | ||
Sync[F].delay(Caffeine.newBuilder()) | ||
.map(b => defaultTimeout.fold(b)(ts => b.expireAfterWrite(ts.nanos, TimeUnit.NANOSECONDS))) | ||
.map(b => accessTimeout.fold(b)(ts => b.expireAfterAccess(ts.nanos, TimeUnit.NANOSECONDS))) | ||
.map(b => maxSize.fold(b)(b.maximumSize)) | ||
.map(_.build[K with Object, V with Object]()) | ||
.map(_.asInstanceOf[CCache[K, V]]) // 2.12 hack | ||
.map(fromCache[F, K, V](_)) | ||
} | ||
|
||
/** Build a Cache from a Caffeine Cache **/ | ||
def fromCache[F[_]: Sync, K, V](cache: CCache[K, V]): Cache[F, K, V] = | ||
new CaffeineCache[F, K, V](cache) | ||
|
||
|
||
} |
47 changes: 47 additions & 0 deletions
47
modules/caffeine/src/test/scala/io/chrisdavenport/mules/caffeine/CaffeineCacheSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package io.chrisdavenport.mules.caffeine | ||
|
||
import org.specs2.mutable.Specification | ||
import scala.concurrent.duration._ | ||
import cats.effect._ | ||
// import cats.effect.implicits._ | ||
import cats.effect.IO | ||
import cats.effect.specs2.CatsIO | ||
import io.chrisdavenport.mules.TimeSpec | ||
|
||
class CaffeineCacheSpec extends Specification with CatsIO { | ||
"CaffeineCache" should { | ||
"get a value in a quicker period than the timeout" in { | ||
val setup = for { | ||
cache <- CaffeineCache.build[IO, String, Int](Some(TimeSpec.unsafeFromDuration(1.second)), None, None) | ||
_ <- cache.insert("Foo", 1) | ||
_ <- Timer[IO].sleep(1.milli) | ||
value <- cache.lookup("Foo") | ||
} yield value | ||
setup.map(_ must_=== Some(1)) | ||
} | ||
|
||
|
||
"remove a value after delete" in { | ||
val setup = for { | ||
cache <- CaffeineCache.build[IO, String, Int](None, None, None) | ||
_ <- cache.insert("Foo", 1) | ||
_ <- cache.delete("Foo") | ||
value <- cache.lookup("Foo") | ||
} yield value | ||
setup.map(_ must_=== None) | ||
} | ||
|
||
|
||
"Lookup after interval fails to get a value" in { | ||
val setup = for { | ||
cache <- CaffeineCache.build[IO, String, Int](Some(TimeSpec.unsafeFromDuration(1.second)), None, None) | ||
_ <- cache.insert("Foo", 1) | ||
_ <- Timer[IO].sleep(2.second) | ||
value <- cache.lookup("Foo") | ||
} yield value | ||
setup.map(_ must_=== None) | ||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters