mumoshu / play2-memcached

A memcached plugin for Play 2.x
Other
161 stars 66 forks source link

NotSerializableException #15

Open marcosinigaglia opened 11 years ago

marcosinigaglia commented 11 years ago

Hi, i try to use Memcache to store a ebean model object. I save the object but i have this exception: "java.io.NotSerializableException: play.libs.F$Tuple"

Any ideas? Thanks

Enalmada commented 11 years ago

You can only store things that are serializable and something you are trying to store is not. Some extra info that may be relevant: http://stackoverflow.com/questions/11600213/why-doesnt-java-lang-object-implement-the-serializable-interface

jakoblind commented 11 years ago

so lets say I have a case class. How do I make that serializable? When I try to cache an instance of my case class Product below I get an error.

case class Product(id: Int, title: Option[String], desc: Option[String], image: Option[String])

mumoshu commented 11 years ago

You can try java.io.Externalizable to make your own class serializable.

Here's what I have tested:

https://github.com/mumoshu/play2-memcached/commit/e04e1663d

jakoblind commented 11 years ago

Then I must implement some Java interface? I dont like that solution :)

mumoshu commented 11 years ago

Hi,

How about using MessagePack to serialize any object to bytes, then putting it in memcached?

I have read that Pinterest uses MessagePack and memcached this way :)

yokomizor commented 10 years ago

Unfortunately play.mvc.Results is not serializable, then caching HTTP responses does not work:

@Cached(key = "homePage")
public static Result index() {
    return ok("Hello world");
}

Stack:

java.io.NotSerializableException: play.mvc.Results$Status
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183) ~[na:1.7.0_60]
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347) ~[na:1.7.0_60]
        at com.github.mumoshu.play2.memcached.MemcachedPlugin$CustomSerializing.serialize(MemcachedPlugin.scala:96) ~[play2-memcached_2.10-0.6.0.jar:0.6.0]
        at net.spy.memcached.transcoders.SerializingTranscoder.encode(SerializingTranscoder.java:162) ~[spymemcached-2.9.0.jar:2.9.0]
        at net.spy.memcached.MemcachedClient.asyncStore(MemcachedClient.java:291) ~[spymemcached-2.9.0.jar:2.9.0]
anoopsin commented 9 years ago

Any news on this one ?

Enalmada commented 9 years ago

This link helped me as a sample: https://gist.github.com/ramn/5566596 Inlining the important parts, @SerialVersionUID() and extends Serializable here since links can rot...

@SerialVersionUID(15L)
class Animal(name: String, age: Int) extends Serializable {
  ...
}
mkurz commented 7 years ago

If you want to cache a Result just use SerializableResult: https://github.com/playframework/playframework/blob/2.6.1/framework/src/play-cache/src/main/scala/play/api/cache/Cached.scala#L189