Clojure 学习 —— 连接redis

clojure 连接redis简单介绍及示例

clojure 连接 redis 示例

project.cli 添加redis依赖: [clj-redis "0.0.12"]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

(defproject myClojure "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}

:dependencies [
[org.clojure/clojure "1.5.1"]
[org.clojure/java.jdbc "0.0.6"]
[org.clojure/clojure-contrib "1.2.0"]
[mysql/mysql-connector-java "5.1.26"]

[com.novemberain/monger "1.5.0"]

[com.taoensso/carmine "1.2.1"]
[clj-redis "0.0.12"]
])

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

(ns myClojure.redis2
(require [clj-redis.client :as redis]))


;; 连接redis
(def db (redis/init))

(println (redis/ping db))

(println (redis/set db "foo" "BAR"))
(println (redis/get db "foo"))

(println) ;; 空一行

(println (redis/set db "blog" "http://blog.ithomer.net"))
(println (redis/get db "blog"))

运行结果:

1
2
3
4
5
6
PONG
OK
BAR

OK
http://blog.ithomer.net

redis 数据内容:

clojure_redis.png

Carmine 示例

Carmine, a Clojure Redis client

project.clj 添加依赖: [com.taoensso/carmine "2.4.0"]

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

(ns myClojure.redis
(:require [taoensso.carmine :as car]
[taoensso.carmine.message-queue :as carmine-mq])

(:gen-class :main true))


(def pool (car/make-conn-pool)) ; See docstring for additional options
(def spec-server1 (car/make-conn-spec)) ; ''

(defmacro wcar [& body] `(car/with-conn pool spec-server1 ~@body))

(defn -main [& args]
(println "rd01 run!")
;;
(wcar (car/ping)
(car/set "foo" "hello ithomer")
(car/get "foo"))

)


(-main)

运行结果:

1
2
3
rd01 run!

["PONG" "OK" "hello ithomer"]

参考推荐:
Carmine, a Clojure Redis client(github)
clj-redis(github)
clojure操作redis
Clojure 学习入门(9)—— 连接redis

文章目录
  1. 1. clojure 连接 redis 示例
  2. 2. Carmine, a Clojure Redis client