如何使用 scala-cache API 将对象集合存储到内存缓存中
How to store a collection of objects into memcache using scala-cache API
我使用 scalacache
和 memcache
使用以下依赖项
libraryDependencies += "com.github.cb372" %% "scalacache-memcached" % "0.24.0"
使用 here 中的示例,我能够存储简单的数据类型。在现有示例的范围内,我编写了以下代码来存储 collection
of Person
objects
final case class Person(id: Int, name: String, company: String) extends
Codec[Person]{
override def encode(value: Person): Array[Byte] =
new String(value.id+"|"+value.name+"|"+value.company).getBytes("utf-8")
override def decode(bytes: Array[Byte]): Codec.DecodingResult[Person] =
Codec.tryDecode(convertToPerson(bytes))
def convertToPerson(bytes: Array[Byte]) : Person = {
val str = new String(bytes, "utf-8")
val array: Array[String] = str.split("|")
Person.apply(array.apply(0).toInt, array.apply(1), array.apply(2))
}
}
我正在尝试使用 memcache
以下列方式存储此 Person
class
val personSeq = List(
Person(1, "Abc", "Xyz"),
Person(2, "Ghi", "Prq"),
Person(3, "Jkl", "Uvw")
)
implicit val cache = MemcachedCache("localhost:11211")
val inserted = put("people")(personSeq)
println(inserted.get)
val result = get("people")
val s = result.getOrElse("None")
println(s)
这里我遇到了以下错误。
Error:(38, 39) Could not find any Codecs for type V.
If you would like to serialize values in a binary format, please import the binary codec:
import scalacache.serialization.binary._
If you would like to serialize values as JSON using circe, please import the circe codec
and provide a circe Encoder[V] and Decoder[V], e.g.:
import scalacache.serialization.circe._
import io.circe.generic.auto._
You will need a dependency on the scalacache-circe module.
See the documentation for more details on codecs.
implicit val cache = MemcachedCache("localhost:11211")
Error:(38, 39) ambiguous implicit values:
both object IntBinaryCodec in trait BinaryPrimitiveCodecs of type scalacache.serialization.binary.package.IntBinaryCodec.type
and object DoubleBinaryCodec in trait BinaryPrimitiveCodecs of type scalacache.serialization.binary.package.DoubleBinaryCodec.type
match expected type scalacache.serialization.Codec[V]
implicit val cache = MemcachedCache("localhost:11211")
关于如何使用 scala-cache 将集合保存到内存缓存中有什么建议吗?
您可以通过将 Cache
类型变量设为集合来存储集合:
implicit val personSeqCache: Cache[List[Person]] = MemcachedCache("localhost:11211")
这对我有用,即使不必定义 Codec[Person]
或 Codec[List[Person]]
。当您 import scalacache.serialization.binary._
时,Java 序列化用于根据 this
对任何对象进行编码
我使用 scalacache
和 memcache
使用以下依赖项
libraryDependencies += "com.github.cb372" %% "scalacache-memcached" % "0.24.0"
使用 here 中的示例,我能够存储简单的数据类型。在现有示例的范围内,我编写了以下代码来存储 collection
of Person
objects
final case class Person(id: Int, name: String, company: String) extends
Codec[Person]{
override def encode(value: Person): Array[Byte] =
new String(value.id+"|"+value.name+"|"+value.company).getBytes("utf-8")
override def decode(bytes: Array[Byte]): Codec.DecodingResult[Person] =
Codec.tryDecode(convertToPerson(bytes))
def convertToPerson(bytes: Array[Byte]) : Person = {
val str = new String(bytes, "utf-8")
val array: Array[String] = str.split("|")
Person.apply(array.apply(0).toInt, array.apply(1), array.apply(2))
}
}
我正在尝试使用 memcache
以下列方式存储此 Person
class
val personSeq = List(
Person(1, "Abc", "Xyz"),
Person(2, "Ghi", "Prq"),
Person(3, "Jkl", "Uvw")
)
implicit val cache = MemcachedCache("localhost:11211")
val inserted = put("people")(personSeq)
println(inserted.get)
val result = get("people")
val s = result.getOrElse("None")
println(s)
这里我遇到了以下错误。
Error:(38, 39) Could not find any Codecs for type V.
If you would like to serialize values in a binary format, please import the binary codec:
import scalacache.serialization.binary._
If you would like to serialize values as JSON using circe, please import the circe codec
and provide a circe Encoder[V] and Decoder[V], e.g.:
import scalacache.serialization.circe._
import io.circe.generic.auto._
You will need a dependency on the scalacache-circe module.
See the documentation for more details on codecs.
implicit val cache = MemcachedCache("localhost:11211")
Error:(38, 39) ambiguous implicit values:
both object IntBinaryCodec in trait BinaryPrimitiveCodecs of type scalacache.serialization.binary.package.IntBinaryCodec.type
and object DoubleBinaryCodec in trait BinaryPrimitiveCodecs of type scalacache.serialization.binary.package.DoubleBinaryCodec.type
match expected type scalacache.serialization.Codec[V]
implicit val cache = MemcachedCache("localhost:11211")
关于如何使用 scala-cache 将集合保存到内存缓存中有什么建议吗?
您可以通过将 Cache
类型变量设为集合来存储集合:
implicit val personSeqCache: Cache[List[Person]] = MemcachedCache("localhost:11211")
这对我有用,即使不必定义 Codec[Person]
或 Codec[List[Person]]
。当您 import scalacache.serialization.binary._
时,Java 序列化用于根据 this