Memcontinuationed - Memcached的Scala客户端


Apache
跨平台
Scala

软件简介

Memcontinuationed是一个Memcached的Scala客户端

示例代码:

import com.dongxiguo.memcontinuationed.Memcontinuationed
import com.dongxiguo.memcontinuationed.StorageAccessor
import java.io._
import java.net._
import java.nio.channels.AsynchronousChannelGroup
import java.util.concurrent.Executors
import scala.util.continuations.reset
import scala.util.control.Exception.Catcher

object Sample {

  def main(args: Array[String]) {
    val threadPool = Executors.newCachedThreadPool()
    val channelGroup = AsynchronousChannelGroup.withThreadPool(threadPool)

    // The locator decide where the memcached server is.
    // You may want to implement ketama hashing here.
    def locator(accessor: StorageAccessor[_]) = {
      new InetSocketAddress("localhost", 1978)
    }

    val memcontinuationed = new Memcontinuationed(channelGroup, locator)

    // The error handler
    implicit def catcher:Catcher[Unit] = {
      case e: Exception =>
        scala.Console.err.print(e)
        sys.exit(-1)
    }

    reset {
      memcontinuationed.set(MyKey("hello"), "Hello, World!")
      val result = memcontinuationed.require(MyKey("hello"))
      assert(result == "Hello, World!")
      println(result)
      sys.exit()
    }
  }
}

/**
 * `MyKey` specifies how to serialize the data of key/value pair.
 */
case class MyKey(override val key: String) extends StorageAccessor[String] {

  override def encode(output: OutputStream, data: String, flags: Int) {
    output.write(data.getBytes("UTF-8"))
  }

  override def decode(input: InputStream, flags: Int): String = {
    val result = new Array[Byte](input.available)
    input.read(result)
    new String(result, "UTF-8")
  }
}