4

I am using the following code to set keys in redis

(defn save-to-redis[key value]
   (let [str-value (json/generate-string value)]
      (redis/with-server {:host "127.0.0.1" :port 6379 :db 0}
         (redis/set key str-value)))) 

but when I check the value set there by running redis-cli I see that it is returning the length of str-value not str-value

This I have stripped it down to:

(defn save-to-redis[key value]
     (redis/with-server {:host "127.0.0.1" :port 6379 :db 0}
    (redis/set "foo" "bar")))

and when I use "redis-cli> get foo" it responds "3"

I am looking at the example on http://devender.wordpress.com/2010/06/13/redis-and-clojure/ and even copying and pasting his example fails. I try:

(defn test-redis []
 (println "testing redis")
 (redis/with-server {:host "127.0.0.1" :port 6379 :db 0}
   (do
     (redis/set "foo" "bar")
     (println (redis/get "foo")))))

and I get

testing redis
Exception: java.lang.Exception: Server error: ERR unknown command 'bar'
                  internal.clj:123 redis.internal/eval1198[fn]
                  MultiFn.java:163 clojure.lang.MultiFn.invoke
                  internal.clj:114 redis.internal/read-reply
                  internal.clj:112 redis.internal/read-reply
                      redis.clj:75 redis/get
                       core.clj:65 spendy.core/test-redis[fn]
                   internal.clj:48 redis.internal/with-server*
                       core.clj:62 spendy.core/test-redis
                       core.clj:74 spendy.core/respond-to-ajax
                     routes.clj:16 spendy.routes/fn
                       core.clj:39 compojure.core/if-route[fn]
                       core.clj:24 compojure.core/if-method[fn]
                       core.clj:98 compojure.core/routing[fn]
                     core.clj:2053 clojure.core/some
                       core.clj:98 compojure.core/routing
                   RestFn.java:140 clojure.lang.RestFn.applyTo
                      core.clj:542 clojure.core/apply
                      core.clj:103 compojure.core/routes[fn]
             keyword_params.clj:21 ring.middleware.keyword-params/wrap-keyword-params[fn]
              nested_params.clj:64 ring.middleware.nested-params/wrap-nested-params[fn]
                     params.clj:76 ring.middleware.params/wrap-params[fn]
           multipart_params.clj:80 ring.middleware.multipart-params/wrap-multipart-params[fn]
                    session.clj:40 ring.middleware.session/wrap-session[fn]
                   cookies.clj:132 ring.middleware.cookies/wrap-cookies[fn]
                 middleware.clj:12 hiccup.middleware/wrap-base-url[fn]
                      Var.java:365 clojure.lang.Var.invoke
                 stacktrace.clj:15 ring.middleware.stacktrace/wrap-stacktrace-log[fn]
                 stacktrace.clj:79 ring.middleware.stacktrace/wrap-stacktrace-web[fn]
            reload_modified.clj:15 ring.middleware.reload-modified/wrap-reload-modified[fn]
                 stacktrace.clj:15 ring.middleware.stacktrace/wrap-stacktrace-log[fn]
                 stacktrace.clj:79 ring.middleware.stacktrace/wrap-stacktrace-web[fn]
                      jetty.clj:16 ring.adapter.jetty/proxy-handler[fn]
                  (Unknown Source) ring.adapter.jetty.proxy$org.mortbay.jetty.handler.AbstractHandler$0.handle
           HandlerWrapper.java:152 org.mortbay.jetty.handler.HandlerWrapper.handle
                   Server.java:326 org.mortbay.jetty.Server.handle
           HttpConnection.java:542 org.mortbay.jetty.HttpConnection.handleRequest
           HttpConnection.java:945 org.mortbay.jetty.HttpConnection$RequestHandler.content
               HttpParser.java:756 org.mortbay.jetty.HttpParser.parseNext
               HttpParser.java:218 org.mortbay.jetty.HttpParser.parseAvailable
           HttpConnection.java:404 org.mortbay.jetty.HttpConnection.handle
          SocketConnector.java:228 org.mortbay.jetty.bio.SocketConnector$Connection.run
         QueuedThreadPool.java:582 org.mortbay.thread.QueuedThreadPool$PoolThread.run

I am at my wit's end. If it helps here is my ns form:

(ns spendy.core
  (:use [hiccup core page-helpers])
  (:require redis
        [clj-json.core :as json]))
Benjie Holson
  • 175
  • 1
  • 6
  • by the way, I tried several other values to put in the database and it always put the length of them. Even unquoted numbers, so (redis/set "foo" 4459) would result is "foo" being set to "4" in the database. – Benjie Holson Oct 15 '11 at 01:00
  • 1
    I never got this to work, Even clojure-redis's selftest failed (using none of my code) Switched to using Jedis, which was straight forward with this blogpost as an example: https://gist.github.com/997393/a31516b339f1f63cd532edd7f2e0851d1be28dd6 – Benjie Holson Oct 19 '11 at 00:14

2 Answers2

1

Which version of redis-clojure are you using? It works fine with this: https://github.com/tavisrudd/redis-clojure

Tavis Rudd
  • 1,156
  • 6
  • 12
1

Try this (from mmcgrana and technomancy--two Clojure gurus). It just wraps Jedis--the java redis client:

https://github.com/mmcgrana/clj-redis

Don
  • 11
  • 1
  • Here's yet another http://ztellman.github.com/aleph/aleph.redis-api.html non-blocking nio style. – Tavis Rudd Oct 26 '11 at 20:29
  • and one of the few examples I could find of aleph.redis in use: https://bitbucket.org/sjl/newseasons/src/e3e497ef8f62/src/newseasons/models/users.clj – Tavis Rudd Oct 26 '11 at 20:30