Skip to content

Commit

Permalink
Merge pull request #29 from rwilson/async-get-and-touch
Browse files Browse the repository at this point in the history
Add async-get-and-touch, standardize get-and-touch
  • Loading branch information
michaelklishin committed Jul 20, 2016
2 parents ddd4b6a + 18a1cbf commit 9eea48b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
39 changes: 23 additions & 16 deletions src/clojure/clojurewerkz/spyglass/client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(:refer-clojure :exclude [set get flush replace])
(:import [net.spy.memcached MemcachedClient ConnectionFactory DefaultConnectionFactory
BinaryConnectionFactory KetamaConnectionFactory AddrUtil ConnectionFactoryBuilder
FailureMode ConnectionFactoryBuilder$Protocol]
FailureMode ConnectionFactoryBuilder$Protocol CASValue]
[net.spy.memcached.transcoders Transcoder SerializingTranscoder]
[clojurewerkz.spyglass OperationFuture BulkGetFuture GetFuture]
[net.spy.memcached.auth AuthDescriptor PlainCallbackHandler]))
Expand Down Expand Up @@ -56,6 +56,11 @@
(.setProtocol cfb ConnectionFactoryBuilder$Protocol/BINARY))
(.build cfb)))

(defn- cas-value->map
"Given a CASValue, transform it to a map with keys #{:cas :value}"
[^CASValue cas-value]
{:value (when cas-value (.getValue cas-value))
:cas (when cas-value (.getCas cas-value))})


;;
Expand Down Expand Up @@ -131,13 +136,6 @@
([^MemcachedClient client ^String key ^Transcoder transcoder]
(.get client key transcoder)))

(defn get-and-touch
"Get a single key and reset its expiration."
([^MemcachedClient client ^String key ^long expiration]
(.getAndTouch client key expiration))
([^MemcachedClient client ^String key ^long expiration ^Transcoder transcoder]
(.getAndTouch client key expiration transcoder)))

(defn ^clojurewerkz.spyglass.GetFuture
async-get
"Get the given key asynchronously"
Expand All @@ -146,6 +144,20 @@
([^MemcachedClient client ^String key ^Transcoder transcoder]
(GetFuture. (.asyncGet client key transcoder))))

(defn get-and-touch
"Get a single key and reset its expiration."
([^MemcachedClient client ^String key ^long expiration]
(cas-value->map (.getAndTouch client key expiration)))
([^MemcachedClient client ^String key ^long expiration ^Transcoder transcoder]
(cas-value->map (.getAndTouch client key expiration transcoder))))

(defn async-get-and-touch
"Get a single key and reset its expiration asynchronously"
([^MemcachedClient client ^String key ^long expiration]
(OperationFuture. (.asyncGetAndTouch client key expiration)))
([^MemcachedClient client ^String key ^long expiration ^Transcoder transcoder]
(OperationFuture. (.asyncGetAndTouch client key expiration transcoder))))

(defn ^clojure.lang.IPersistentMap get-multi
"Get the values for multiple keys from the cache. Returns a future that will return a mutable map of results."
([^MemcachedClient client ^java.util.Collection keys]
Expand Down Expand Up @@ -202,16 +214,11 @@
(.decr client key by default expiration)))

(defn gets
"Gets (with CAS support) a single key."
([^MemcachedClient client ^String key]
(let [response (.gets client key)]
{:value (when response
(.getValue response)) :cas (when response
(.getCas response))}))
(cas-value->map (.gets client key)))
([^MemcachedClient client ^String key transcoder]
(let [response (.gets client key transcoder)]
{:value (when response
(.getValue response)) :cas (when response
(.getCas response))})))
(cas-value->map (.gets client key transcoder))))

(defn cas
"Perform a synchronous CAS (compare-and-swap) operation."
Expand Down
24 changes: 24 additions & 0 deletions test/clojurewerkz/spyglass/test/client_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,30 @@
"kw-key" :memcached
"ratio-key" 3/8))))

;; get-and-touch is only supported in binary protocol
(deftest test-set-then-get-and-touch
(when-not ci?
(testing "with the binary protocol"
(are [k v]
(do (c/set bc k 10 v)
(is (= v (:value (c/get-and-touch bc k 10)))))
"s-key" "s-value"
"l-key" 100000
"kw-key" :memcached
"ratio-key" 3/8))))

;; async-get-and-touch is only supported in binary protocol
(deftest test-set-then-async-get-and-touch
(when-not ci?
(testing "with the binary protocol"
(are [k v]
(do (c/set bc k 10 v)
(is (= v (.getValue @(c/async-get-and-touch bc k 10)))))
"s-key" "s-value"
"l-key" 100000
"kw-key" :memcached
"ratio-key" 3/8))))

(deftest test-async-multiget
(testing "with the text protocol"
(c/set tc "key1" 20 "a-value")
Expand Down

0 comments on commit 9eea48b

Please sign in to comment.