Pickling Case Classes to Memcached With Scala

| Comments

Recently, I’ve been working on a rewrite of Sluggy Freelance - a friend’s site which I’ve worked on for about a decade now. Caching is a big part of keeping site cost down, and over the years I’ve come to trust Memcached. Fast, lightweight, and easy, Memcached has served me well over the years over several iterations of the site… from Perl, PHP, and Python.

Today, I’m rewriting the site in Scala with Scalatra and React.js. As a result, I’m discovering all sorts of new fun that I haven’t dealt with in Scala yet. One of these is using Memcached, and specifically serialising/deserialising Case Classes. With the combination of a good Memcached library - shade, in this case - and Scala Pickling*, I’ve found a powerful combination.

So far, I’m working with a datastructure representing navigation: the current book, chapter, and section as well as lists of others of those based on context. As a bonus… I’m actually serialising case classes that are fetched from Slick.

Here’s the navigation structure:

1 case class NavigationStruct(
2   book: Book,
3   allBooks: Seq[Book],
4   section: Section,
5   currentSections: Seq[Section],
6   chapter: Chapter,
7   currentChapters: Seq[Chapter]
8 )

We’ll leave the content of Book, Section, and Chapter out, but suffice to say they are standard case classes made up of a variety of primitive fields and a few Joda LocalDates for good measure.

Here are my imports, setting up memcached :

1 import shade.memcached._
2 // shade uses futures so you'll need the execution context
3 import scala.concurrent.ExecutionContext.Implicits.{global => ec}

Now, given the topic of conversation what we need is two things:

  1. Code to convert a NavigationStruct case class to and from binary via pickling
  2. Code to tell the shade memcached client how to convert scala data to and from memcached

Luckily, we can combine these two things. shade provides a Codec[T] mechanism for encoding, into which we can wire our pickling. Here’s a rough sketch of my Codec[T] for NavigationStruct:

1 implicit object NavStructCodec extends Codec[NavigationStruct] {
2     def serialize(struct: NavigationStruct): Array[Byte] =  ???
3 
4     def deserialize(data: Array[Byte]): NavigationStruct = ???
5   }

Our implicit Codec[T] object has two methods: serialize(T): Array[Byte] and deserialize(Array[Byte]): T. This is the shade memcached specific code: It leaves it up to us how we want to serialize/deserialize as long as we work with Arrays of bytes.

And so, in comes Scala pickling…

1 import scala.pickling._
2 import scala.pickling.Defaults._
3 import scala.pickling.binary._

While I import binary support from Pickling, it is worth noting that there’s also JSON Support built in as outlined in [the docs]](https://github.com/scala/pickling).

With the scala.pickling.Defaults._ import comes a few implicits, which let us “pickle” (aka serialize) arbitrary objects:

1 struct.pickle.value

And “unpickle” (aka deserialize) from raw bytes back to something useful:

1 data.unpickle[NavigationStruct]

That’s actually it, at least for my simple data structures. Here’s the final implicit object:

1 implicit object NavStructCodec extends Codec[NavigationStruct] {
2     def serialize(struct: NavigationStruct): Array[Byte] = { 
3       struct.pickle.value 
4     }
5 
6     def deserialize(data: Array[Byte]): NavigationStruct = {
7       data.unpickle[NavigationStruct]
8     }
9   }

This leaves me with an easy route to fetch/save between memcached, where codec handling is automatically taken care of:

1 memcached.awaitGet[NavigationStruct](key)(NavStructCodec) match {
2   case Some(navData) => 
3     navData
4   case None =>
5     val nav = getNavigationData(date)
6     memcached.awaitSet(key, nav, timeout)
7     nav
8 }

Suffice to say, I’m quite happy with the simplicity.

* It’s been mentioned to me as well that scodec is a great solution for serialisation too, but I haven’t yet gone down that road.

Comments