clojure redis set是设置value的字符串长度而不是value
我正在使用以下代码在 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))))
,但是当我通过运行 redis-cli 检查在那里设置的值时,我发现它返回的是 str-value 的长度而不是 str-value
已将其精简为:
(defn save-to-redis[key value]
(redis/with-server {:host "127.0.0.1" :port 6379 :db 0}
(redis/set "foo" "bar")))
我 我使用“redis-cli> get foo”它响应“3”
我正在查看 http://devender.wordpress.com/2010/06/13/redis-and-clojure/ 甚至复制和粘贴他的示例也会失败。我尝试了一下:
(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")))))
但我发现
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
我已经无计可施了。如果有帮助的话这是我的 ns 表格:
(ns spendy.core
(:use [hiccup core page-helpers])
(:require redis
[clj-json.core :as json]))
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]))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用的是哪个版本的 redis-clojure?它可以正常工作: https://github.com/tavisrudd/redis-clojure
Which version of redis-clojure are you using? It works fine with this: https://github.com/tavisrudd/redis-clojure
试试这个(来自 mmcgrana 和 technomancy——两位 Clojure 大师)。它只是包装 Jedis——java redis 客户端:
https://github.com/mmcgrana/clj-redis< /a>
Try this (from mmcgrana and technomancy--two Clojure gurus). It just wraps Jedis--the java redis client:
https://github.com/mmcgrana/clj-redis