Clj-xmemcachedisanopensourcememcachedclientforclojurewrappingxmemcached.Xmemcachedisanopensourcehighperformancememcachedclientforjava.
Toincludeclj-xmemcached,add:
<code> [clj-xmemcached "0.1.1"] </code>
toyourproject.clj.
<!–Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/–>(use [clj-xmemcached.core])
(def client (xmemcached “host:port“))
(def client (xmemcached “host1:port1 host2:port2“ :protocol “binary“))
Thenwecreateamemcachedclientusingbinaryprotocoltotalkwithmemcachedservershost1:port1andhost2:port2.
Validoptionsincluding:
<code> :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. </code>
Storeitems
<!–
CodehighlightingproducedbyActiproCodeHighlighter(freeware)
http://www.CodeHighlighter.com/
–>(xsetclient“key““dennis“)
(xsetclient“key““dennis“100)
(xappendclient“key““zhuang“)
(xprependclient“key““hello,“)
Thevalue100istheexpiretimefortheiteminseconds.Store
functionsincludexset,xadd,xreplace,xappendandxprepend.Pleaseusedoc
toprintdocumentationforthesefunctions.
<!–Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/–>(xget client “key“)
(xget client “key1“ “key2“ “key3“)
(xgets client “key“)
xgetsreturnsavalueincludingacasvalue,forexample:
<!–Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/–> {:value “hello,dennis zhuang“, :class net.rubyeye.xmemcached.GetsResponse, :cas 396}
AndbulkgetreturnsaHashMapcontainsexistentitems.
<!–
CodehighlightingproducedbyActiproCodeHighlighter(freeware)
http://www.CodeHighlighter.com/
–>(xincrclient“num“1)
(xdecrclient“num“1)
(xincrclient“num“10)
Abovecodestrytoincrease/decreaseanumberinmemcachedwithkey“num”,andiftheitemisnotexists,thensetittozero.
<!–Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/–>(xdelete client “num“)
Compareandset
<!–Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/–>(xcas client “key“ inc)
WeuseincfunctiontoincreasethecurrentvalueinmemcachedandtrytocompareandsetitatmostInteger.MAX_VALUEtimes.
xcascanbecalledas:
<!–Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/–> (xcas client key cas-fn max-times)
Thecas-fnisafunctiontoreturnanewvalue,setthenewvalueto
<!–Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/–>(cas-fn current-value)
Shutdown
<!–Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/–>(xshutdown client)
Flush
<!–Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/–>(xflush client)
(xflush client (InetSocketAddress. host port))
Statistics
<!–Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/–>(xstats client)
Pleaseseetheexamplecodeinexample/demo.clj
Copyright(C)2011-2014denniszhuang[killme2008@gmail.com]
DistributedundertheEclipsePublicLicense,thesameasClojure.
转载于:https://blog.51cto.com/aliapp/1325887