支持 redis 的 Scalacache
Scalacache with redis support
我正在尝试将 redis 集成到 scalacache 中。键通常是字符串,但值可以是对象、Set[String] 等。缓存由 this
初始化
val cache: RedisCache = RedisCache(config.host, config.port)
private implicit val scalaCache: ScalaCache[Array[Byte]] = ScalaCache(cacheService.cache)
但是在调用 put 时,我收到了这个错误 "Could not find any Codecs for type Set[String] and Repr"。看起来我需要按照建议 here 为我的缓存输入提供编解码器,所以我添加了,
class A extends Codec[Set[String], Array[Byte]] with GZippingBinaryCodec[Set[String]]
即使在之后,我的 class A 也抛出了同样的错误。我错过了什么。
正如您在 link 中提到的,您可以将值序列化为二进制格式:
import scalacache.serialization.binary._
或作为 JSON 使用 circe:
import scalacache.serialization.circe._
import io.circe.generic.auto._
看起来它在下一个版本中通过二进制和循环序列化解决了。我使用的是版本 10 并通过以下方式解决,
implicit object SetBindaryCodec extends Codec[Any, Array[Byte]] {
override def serialize(value: Any): Array[Byte] = {
val stream: ByteArrayOutputStream = new ByteArrayOutputStream()
val oos = new ObjectOutputStream(stream)
oos.writeObject(value)
oos.close()
stream.toByteArray
}
override def deserialize(data: Array[Byte]): Any = {
val ois = new ObjectInputStream(new ByteArrayInputStream(data))
val value = ois.readObject
ois.close()
value
}
}
与时俱进的好处。将升级版本,发布它以防万一有人需要它。
我正在尝试将 redis 集成到 scalacache 中。键通常是字符串,但值可以是对象、Set[String] 等。缓存由 this
初始化val cache: RedisCache = RedisCache(config.host, config.port)
private implicit val scalaCache: ScalaCache[Array[Byte]] = ScalaCache(cacheService.cache)
但是在调用 put 时,我收到了这个错误 "Could not find any Codecs for type Set[String] and Repr"。看起来我需要按照建议 here 为我的缓存输入提供编解码器,所以我添加了,
class A extends Codec[Set[String], Array[Byte]] with GZippingBinaryCodec[Set[String]]
即使在之后,我的 class A 也抛出了同样的错误。我错过了什么。
正如您在 link 中提到的,您可以将值序列化为二进制格式:
import scalacache.serialization.binary._
或作为 JSON 使用 circe:
import scalacache.serialization.circe._
import io.circe.generic.auto._
看起来它在下一个版本中通过二进制和循环序列化解决了。我使用的是版本 10 并通过以下方式解决,
implicit object SetBindaryCodec extends Codec[Any, Array[Byte]] {
override def serialize(value: Any): Array[Byte] = {
val stream: ByteArrayOutputStream = new ByteArrayOutputStream()
val oos = new ObjectOutputStream(stream)
oos.writeObject(value)
oos.close()
stream.toByteArray
}
override def deserialize(data: Array[Byte]): Any = {
val ois = new ObjectInputStream(new ByteArrayInputStream(data))
val value = ois.readObject
ois.close()
value
}
}
与时俱进的好处。将升级版本,发布它以防万一有人需要它。