Skip to content

Commit

Permalink
implement custom Serializing with play classloader
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Bille committed Sep 5, 2012
1 parent adae7bb commit 5e1e949
Showing 1 changed file with 39 additions and 4 deletions.
Expand Up @@ -6,7 +6,8 @@ import net.spy.memcached.auth.{PlainCallbackHandler, AuthDescriptor}
import net.spy.memcached.{ConnectionFactoryBuilder, AddrUtil, MemcachedClient}
import play.api.cache.{CacheAPI, CachePlugin}
import play.api.{Logger, Play, Application}
import util.control.Exception.catching
//import util.control.Exception.catching
import scala.util.control.Exception._
import net.spy.memcached.transcoders.{Transcoder, SerializingTranscoder}
import net.spy.memcached.compat.log.{Level, AbstractLogger}

Expand Down Expand Up @@ -68,16 +69,50 @@ class MemcachedPlugin(app: Application) extends CachePlugin {

}

lazy val tc = new SerializingTranscoder().asInstanceOf[Transcoder[Any]]
import java.io._

class CustomSerializing extends SerializingTranscoder{

override protected def deserialize(data: Array[Byte]): java.lang.Object = {
try {
new java.io.ObjectInputStream(new java.io.ByteArrayInputStream(data)) {
override protected def resolveClass(desc: ObjectStreamClass) = {
Class.forName(desc.getName(), false, play.api.Play.current.classloader)
}
}.readObject()
}catch{
case e: Exception => {
Logger.error("Could not deserialize", e);
null
}
}
}

override protected def serialize(obj: java.lang.Object) = {
try {
val bos: ByteArrayOutputStream = new ByteArrayOutputStream()
new ObjectOutputStream(bos).writeObject(obj)
bos.toByteArray()
}catch{
case e: Exception => {
Logger.error("Could not serialize", e)
null
}
}
}
}

//lazy val tc = new SerializingTranscoder().asInstanceOf[Transcoder[Any]]
lazy val tc = new CustomSerializing().asInstanceOf[Transcoder[Any]]

lazy val api = new CacheAPI {

def get(key: String) = {
Logger.info("Getting the cached for key " + key)
Logger.debug("Getting the cached for key " + key)
val future = client.asyncGet(key, tc)
catching[Any](classOf[Exception]) opt {
val any = future.get(1, TimeUnit.SECONDS)
Logger.info("any is " + any.getClass)
Logger.debug("any is " + any.getClass)
any match {
case x: java.lang.Byte => x.byteValue()
case x: java.lang.Short => x.shortValue()
Expand Down

0 comments on commit 5e1e949

Please sign in to comment.