</pre> to your project.clj. ## Usage ### Create a client <div style="background-color: #eeeeee;font-size: 13px;border: 1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%"><!--Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/--><span style="color: #000000">(use [clj</span><span style="color: #000000">-</span><span style="color: #000000">xmemcached.core]) (def client (xmemcached </span><span style="color: #000000">"</span><span style="color: #000000">host:port</span><span style="color: #000000">"</span><span style="color: #000000">)) (def client (xmemcached </span><span style="color: #000000">"</span><span style="color: #000000">host1:port1 host2:port2</span><span style="color: #000000">"</span><span style="color: #000000"> :protocol </span><span style="color: #000000">"</span><span style="color: #000000">binary</span><span style="color: #000000">"</span><span style="color: #000000">))</span></div> Then we create a memcached client using binary protocol to talk with memcached servers host1:port1 and host2:port2. Valid options including: <pre>
:name Client’s name :protocol Protocol to talk with memcached,a string value in text,binary or kestrel,default is text protocol. :hash Hash algorithm,a string value in consistent or standard,default is standard hash. :timeout Operation timeout in milliseconds,default is five seconds. :pool Connection pool size,default is one.
`</div> xgets returns a value including a cas value,for example: <div style="background-color: #eeeeee;font-size: 13px;border: 1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%"><!--Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/--><span style="color: #000000"> {:value </span><span style="color: #000000">"</span><span style="color: #000000">hello,dennis zhuang</span><span style="color: #000000">"</span><span style="color: #000000">, :</span><span style="color: #0000ff">class</span><span style="color: #000000"> net.rubyeye.xmemcached.GetsResponse, :cas </span><span style="color: #000000">396</span><span style="color: #000000">}</span>
</div> And bulk get returns a HashMap contains existent items. ### Increase/Decrease numbers <div style="background-color: #eeeeee;font-size: 13px;border: 1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%"><!--</div> Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #000000">(xincr client </span><span style="color: #000000">"</span><span style="color: #000000">num</span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">) (xdecr client </span><span style="color: #000000">"</span><span style="color: #000000">num</span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">) (xincr client </span><span style="color: #000000">"</span><span style="color: #000000">num</span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">)</span> </div> Above codes try to increase/decrease a number in memcached with key "num",and if the item is not exists,then set it to zero. ### Delete items <div style="background-color: #eeeeee;font-size: 13px;border: 1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%"><!--Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/--><span style="color: #000000">(xdelete client </span><span style="color: #000000">"</span><span style="color: #000000">num</span><span style="color: #000000">"</span><span style="color: #000000">)</span>
`</div> We use inc function to increase the current value in memcached and try to compare and set it at most Integer.MAX_VALUE times. xcas can be called as:
<div style="background-color: #eeeeee;font-size: 13px;border: 1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%"><!--Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/--><span style="color: #000000"> (xcas client key cas</span><span style="color: #000000">-</span><span style="color: #000000">fn max</span><span style="color: #000000">-</span><span style="color: #000000">times)</span></div>
The cas-fn is a function to return a new value,set the new value to <div style="background-color: #eeeeee;font-size: 13px;border: 1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%"><!--Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/--><span style="color: #000000">(cas</span><span style="color: #000000">-</span><span style="color: #000000">fn current</span><span style="color: #000000">-</span><span style="color: #000000">value)</span>
Please see the example code in example/demo.clj
Copyright (C) 2011-2014 dennis zhuang[killme2008@gmail.com]
Distributed under the Eclipse Public License, the same as Clojure.
本文来源于"阿里中间件团队播客",原文发表时间" 2011-10-31"