我有一个沉重的副作用函数(想想数据库调用),我想将其用作惰性值,以便仅在第一次使用时才调用它(如果从未使用过,则根本不调用)。
我如何使用 ZIO 执行此操作?
如果我的程序看起来像这样,则该函数只被调用一次(但甚至根本不使用结果):
import scalaz.zio.IO
import scalaz.zio.console._
object Main extends scalaz.zio.App {
def longRunningDbAction: IO[Nothing, Integer] = for {
_ <- putStrLn("Calling the database now")
} yield 42
def maybeUseTheValue(x: Integer): IO[Nothing, Unit] = for {
_ <- putStrLn(s"The database said ${x}")
} yield ()
def maybeNeedItAgain(x: Integer): IO[Nothing, Unit] = for {
_ <- putStrLn("Okay, we did not need it again here.")
} yield ()
override def run(args: List[String]): IO[Nothing, Main.ExitStatus] = for {
valueFromDb <- longRunningDbAction
_ <- maybeUseTheValue(valueFromDb)
_ <- maybeNeedItAgain(valueFromDb)
} yield ExitStatus.ExitNow(0)
}
我想我必须传递一个产生Int
而不是已经具体化的Int
的IO
,但是如果我传入仅调用数据库的原始IO
,它将被重复调用:
object Main extends scalaz.zio.App {
def longRunningDbAction: IO[Nothing, Integer] = for {
_ <- putStrLn("Calling the database now")
} yield 42
def maybeUseTheValue(x: IO[Nothing, Integer]): IO[Nothing, Unit] = for {
gettingItNow <- x
_ <- putStrLn(s"The database said ${gettingItNow}")
} yield ()
def maybeNeedItAgain(x: IO[Nothing, Integer]): IO[Nothing, Unit] = for {
gettingItNow <- x
_ <- putStrLn(s"Okay, we need it again here: ${gettingItNow}")
} yield ()
override def run(args: List[String]): IO[Nothing, Main.ExitStatus] = for {
_ <- maybeUseTheValue(longRunningDbAction)
_ <- maybeNeedItAgain(longRunningDbAction)
} yield ExitStatus.ExitNow(0)
}
有没有办法将longRunningDbAction
"包装"成使其变得懒惰的东西?
我想出了以下内容:
def lazyIO[E,A](io: IO[E,A]): IO[Nothing, IO[E, A]] = {
for {
barrier <- Promise.make[Nothing, Unit]
fiber <- (barrier.get *> io).fork
} yield barrier.complete(()) *> putStrLn("getting it") *> fiber.join
}
ZIO 1.0-RC4 的更新版本(支持环境):
def lazyIO[R, E, A](io: ZIO[R, E, A]): ZIO[R, Nothing, ZIO[R, E, A]] = {
for {
barrier <- Promise.make[Nothing, Unit]
fiber <- (barrier.await *> io).fork
} yield barrier.succeed(()) *> fiber.join
}
因此,这是一个 IO,它采用 IO 并返回它的惰性版本。
它的工作原理是启动一个运行原始io
的fiber
,但只有在承诺(barrier
)完成后
。惰性 IO 首先完成该barrier
(如果它是第一个这样做的 IO,则将取消阻止fiber
,进而运行包装的io
),然后联接fiber
以检索计算结果。
有了这个,我可以做
override def run(args: List[String]): IO[Nothing, Main.ExitStatus] = for {
valueFromDb <- lazyIO(longRunningDbAction)
_ <- maybeUseTheValue(valueFromDb)
_ <- maybeNeedItAgain(valueFromDb)
} yield ExitStatus.ExitNow(0)
控制台输出显示,惰性值确实被拉取了两次,但只有第一个触发了"数据库访问":
getting it
Calling the database now
The database said 42
getting it
Okay, we need it again here: 42
ZIO现在已经记住了。
override def run(args: List[String]): IO[Nothing, Main.ExitStatus] = for {
valueFromDb <- ZIO.memoize(longRunningDbAction)
_ <- maybeUseTheValue(valueFromDb)
_ <- maybeNeedItAgain(valueFromDb)
} yield ExitStatus.ExitNow(0)
它基本上与这个答案相同:源看起来像这样
/**
* Returns an effect that, if evaluated, will return the lazily computed result
* of this effect.
*/
final def memoize: ZIO[R, Nothing, IO[E, A]] =
for {
r <- ZIO.environment[R]
p <- Promise.make[E, A]
l <- Promise.make[Nothing, Unit]
_ <- (l.await *> ((self provide r) to p)).fork
} yield l.succeed(()) *> p.await