【问题标题】:Problem using redis-clojure with LeiningenRedis-clojure 与 Leiningen 一起使用时出现问题
【发布时间】:2011-05-04 00:20:30
【问题描述】:

嘿,我是 Clojure 和 Leiningen 的新手,有点卡住了。我已经设法与 Leiningen 建立了一个项目。我可以将它编译成一个 uberjar 并运行 repl。我还设法加载了一个名为 aleph 的依赖项来运行一个简单的并发网络服务器。

对我来说下一步是使用redis-clojure 访问redis。但在这里我被困住了。这是我的project.clj

(defproject alpha "0.0.1-SNAPSHOT"
  :description "Just an alpha test script"
  :main alpha.core
  :dependencies [[org.clojure/clojure "1.2.0"]
                 [org.clojure/clojure-contrib "1.2.0"]
                 [aleph, "0.1.2-SNAPSHOT"]
                 [redis-clojure "1.2.4"]])

这是我的core.clj:请注意,我只是根据redis-clojure 中的示例添加了(:requre redis) 行。

(ns alpha.core
  (:require redis)
  (:gen-class))

(use `aleph.core 'aleph.http)

(defn alpha [channel request]
  (let [] (enqueue-and-close channel
                     {:status 200
                      :header {"Content-Type" "text/html"}
                      :body "Hello Clojure World!"}))
          (println (str request)))

(defn -main [& args]
  (start-http-server alpha {:port 9292}))

当我尝试运行 lein repl 时,会发生这种情况:

java.io.FileNotFoundException: Could not locate redis__init.class or redis.clj on classpath:  (core.clj:1)

是的,我已经运行了lein deps,并且我的lib 目录中提供了redis-clojure jar。我可能遗漏了一些微不足道的东西,但我已经在这个问题上待了几个小时,而且还没有接近解决方案。谢谢!

【问题讨论】:

    标签: clojure redis leiningen


    【解决方案1】:

    命名空间 redis 不存在。我想你需要

    (:require [redis.core :as redis])
    

    一种检查可用命名空间的方法:

    (use 'clojure.contrib.find-namespaces)
    (filter #(.startsWith (str %) "redis") (find-namespaces-on-classpath))
    

    【讨论】:

    • 就是这个。你是怎么想出来的?我的意思是,我怎么会发现自己?
    • 你也可以查看jar文件。如果您使用 Emacs,请像打开普通文件一样打开 jar 并查看其内容。否则在命令行上使用“jar -tf lib/redis-clojure-1.2.4.jar”。
    【解决方案2】:

    这适用于最新版本的 Clojure,在此示例中,它会查找包含子字符串“jdbc”的所有命名空间的名称:

    (map str
      (filter
        #(> (.indexOf (str %) "jdbc") -1)
        (all-ns)))
    

    结果是一个序列,例如:

    => ("clojure.java.jdbc")

    【讨论】:

      猜你喜欢
      • 2012-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-15
      • 2011-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多