提交 fb937180 编写于 作者: 梦境迷离's avatar 梦境迷离

use stm for caffeine

上级 e81841ed
......@@ -41,8 +41,8 @@ object Implicits {
ZStream.fromEffect(STM.atomically {
STM.foreach_(identities)(key => ZCaffeine.del(key))
}) *> ({
Utils.debugS(s"Caffeine ZStream update >>> identities:[$identities]")
.when(!ZCaffeine.disabledLog)
// 使用when会导致流变成空,继而使用runHead返回None
if (ZCaffeine.disabledLog) ZStream.unit else Utils.debugS(s"Caffeine ZStream update >>> identities:[$identities]")
} *> business)
}
}
......@@ -61,7 +61,7 @@ object Implicits {
}
for {
ret <- ZStream.fromEffect(stmResult)
_ <- Utils.debugS(s"Caffeine ZStream getIfPresent >>> identity:[$key],field:[$field],result:[$ret]").when(!ZCaffeine.disabledLog)
_ <- if (ZCaffeine.disabledLog) ZStream.unit else Utils.debugS(s"Caffeine ZStream getIfPresent >>> identity:[$key],field:[$field],result:[$ret]")
r <- ZStream.fromIterable(ret)
} yield r
}
......@@ -73,7 +73,7 @@ object Implicits {
STM.foreach_(identities)(key => ZCaffeine.del(key))
} *> {
business.tap(updateResult => Utils.debug(s"Caffeine ZIO update >>> identities:[$identities],updateResult:[$updateResult]")
.when(!ZCaffeine.disabledLog))
.unless(ZCaffeine.disabledLog))
}
}
}
......@@ -90,7 +90,7 @@ object Implicits {
ret <- chunk.fold(ZCaffeine.hSet[T](key, field, syncResult))(c => STM.succeed(c))
} yield ret
}.tap(ret => Utils.debug(s"Caffeine ZIO getIfPresent >>> identity:[$key],field:[$field],result:[$ret]")
.when(!ZCaffeine.disabledLog))
.unless(ZCaffeine.disabledLog))
}
}
}
caffeine {
maximumSize = 100
expireAfterWriteSeconds = 60
disabledLog = false
}
\ No newline at end of file
......@@ -28,6 +28,7 @@ import zio.stream.ZStream
import zio.{ Task, ZIO }
import org.bitlap.cacheable.core.cacheEvict
import zio.stm.STM
import zio.Chunk
import scala.util.Random
import java.util
......@@ -164,4 +165,22 @@ class CacheableTest extends AnyFlatSpec with Matchers {
)
result._1 shouldEqual result._2
}
"cacheable9" should "zio operation is ok with chunk" in {
val chunk = Chunk(Random.nextInt().toString, Random.nextInt().toString, Random.nextInt().toString)
@cacheable(local = true)
def readIOFunction(id: Int, key: String): ZIO[Any, Throwable, Chunk[String]] = {
ZIO.succeed(chunk)
}
println(chunk)
val result = runtime.unsafeRun(for {
_ <- STM.atomically(ZCaffeine.del("CacheableTest-readIOFunction"))
method <- readIOFunction(1, "hello")
cache <- STM.atomically(ZCaffeine.hGet[Chunk[String]]("CacheableTest-readIOFunction", "1-hello").map(_.getOrElse(Chunk.empty)))
} yield method -> cache
)
result._1 shouldEqual result._2
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册