Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelklishin committed Jun 3, 2012
0 parents commit 8b2b099
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/target
/lib
/classes
/checkouts
pom.xml
*.jar
*.class
.lein-deps-sum
.lein-failures
.lein-plugins
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: clojure
lein: lein2
script: lein2 all test
jdk:
- openjdk6
- openjdk7
- oraclejdk7
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# What is Spyglass

Spyglass is a Clojure client for Memcached built on top of [SpyMemcached](http://code.google.com/p/spymemcached/)


## This is a Work In Progress

Spyglass is *very* young and incomplete. Nothing to see here yet, move along.


## Development

Spyglass uses [Leiningen 2](https://github.com/technomancy/leiningen/blob/master/doc/TUTORIAL.md). Make sure you have it installed and then run tests against
all supported Clojure versions using

lein2 all test

Then create a branch and make your changes on it. Once you are done with your changes and all tests pass, submit
a pull request on Github.


## License

Copyright (C) 2012 Michael S. Klishin

Distributed under the Eclipse Public License, the same as Clojure.
20 changes: 20 additions & 0 deletions project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(defproject clojurewerkz/spyglass "1.0.0-SNAPSHOT"
:description "A Clojure client for Memcached implemented as a very thin layer on top of SpyMemcached"
:url "http://github.com/clojurewerkz/spyglass"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.3.0"]
[ spy/spymemcached "2.8.1"]]
:repositories {"spy-memcached" {:url "http://files.couchbase.com/maven2/"
:checksum :ignore}
"sonatype" {:url "http://oss.sonatype.org/content/repositories/releases"
:snapshots false
:releases {:checksum :fail :update :always}}
"sonatype-snapshots" {:url "http://oss.sonatype.org/content/repositories/snapshots"
:snapshots true
:releases {:checksum :fail :update :always}}}
:source-paths ["src/clojure"]
:warn-on-reflection true
:profiles {:1.4 {:dependencies [[org.clojure/clojure "1.4.0"]]}
:1.5 {:dependencies [[org.clojure/clojure "1.5.0-master-SNAPSHOT"]]}}
:aliases {"all" ["with-profile" "dev:dev,1.4:dev,1.5"]})
36 changes: 36 additions & 0 deletions src/clojure/clojurewerkz/spyglass/client.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
(ns clojurewerkz.spyglass.client
(:refer-clojure :exclude [set get])
(:import [net.spy.memcached MemcachedClient DefaultConnectionFactory BinaryConnectionFactory AddrUtil]))




(defn- servers
[^String server-list]
(AddrUtil/getAddresses server-list))

(defn text-connection
"Returns a new text protocol client that will use the provided list of servers"
[^String server-list]
(MemcachedClient. (DefaultConnectionFactory.) (servers server-list)))

(defn bin-connection
"Returns a new binary protocol client that will use the provided list of servers"
[^String server-list]
(MemcachedClient. (BinaryConnectionFactory.) (servers server-list)))


(defn set
"Set an object in the cache (using the default transcoder) regardless of any existing value"
[^MemcachedClient client ^String key ^long expiration value]
(.set client (name key) expiration value))

(defn get
"Get with a single key and decode using the default transcoder"
[^MemcachedClient client ^String key]
(.get client (name key)))

(defn touch
"Touch the given key to reset its expiration time"
[^MemcachedClient client ^String key ^long expiration]
(.touch client (name key) expiration))
50 changes: 50 additions & 0 deletions test/clojurewerkz/spyglass/test/client_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
(ns clojurewerkz.spyglass.test.client-test
(:require [clojurewerkz.spyglass.client :as c])
(:use clojure.test))


(def tc (c/text-connection "localhost:11211"))
(def bc (c/bin-connection "localhost:11211"))

(deftest test-set-then-get
(testing "with text protocol"
(are [k v]
(do (c/set tc k 10 v)
(is (= v (c/get tc k))))
"s-key" "s-value"
"l-key" 100000
"kw-key" :memcached
:sym 'symbol
"ratio-key" 3/8))
(testing "with binary protocol"
(are [k v]
(do (c/set bc k 10 v)
(is (= v (c/get bc k))))
"s-key" "s-value"
"l-key" 100000
"kw-key" :memcached
:sym 'symbol
"ratio-key" 3/8)))


(deftest test-set-then-touch
(testing "with text protocol"
(are [k v]
(do (c/set tc k 10 v)
;; touch is not supported by the text protocol
(is (thrown? UnsupportedOperationException
(.get (c/touch tc k 4)))))
"s-key" "s-value"
"l-key" 100000
"kw-key" :memcached
:sym 'symbol
"ratio-key" 3/8))
(testing "with binary protocol"
(are [k v]
(do (c/set bc k 10 v)
(is (.get (c/touch bc k 4))))
"s-key" "s-value"
"l-key" 100000
"kw-key" :memcached
:sym 'symbol
"ratio-key" 3/8)))

0 comments on commit 8b2b099

Please sign in to comment.