Skip to content

Caches are tricky - Let Mules Haul its Weight.

License

Notifications You must be signed in to change notification settings

OddKristensen/mules

 
 

Repository files navigation

mules Build Status Maven Central

Caches are tricky - Let Mules Haul its Weight.

Quickstart

To use mules in an existing SBT project with Scala 2.11 or a later version, add the following dependencies to your build.sbt depending on your needs:

libraryDependencies ++= Seq(
  "io.chrisdavenport" %% "mules"     % "<version>"
)

Then the import

import io.chrisdavenport.mules._
import cats.effect.IO
import scala.concurrent.duration._
import cats.effect.unsafe.implicits.global

Now Lets Use What We have built.

val getInserted = for {
  cache <- MemoryCache.ofSingleImmutableMap[IO, String, Int](Some(TimeSpec.unsafeFromDuration(1.second)))
  _     <- cache.insert("Foo", 1)
  value <- cache.lookup("Foo")
} yield value

getInserted.unsafeRunSync
// res0: Option[Int] = Some(1)

val getRemoved = for {
  cache <- MemoryCache.ofSingleImmutableMap[IO, String, Int](None)
  _     <- cache.insert("Foo", 1)
  _     <- cache.delete("Foo")
  value <- cache.lookup("Foo")
} yield value

getRemoved.unsafeRunSync
// res1: Option[Int] = None

val getAfterPurged = for {
  cache <- MemoryCache.ofSingleImmutableMap[IO, String, Int](Some(TimeSpec.unsafeFromDuration(1.second)))
  _     <- cache.insert("Foo", 1)
  _     <- IO.sleep(2.seconds)
  _     <- cache.purgeExpired
  value <- cache.lookupNoUpdate("Foo")
} yield value

getAfterPurged.unsafeRunSync
// res2: Option[Int] = None

val lookupAfterInterval = for {
  cache <- MemoryCache.ofSingleImmutableMap[IO, String, Int](Some(TimeSpec.unsafeFromDuration(1.second)))
  _     <- cache.insert("Foo", 1)
  _     <- IO.sleep(2.seconds)
  value <- cache.lookup("Foo")
} yield value

lookupAfterInterval.unsafeRunSync
// res3: Option[Int] = None

About

Caches are tricky - Let Mules Haul its Weight.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Scala 100.0%