F多态代码中如何使用guava cache loader

How use guave cache loader in F polymorphic code

我有一项服务,那是 return 的笑话 from official example:

  final case class JokeError(e: Throwable) extends RuntimeException

  def impl[F[_] : Sync](C: Client[F]): Jokes[F] = new Jokes[F] {
    val dsl = new Http4sClientDsl[F] {}
    import dsl._
    def get: F[Jokes.Joke] = {
      C.expect[Joke](GET(uri"https://icanhazdadjoke.com/"))
        .adaptError { case t => JokeError(t) }
    }
  }

但我想用番石榴缓存缓存第一个请求的笑话(只是通过常量键,这无关紧要):

object Jokes {
  def apply[F[_]](implicit ev: Jokes[F]): Jokes[F] = ev

  final case class Joke(joke: String) extends AnyRef
  object Joke {
    implicit val jokeDecoder: Decoder[Joke] = deriveDecoder[Joke]
    implicit def jokeEntityDecoder[F[_]: Sync]: EntityDecoder[F, Joke] =
      jsonOf
    implicit val jokeEncoder: Encoder[Joke] = deriveEncoder[Joke]
    implicit def jokeEntityEncoder[F[_]: Applicative]: EntityEncoder[F, Joke] =
      jsonEncoderOf
  }

  final case class JokeError(e: Throwable) extends RuntimeException

  def impl[F[_]: Sync](C: Client[F]): Jokes[F] = new Jokes[F]{

    val cacheLoader : CacheLoader[String, Joke] = new CacheLoader[String, Joke] {
      override def load(key: String): Joke = {
        import dsl._
        val joke: F[Joke] = C.expect[Joke](GET(uri"https://icanhazdadjoke.com/"))
          .adaptError{ case t => JokeError(t)}
        //? F[Joke] => Joke
        null
      }
    }

    val cache = CacheBuilder.newBuilder().build(cacheLoader)

    val dsl = new Http4sClientDsl[F]{}

    def get: F[Jokes.Joke] = {
       //it's ok?
       cache.get("constant").pure[F]
    }
  }
}

如您所见,cacheLoader 需要 "materialized" 值 F[Joke] => Joke。并缓存 return 没有 F

的纯值

如何在 F 多态代码中使用此缓存?

您基本上是在询问如何 运行 在 F 中编写多态代码,为此您需要对 F 进行 Effect 约束。

此外,您需要使用 delay 而不是使用 pure,因为从缓存中获取值是一种副作用。

val cacheLoader : CacheLoader[String, Joke] = new CacheLoader[String, Joke] {
  override def load(key: String): Joke = {
    import dsl._
    val joke: F[Joke] = C.expect[Joke](GET(uri"https://icanhazdadjoke.com/"))
      .adaptError{ case t => JokeError(t)}

    // This is a side effect, but can't avoid it due to the way the API is designed
    joke.toIO.unsafeRunSync()
  }
}

val cache = CacheBuilder.newBuilder().build(cacheLoader)

val dsl = new Http4sClientDsl[F]{}

def get: F[Jokes.Joke] = {
   // This is okay :)
   Sync[F].delay(cache.get("constant"))
}

顺便说一句,如果你想使用与 http4s 互操作性非常好的东西,我强烈推荐 mules。在这里查看: https://github.com/ChristopherDavenport/mules