2

我在这里学习教程:http: //typelevel.org/cats/datatypes/freemonad.html并尝试修改它以使用键值存储前面的缓存。到目前为止,这是我想出的,但我遇到了编译器错误valueGetOperation。我明白为什么会出现编译错误,只是不明白如何解决它。使用免费单子时条件行为的最佳实践是什么?

import cats.data.Coproduct
import cats.free.{Free, Inject}

object KvStore {
  sealed trait KvOp[A]
  case class Get[T](key: String) extends KvOp[Option[T]]
  case class Put[T](key: String, value: T) extends KvOp[Unit]
  case class Delete[T](key: String) extends KvOp[Unit]
}

object CacheStore {
  sealed trait CacheOp[A]
  case class Get[T](key: String) extends CacheOp[Option[T]]
  case class Put[T](key: String, value: T) extends CacheOp[Unit]
  case class Delete[T](key: String) extends CacheOp[Unit]
}

type WriteThruCache[A] = Coproduct[KvStore.KvOp, CacheStore.CacheOp, A]

class KvOps[F[_]](implicit I: Inject[KvStore.KvOp, F]) {
  import KvStore._
  def get[T](key: String): Free[F, Option[T]] = Free.inject[KvOp, F](Get(key))
  def put[T](key: String, value: T): Free[F, Unit] = Free.inject[KvOp, F](Put(key, value))
  def delete[T](key: String): Free[F, Unit] = Free.inject[KvOp, F](Delete(key))
}

object KvOps {
  implicit def kvOps[F[_]](implicit I: Inject[KvStore.KvOp, F]): KvOps[F] = new KvOps[F]
}

class CacheOps[F[_]](implicit I: Inject[CacheStore.CacheOp, F]) {
  import CacheStore._
  def get[T](key: String): Free[F, Option[T]] = Free.inject[CacheOp, F](Get(key))
  def put[T](key: String, value: T): Free[F, Unit] = Free.inject[CacheOp, F](Put(key, value))
  def delete[T](key: String): Free[F, Unit] = Free.inject[CacheOp, F](Delete(key))
}

object CacheOps {
  implicit def cacheOps[F[_]](implicit I: Inject[CacheStore.CacheOp, F]): CacheOps[F] = new CacheOps[F]
}

def valueWriteOperation[T](implicit Kv: KvOps[WriteThruCache], Cache: CacheOps[WriteThruCache]): ((String, T) => Free[WriteThruCache, Unit]) = {
  (key: String, value: T)  =>
    for {
      _ <- Kv.put(key, value)
      _ <- Cache.put(key, value)
    } yield ()
}

// This is where I'm stuck
// desired behavior: If the value isn't in the cache, load it from the kv store and put it in the cache
def valueGetOperation[T](implicit Kv: KvOps[WriteThruCache], Cache: CacheOps[WriteThruCache]): ((String) => Free[WriteThruCache, Option[T]]) = {
  (key: String) =>
    for {
      cacheOption <- Cache.get[T](key)
      kvOption <- Kv.get[T](key) if cacheOption.isEmpty // value withFilter is not a member of cats.free.Free[A$A39.this.WriteThruCache,Option[T]]
    } yield cacheOption.orElse(kvOption)
}
4

3 回答 3

4

正如您在for理解中所知道的那样,当您使用if它时,编译器会将其脱糖以调用withFilter方法,如果它不可访问,则它会退回到filter方法。如果它们未实现,您将收到编译器错误。

但是,您可以简单地使用if else

for {
  booleanValue <- myfreeAlbebra.checkCondidtion(arg1, arg2)
  valueToReturn <- if (booleanValue) {
    myfreeAlbebra.someValue
  } else {
    myfreeAlbebra.someOtherValue
  }
} yield valueToReturn

或者,您可以执行以下操作:

for {
  booleanValue  <- myfreeAlbebra.checkCondidtion(arg1, arg2)
  valueToReturnOpt <- myfreeAlbebra.someValue
  fallbackValue <- myfreeAlbebra.someOtherValue
} yield valueToReturnOpt.getOrElse(fallbackValue)

形式一将分配价值valueToReturn取决于booleanValue。因此,只会解释一个分支。后者将评估这两个值并根据是否valueToReturnOpt为空返回其中一个。

我个人会尝试类似:

def valueGetOperation[T](implicit Kv: KvOps[WriteThruCache], Cache: CacheOps[WriteThruCache]): ((String) => Free[WriteThruCache, Option[T]]) = {
  (key: String) =>
    for {
      cacheOption <- Cache.get[T](key)
      returnedValue <- if (cacheOption.isEmpty) Cache.get[T](key) else Kv.get[T](key)
    } yield returnedValue
}
于 2017-01-15T10:22:05.297 回答
0

按照 Mateusz 的建议,这就是我想出的:

def withFallback[A[_], T](loadedValue: Option[T], fallback: => Free[A, Option[T]]): Free[A, Option[T]] = {
  if(loadedValue.isDefined) {
    Free.pure[A, Option[T]](loadedValue)
  } else {
    fallback
  }
}

def valueGetOperation[T](implicit Kv: KvOps[WriteThruCache], Cache: CacheOps[WriteThruCache]): ((String) => Free[WriteThruCache, Option[T]]) = {
  (key: String) =>
    for {
      cachedOption <- Cache.get[T](key)
      actualValue <- withFallback[WriteThruCache, T](cachedOption, fallback = Kv.get[T](key))
    } yield actualValue
}

如果有一个标准构造来实现 withFallback 我很高兴知道它。

于 2017-01-17T01:35:53.920 回答
0

你也可以使用OptionT#orElse.

import cats.data.OptionT

type KV[A] = Free[WriteThruCache, A]

def valueGetOperation[T](
  implicit
  Kv: KvOps[WriteThruCache], 
  Cache: CacheOps[WriteThruCache]
): String => KV[Option[T]] =
  key => OptionT[KV, T](Cache.get[T](key)).orElse(OptionT[KV, T](Kv.get[T](key))).value

或者OptionT#orElseF

def valueGetOperation[T](
  implicit
  Kv: KvOps[WriteThruCache], 
  Cache: CacheOps[WriteThruCache]
): String => KV[Option[T]] = 
  key => OptionT[KV, T](Cache.get[T](key)).orElseF(Kv.get[T](key)).value

请注意,使用-Ypartial-unificationScala 2.12 中的标志,您不需要KV类型别名,您可以编写OptionT(...)而不是OptionT[KV, T](...).

于 2017-02-07T14:33:53.153 回答