Skip to content

Commit

Permalink
Added nippy transcoder
Browse files Browse the repository at this point in the history
  • Loading branch information
killme2008 committed Jan 26, 2014
1 parent 285f478 commit a60f0d1
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 15 deletions.
9 changes: 6 additions & 3 deletions README.md
Expand Up @@ -5,8 +5,9 @@ An opensource memcached client for clojure wraps [xmemcached](http://code.google
##Leiningen Usage

To use clj-xmemcached,add:
```clj
[clj-xmemcached "0.2.3"]

```
[clj-xmemcached "0.2.3-beta"]
```
to your project.clj.

Expand Down Expand Up @@ -171,9 +172,11 @@ Because `memcached` function returns a delayed object,so if you want to get the

### Transcoders

We use [SerializationTranscoder](http://xmemcached.googlecode.com/svn/trunk/apidocs/net/rubyeye/xmemcached/transcoders/SerializingTranscoder.html) by default,it will encode/decode values by java serialization.
We use [SerializationTranscoder](http://xmemcached.googlecode.com/svn/trunk/apidocs/net/rubyeye/xmemcached/transcoders/SerializingTranscoder.html) by default,it will encode/decode values using java serialization.
But since `0.2.2`, we provide a new transcoder `clj-json-transcoder` to encode/decode values using [clojure.data.json](https://github.com/clojure/data.json).It is suitable to integrate with other systems written in other languages.

And we add `nippy-transcoder` in 0.2.3, it use [nippy](https://github.com/ptaoussanis/nippy) for serialization.

### Example

Please see the example code in [example/demo.clj](https://github.com/killme2008/clj-xmemcached/blob/master/example/demo.clj)
Expand Down
4 changes: 2 additions & 2 deletions example/benchmark.clj
Expand Up @@ -2,7 +2,7 @@
(:require [clj-xmemcached.core :as xm]))


(def client (xm/memcached "localhost:11211"))
(def client (xm/memcached "localhost:11211" :transcoder xm/nippy-transcoder))
(def threads 50)
(def repeats 10000)

Expand Down Expand Up @@ -34,4 +34,4 @@
(benchmark "Benchmark set & get"
(xm/set (str (+ times (* thread repeats))) value)
(xm/get (str (+ times (* thread repeats)))))
(xm/shutdown @client)
(xm/shutdown @client)
17 changes: 9 additions & 8 deletions project.clj
@@ -1,14 +1,15 @@
(defproject clj-xmemcached "0.2.3"
(defproject clj-xmemcached "0.2.3-beta"
:author "dennis zhuang[killme2008@gmail.com]"
:description "An opensource memcached client for clojure wrapping xmemcached"
:dependencies [[org.clojure/clojure "1.4.0"]
[org.clojure/data.json "0.2.2"]
:dependencies [[org.clojure/data.json "0.2.4"]
[org.slf4j/slf4j-log4j12 "1.5.6"]
[com.googlecode.xmemcached/xmemcached "1.4.2"]]
[com.taoensso/nippy "2.5.2"]
[com.googlecode.xmemcached/xmemcached "1.4.1"]]
:test-paths ["test" "example"]
:profiles {:dev {:dependencies [[log4j/log4j "1.2.16"]
[org.slf4j/slf4j-log4j12 "1.5.6"]]
:resource-paths ["dev"]}}
:plugins [[lein-exec "0.3.0"]
[codox "0.6.4"]]
:warn-on-reflection true)
:resource-paths ["dev"]}
:1.4 {:dependencies [[org.clojure/clojure "1.4.0"]]}
:1.5 {:dependencies [[org.clojure/clojure "1.5.1"]]}}
:plugins [[lein-exec "0.3.0"]]
:warn-on-reflection true)
24 changes: 22 additions & 2 deletions src/clj_xmemcached/core.clj
Expand Up @@ -9,7 +9,8 @@
(net.rubyeye.xmemcached.command BinaryCommandFactory KestrelCommandFactory TextCommandFactory)
(java.net InetSocketAddress))
(:refer-clojure :exclude [get set replace])
(:require [clojure.data.json :as json])
(:require [clojure.data.json :as json]
[taoensso.nippy :as nippy])
(:use
[clojure.walk :only [walk]]))

Expand Down Expand Up @@ -209,6 +210,25 @@
`(= byte-array-class
(class ~x)))

(def nippy-transcoder (reify Transcoder
(encode [this obj]
(cond (string? obj) (CachedData. 0 (.getBytes ^String obj "utf-8") (* 1024 1024) -1)
(bytes? obj) (CachedData. 2 (bytes obj) (* 1024 1024) -1)
:else
(CachedData. 1 (nippy/freeze obj) (* 1024 1024) -1)))
(decode [this ^CachedData data]
(let [ ^bytes bs (.getData data)]
(case (.getFlag data)
0 (String. ^bytes bs "utf-8")
1 (nippy/thaw bs)
2 bs)))
(setPrimitiveAsString [this b])
(setPackZeros [this b])
(setCompressionThreshold [this b])
(isPrimitiveAsString [this] false)
(isPackZeros [this] false)
(setCompressionMode [this m])))

(def clj-json-transcoder (reify Transcoder
(encode [this obj]
(cond (string? obj) (CachedData. 0 (.getBytes ^String obj "utf-8") (* 1024 1024) -1)
Expand Down Expand Up @@ -263,4 +283,4 @@
(let [v# ~load]
(when v#
(add ~key v# ~expire))
v#))))
v#))))
18 changes: 18 additions & 0 deletions test/clj_xmemcached/test/core.clj
Expand Up @@ -72,6 +72,24 @@
(flush-all)
(shutdown))))))

(deftest test-nippy-transcoder
(let [cli (memcached test-servers
:transcoder nippy-transcoder)]
(with-client cli
(try
(is (add "a" 1))
(is (not (add "a" 2)))
(is (= 1 (get "a")))
(is (replace "a" 2))
(is (= 2 (get "a")))
(is (set "a" 3))
(is (= 3 (get "a")))
(is (delete "a"))
(is (nil? (get "a")))
(finally
(flush-all)
(shutdown))))))

(deftest test-set-client!
(let [cli (memcached test-servers)]
(set-client! cli)
Expand Down

0 comments on commit a60f0d1

Please sign in to comment.