PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 12, 2022

[FIXED] How to store a collection of objects into memcache using scala-cache API

 November 12, 2022     memcached, scala     No comments   

Issue

I'm using scalacache with memcache using following dependencies

libraryDependencies += "com.github.cb372" %% "scalacache-memcached" % "0.24.0"

Using the examples from here I'm able to store simple data-types. In extent to existing examples, I wrote the following code to store a 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))
    }
}

And I'm trying to store this Person class using memcache in following way

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)

Here I'm getting following errors.

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")

Any suggestion how to persist a collection into memcache using scala-cache?


Solution

You can store a collection by making the Cache type variable a collection:

    implicit val personSeqCache: Cache[List[Person]] = MemcachedCache("localhost:11211")

This worked for me, even without having to define either a Codec[Person] or a Codec[List[Person]]. When you import scalacache.serialization.binary._ , Java serialization is used to encode any object, according to this



Answered By - FlorianK
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing