Skip to content

Commit

Permalink
fix scala 2.12 compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisjkl committed Nov 7, 2021
1 parent a053a7c commit 91706ea
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ object CaffeineCache {

/** Create a new Caffeine cache.
*/
def apply[F[_]: Sync: Clock, K <: Object, V]: F[CaffeineCache[F, K, V]] =
Sync[F].delay(Caffeine.newBuilder().build[K, Entry[V]]()).map(apply(_))
def apply[F[_]: Sync: Clock, K <: AnyRef, V]: F[CaffeineCache[F, K, V]] =
Sync[F].delay(Caffeine.newBuilder.build[K, Entry[V]]()).map(apply(_))

/** Create a new cache utilizing the given underlying Caffeine cache.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,18 @@ class CaffeineCacheSpec extends AnyFlatSpec with Matchers with BeforeAndAfter wi
unsafeRun(f(ticker)) shouldBe Outcome.succeeded(Some(succeed))
}

private def newCCache = Caffeine.newBuilder.build[Int, Entry[String]]
case class MyInt(int: Int)

private def newCCache = Caffeine.newBuilder.build[MyInt, Entry[String]]

private def newFCache[F[_]: Sync, V](
underlying: com.github.benmanes.caffeine.cache.Cache[Int, Entry[V]]
underlying: com.github.benmanes.caffeine.cache.Cache[MyInt, Entry[V]]
) = {
CaffeineCache[F, Int, V](underlying)
CaffeineCache[F, MyInt, V](underlying)
}

private def newIOCache[V](
underlying: com.github.benmanes.caffeine.cache.Cache[Int, Entry[V]]
underlying: com.github.benmanes.caffeine.cache.Cache[MyInt, Entry[V]]
) = {
newFCache[IO, V](underlying)
}
Expand All @@ -49,14 +51,14 @@ class CaffeineCacheSpec extends AnyFlatSpec with Matchers with BeforeAndAfter wi
it should "return the value stored in the underlying cache if expiration is not specified" in ticked { _ =>
val underlying = newCCache
val entry = Entry("hello", expiresAt = None)
underlying.put(1, entry)
underlying.put(MyInt(1), entry)

newIOCache(underlying).get(1).map(_ shouldBe Some("hello"))
newIOCache(underlying).get(MyInt(1)).map(_ shouldBe Some("hello"))
}

it should "return None if the given key does not exist in the underlying cache" in ticked { _ =>
val underlying = newCCache
newIOCache(underlying).get(2).map(_ shouldBe None)
newIOCache(underlying).get(MyInt(2)).map(_ shouldBe None)
}

it should "return None if the given key exists but the value has expired" in ticked { ticker =>
Expand All @@ -65,8 +67,8 @@ class CaffeineCacheSpec extends AnyFlatSpec with Matchers with BeforeAndAfter wi
val underlying = newCCache
val expiredEntry =
Entry("hello", expiresAt = Some(Instant.ofEpochMilli(now.toMillis).minusSeconds(60)))
underlying.put(1, expiredEntry)
newIOCache(underlying).get(1).map(_ shouldBe None)
underlying.put(MyInt(1), expiredEntry)
newIOCache(underlying).get(MyInt(1)).map(_ shouldBe None)
}
}

Expand All @@ -76,17 +78,17 @@ class CaffeineCacheSpec extends AnyFlatSpec with Matchers with BeforeAndAfter wi
val underlying = newCCache
val expiredEntry =
Entry("hello", expiresAt = Some(Instant.ofEpochMilli(now.toMillis).plusSeconds(60)))
underlying.put(1, expiredEntry)
newIOCache(underlying).get(1).map(_ shouldBe Some("hello"))
underlying.put(MyInt(1), expiredEntry)
newIOCache(underlying).get(MyInt(1)).map(_ shouldBe Some("hello"))
}
}

behavior of "put"

it should "store the given key-value pair in the underlying cache with no TTL" in ticked { _ =>
val underlying = newCCache
newIOCache(underlying).put(1)("hello", None) *>
IO { underlying.getIfPresent(1) }
newIOCache(underlying).put(MyInt(1))("hello", None) *>
IO { underlying.getIfPresent(MyInt(1)) }
.map(_ shouldBe Entry("hello", None))
}

Expand All @@ -98,8 +100,8 @@ class CaffeineCacheSpec extends AnyFlatSpec with Matchers with BeforeAndAfter wi

val underlying = newCCache

newFCache[IO, String](underlying).put(1)("hello", Some(10.seconds)).map { _ =>
underlying.getIfPresent(1) should be(Entry("hello", expiresAt = Some(now.plusSeconds(10))))
newFCache[IO, String](underlying).put(MyInt(1))("hello", Some(10.seconds)).map { _ =>
underlying.getIfPresent(MyInt(1)) should be(Entry("hello", expiresAt = Some(now.plusSeconds(10))))
}
}

Expand All @@ -108,8 +110,8 @@ class CaffeineCacheSpec extends AnyFlatSpec with Matchers with BeforeAndAfter wi
val now = Instant.ofEpochMilli(ctx.now().toMillis)

val underlying = newCCache
newFCache[IO, String](underlying).put(1)("hello", Some(30.days)).map { _ =>
underlying.getIfPresent(1) should be(
newFCache[IO, String](underlying).put(MyInt(1))("hello", Some(30.days)).map { _ =>
underlying.getIfPresent(MyInt(1)) should be(
Entry("hello", expiresAt = Some(now.plusMillis(30.days.toMillis)))
)
}
Expand All @@ -120,20 +122,20 @@ class CaffeineCacheSpec extends AnyFlatSpec with Matchers with BeforeAndAfter wi
it should "delete the given key and its value from the underlying cache" in ticked { _ =>
val underlying = newCCache
val entry = Entry("hello", expiresAt = None)
underlying.put(1, entry)
underlying.getIfPresent(1) should be(entry)
underlying.put(MyInt(1), entry)
underlying.getIfPresent(MyInt(1)) should be(entry)

newIOCache(underlying).remove(1) *>
IO(underlying.getIfPresent(1)).map(_ shouldBe null)
newIOCache(underlying).remove(MyInt(1)) *>
IO(underlying.getIfPresent(MyInt(1))).map(_ shouldBe null)
}

behavior of "get after put"

it should "store the given key-value pair in the underlying cache with no TTL, then get it back" in ticked { _ =>
val underlying = newCCache
val cache = newIOCache(underlying)
cache.put(1)("hello", None) *>
cache.get(1).map { _ shouldBe defined }
cache.put(MyInt(1))("hello", None) *>
cache.get(MyInt(1)).map { _ shouldBe defined }
}

behavior of "get after put with TTL"
Expand All @@ -143,28 +145,28 @@ class CaffeineCacheSpec extends AnyFlatSpec with Matchers with BeforeAndAfter wi
val underlying = newCCache
val cache = newFCache[IO, String](underlying)

cache.put(1)("hello", Some(5.seconds)) *>
cache.get(1).map { _ shouldBe defined }
cache.put(MyInt(1))("hello", Some(5.seconds)) *>
cache.get(MyInt(1)).map { _ shouldBe defined }
}

it should "store the given key-value pair with the given TTL, then get it back (after a sleep) when not expired" in ticked {
implicit ticker =>
val underlying = newCCache
val cache = newFCache[IO, String](underlying)

cache.put(1)("hello", Some(50.seconds)) *>
cache.put(MyInt(1))("hello", Some(50.seconds)) *>
IO.sleep(40.seconds) *> // sleep, but not long enough for the entry to expire
cache.get(1).map { _ shouldBe defined }
cache.get(MyInt(1)).map { _ shouldBe defined }
}

it should "store the given key-value pair with the given TTL, then return None if the entry has expired" in ticked {
implicit ticker =>
val underlying = newCCache
val cache = newFCache[IO, String](underlying)

cache.put(1)("hello", Some(50.seconds)) *>
cache.put(MyInt(1))("hello", Some(50.seconds)) *>
IO.sleep(60.seconds) *> // sleep long enough for the entry to expire
cache.get(1).map { _ shouldBe empty }
cache.get(MyInt(1)).map { _ shouldBe empty }
}

}

0 comments on commit 91706ea

Please sign in to comment.