Memcotiuatioed是Scala的异步memcached客户端。Memcotiuatioed是JVM上最快的Memcached客户端,比spymemcached或 Whali的客户端要快得多。
示例代码:
importcom.dogxiguo.memcotiuatioed.Memcotiuatioedimportcom.dogxiguo.memcotiuatioed.StorageAccessorimportjava.io._importjava.et._importjava.io.chaels.AsychroousChaelGroupimportjava.util.cocurret.Executorsimportscala.util.cotiuatios.resetimportscala.util.cotrol.Exceptio.CatcherobjectSample{ defmai(args:Array[Strig]){ valthreadPool=Executors.ewCachedThreadPool() valchaelGroup=AsychroousChaelGroup.withThreadPool(threadPool) //Thelocatordecidewherethememcachedserveris. //Youmaywattoimplemetketamahashighere. deflocator(accessor:StorageAccessor[_])={ ewIetSocketAddress("localhost",1978) } valmemcotiuatioed=ewMemcotiuatioed(chaelGroup,locator) //Theerrorhadler implicitdefcatcher:Catcher[Uit]={ casee:Exceptio=> scala.Cosole.err.prit(e) sys.exit(-1) } reset{ memcotiuatioed.set(MyKey("hello"),"Hello,World!") valresult=memcotiuatioed.require(MyKey("hello")) assert(result=="Hello,World!") pritl(result) sys.exit() } }}/** *`MyKey`specifieshowtoserializethedataofkey/valuepair. */caseclassMyKey(overridevalkey:Strig)extedsStorageAccessor[Strig]{ overridedefecode(output:OutputStream,data:Strig,flags:It){ output.write(data.getBytes("UTF-8")) } overridedefdecode(iput:IputStream,flags:It):Strig={ valresult=ewArray[Byte](iput.available) iput.read(result) ewStrig(result,"UTF-8") }}
评论