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

use json for zio-redis schema

上级 c7fdcd2d
......@@ -80,10 +80,10 @@ Add library dependency
**Use `smt-cacheable-redis`**
> Not Unavailable
> TODO Not unavailable, no distributed lock
```scala
// distributed cache, include dependencies: zio-redis,config,zio-schema, optional (zio-schema-protobuf,zio-schema-derivation for case class)
// distributed cache, include dependencies: zio-redis, config, zio-schema, zio-schema-json, optional (zio-schema-derivation for case class)
// dependsOn `smt-cacheable-core`(not support Scala2.11.x)
"org.bitlap" %% "smt-cacheable-redis" % "<VERSION>"
```
......
......@@ -82,10 +82,10 @@
**使用redis实现的cacheable模块**
> 目前不可用
> TODO,目前不可用,无分布式锁
```scala
// 分布式缓存, 内部包含的依赖: zio-redis, config, zio-schema, 可选的 (zio-schema-protobuf, zio-schema-derivation用于样例类序列化)
// 分布式缓存, 内部包含的依赖: zio-redis, config, zio-schema, zio-schema-json, 可选的 (zio-schema-derivation用于样例类序列化)
// 依赖于`smt-cacheable-core`(不支持 Scala2.11.x)
"org.bitlap" %% "smt-cacheable-redis" % "<VERSION>"
```
......
......@@ -81,7 +81,7 @@ lazy val `cacheable-redis` = (project in file("cacheable-redis"))
"dev.zio" %% "zio-redis" % "0.0.0+381-86c20614-SNAPSHOT", // 实验性质的
"com.typesafe" % "config" % "1.4.2",
"dev.zio" %% "zio-schema" % "0.1.8",
"dev.zio" %% "zio-schema-protobuf" % "0.1.8",
"dev.zio" %% "zio-schema-json" % "0.1.8",
"dev.zio" %% "zio-schema-derivation" % "0.1.8" % Test,
),
excludeDependencies ++= Seq(
......
......@@ -24,7 +24,7 @@ package org.bitlap.cacheable.redis
import com.typesafe.config.{ Config, ConfigFactory }
import org.bitlap.cacheable.core.Utils
import zio.redis.{ Redis, RedisConfig, RedisError, RedisExecutor }
import zio.schema.codec.{ Codec, ProtobufCodec }
import zio.schema.codec.{ Codec, JsonCodec }
import zio.{ Has, Layer, ULayer, ZLayer }
import zio.logging.Logging
......@@ -43,7 +43,7 @@ object ZRedisConfiguration {
private[redis] lazy val disabledLog: Boolean = custom.getBoolean("redis.disabledLog")
private val codec: ULayer[Has[Codec]] = ZLayer.succeed[Codec](ProtobufCodec)
private val codec: ULayer[Has[Codec]] = ZLayer.succeed[Codec](JsonCodec)
lazy val redisLayer: Layer[RedisError.IOError, ZRedisCacheService] =
(((if (disabledLog) Logging.ignore else Utils.logLayer) ++ ZLayer.succeed(redisConf)) >>>
......
redis {
host = "0.0.0.0"
port = 6379
disabledLog = false
}
\ No newline at end of file
......@@ -61,58 +61,58 @@ class CacheableTest extends AnyFlatSpec with Matchers {
ZIO.effect(CacheValue(Random.nextInt() + ""))
}
}
//
// "cacheable4" should "zstream operation is ok with redis" in {
// val chunk = Chunk(Random.nextInt().toString, Random.nextInt().toString, Random.nextInt().toString)
//
// @cacheable(local = false)
// def readStreamFunction(id: Int, key: String): ZStream[Any, Throwable, String] = {
// ZStream.fromIterable(chunk)
// }
//
// println(chunk)
// val result = runtime.unsafeRun(for {
// _ <- ZRedisService.del("CacheableTest-readStreamFunction")
// method <- readStreamFunction(1, "hello").runCollect
// cache <- ZRedisService.hGet[Chunk[String]]("CacheableTest-readStreamFunction", "1-hello")
// } yield method -> cache
// )
// result._1 shouldEqual result._2
// }
//
// "cacheable5" should "entity zstream is ok with redis" in {
// val cacheValue = CacheValue(Random.nextInt().toString)
//
// @cacheable(local = false)
// def readEntityStreamFunction(id: Int, key: String): ZStream[Any, Throwable, CacheValue] = {
// ZStream.fromEffect(ZIO.effect(cacheValue))
// }
//
// val result = runtime.unsafeRun(for {
// _ <- ZRedisService.del("CacheableTest-readEntityStreamFunction")
// method <- readEntityStreamFunction(1, "hello").runHead
// } yield method
// )
//
// result shouldEqual Some(cacheValue)
//}
//
// "cacheable6" should "entity zio is ok with redis" in {
// val cacheValue = CacheValue(Random.nextInt().toString)
//
// @cacheable(local = false)
// def readEntityIOFunction(id: Int, key: String): ZIO[Any, Throwable, CacheValue] = {
// ZIO.effect(cacheValue)
// }
//
// val result = runtime.unsafeRun(for {
// _ <- ZRedisService.del("CacheableTest-readEntityIOFunction")
// method <- readEntityIOFunction(1, "hello")
// cache <- ZRedisService.hGet[CacheValue]("CacheableTest-readEntityIOFunction", "1-hello")
// } yield Some(method) -> Some(cache)
// )
//
// result._1 shouldEqual result._2
//
// }
"cacheable4" should "zstream operation is ok with redis" in {
val chunk = Chunk(Random.nextInt().toString, Random.nextInt().toString, Random.nextInt().toString)
@cacheable(local = false)
def readStreamFunction(id: Int, key: String): ZStream[Any, Throwable, String] = {
ZStream.fromIterable(chunk)
}
println(chunk)
val result = runtime.unsafeRun(for {
_ <- ZRedisService.del("CacheableTest-readStreamFunction")
method <- readStreamFunction(1, "hello").runCollect
cache <- ZRedisService.hGet[Chunk[String]]("CacheableTest-readStreamFunction", "1-hello")
} yield method -> cache.getOrElse(Chunk.empty)
)
result._1 shouldEqual result._2
}
"cacheable5" should "entity zstream is ok with redis" in {
val cacheValue = CacheValue(Random.nextInt().toString)
@cacheable(local = false)
def readEntityStreamFunction(id: Int, key: String): ZStream[Any, Throwable, CacheValue] = {
ZStream.fromEffect(ZIO.effect(cacheValue))
}
val result = runtime.unsafeRun(for {
_ <- ZRedisService.del("CacheableTest-readEntityStreamFunction")
method <- readEntityStreamFunction(1, "hello").runHead
} yield method
)
result shouldEqual Some(cacheValue)
}
"cacheable6" should "entity zio is ok with redis" in {
val cacheValue = CacheValue(Random.nextInt().toString)
@cacheable(local = false)
def readEntityIOFunction(id: Int, key: String): ZIO[Any, Throwable, CacheValue] = {
ZIO.effect(cacheValue)
}
val result = runtime.unsafeRun(for {
_ <- ZRedisService.del("CacheableTest-readEntityIOFunction")
method <- readEntityIOFunction(1, "hello")
cache <- ZRedisService.hGet[CacheValue]("CacheableTest-readEntityIOFunction", "1-hello")
} yield Some(method) -> cache
)
result._1 shouldEqual result._2
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册