clj-xmemcached: memcached client for clojure




# clj-xmemcached

Clj-xmemcached is an opensource memcached client for clojure wrapping xmemcached. Xmemcached is an opensource high performance memcached client for java.

## Leiningen Usage

To include clj-xmemcached,add:

[clj-xmemcached “0.1.1”] </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%">&lt;!--Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/--&gt;<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.

### Store items

<!–

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

–>(xset client key dennis)
(xset client
key dennis 100)
(xappend client
key zhuang)
(xprepend client
key hello,)


The value 100 is the expire time for the item in seconds.Store
functions include xset,xadd,xreplace,xappend and xprepend.Please use doc
to print documentation for these functions.

### Get items

<!–Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/-->(xget client key)
(xget client
key1 key2 key3)
(xgets client
key)

xgets returns a value including a cas value,for example:
<!–Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/--> {:value hello,dennis zhuang, :class net.rubyeye.xmemcached.GetsResponse, :cas 396}

And bulk get returns a HashMap contains existent items.

### Increase/Decrease numbers

<!–

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

–>(xincr client num 1)
(xdecr client
num 1)
(xincr client
num 1 0)


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

<!–Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/-->(xdelete client num)

Compare and set

<!–Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/-->(xcas client key inc)

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:
<!–Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/--> (xcas client key cas-fn max-times)



The cas-fn is a function to return a new value,set the new value to
<!–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)

Example

Please see the example code in example/demo.clj

License

Copyright (C) 2011-2014 dennis zhuang[killme2008@gmail.com]

Distributed under the Eclipse Public License, the same as Clojure.

企业级互联网架构Aliware,让您的业务能力云化:https://www.aliyun.com/aliware