Memcached的Scala客户端:Memcontinuationed
Memcontinuationed是一个Memcached的Scala客户端。Memcontinuationed是JVM上最快的memcached客户端,比spymemcached 更快。
示例代码:
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") } }
本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!