Clojure 学习入门(9)—— 连接redis

clojure 连接 redis 示例

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

(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"]
                 ])

示例代码:

(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"))

运行结果:

PONG
OK
BAR

OK
http://blog.ithomer.net


redis 数据内容:




Carmine 示例

Carmine, a Clojure Redis client

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


示例代码:

(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)

运行结果:
rd01 run!

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



参考推荐:

Carmine, a Clojure Redis client(github)

clj-redis(github)

clojure操作redis


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值