Memcontinuationed是Scala的异步memcached客户端。Memcontinuationed是JVM上最快的Memcached客户端,比spymemcached或 Whalin的客户端要快得多。
示例代码:
importcom.dongxiguo.memcontinuationed.Memcontinuationedimportcom.dongxiguo.memcontinuationed.StorageAccessorimportjava.io._importjava.net._importjava.nio.channels.AsynchronousChannelGroupimportjava.util.concurrent.Executorsimportscala.util.continuations.resetimportscala.util.control.Exception.CatcherobjectSample{ defmain(args:Array[String]){ valthreadPool=Executors.newCachedThreadPool() valchannelGroup=AsynchronousChannelGroup.withThreadPool(threadPool) //Thelocatordecidewherethememcachedserveris. //Youmaywanttoimplementketamahashinghere. deflocator(accessor:StorageAccessor[_])={ newInetSocketAddress("localhost",1978) } valmemcontinuationed=newMemcontinuationed(channelGroup,locator) //Theerrorhandler implicitdefcatcher:Catcher[Unit]={ casee:Exception=> scala.Console.err.print(e) sys.exit(-1) } reset{ memcontinuationed.set(MyKey("hello"),"Hello,World!") valresult=memcontinuationed.require(MyKey("hello")) assert(result=="Hello,World!") println(result) sys.exit() } }}/** *`MyKey`specifieshowtoserializethedataofkey/valuepair. */caseclassMyKey(overridevalkey:String)extendsStorageAccessor[String]{ overridedefencode(output:OutputStream,data:String,flags:Int){ output.write(data.getBytes("UTF-8")) } overridedefdecode(input:InputStream,flags:Int):String={ valresult=newArray[Byte](input.available) input.read(result) newString(result,"UTF-8") }}
评论