Clojure and Redis(Using Redis in Clojure)

Artificial Intelligence Technology   Semantic Web Technology   Search Technology     DataBase Technology   Ontology Technology   Digital Transformation Technology   User Interface and DataVisualization   Workflow & Services.  Programming  Clojure    Database Technologies

As a means of making the aforementioned tightly coupled functions loosely coupled, there is a method of using STM (software transaction memory) as a means of implementing Clojure. For example, in the case of using atom

(def my-atom (atom nil))
(defn my-func []
   (do (swap! my-atom assoc state-data)
       (function col)))

or using core.async, you can use

(def c (chan 1))
(take! c
     (fn [x]
        (println "Clojure Callback value " x)))
(put! c "hello world")

You can use something like Here we introduce the case of using redis as a channel.

Redis (REmote DIctionary Server) is a networked, persistent, in-memory database and a fast kv-store that can handle data structures such as associative arrays, lists, and sets. If you want to use it on a mac, you can use home-brew and install it with the command “brew insatll redis”.

After installation, use the “redis-server” command to launch it as follows

7917:C 21 Mar 2021 06:17:55.399 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
7917:M 21 Mar 2021 06:17:55.400 * Increased maximum number of open files to 10032 (it was originally set to 256).
7917:M 21 Mar 2021 06:17:55.400 * monotonic clock: POSIX clock_gettime
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 6.2.1 (00000000/0) 64 bit
  .-`` .-```.  ```/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 7917
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

7917:M 21 Mar 2021 06:17:55.402 # Server initialized
7917:M 21 Mar 2021 06:17:55.404 * Loading RDB produced by version 6.2.1
7917:M 21 Mar 2021 06:17:55.404 * RDB age 126144 seconds
7917:M 21 Mar 2021 06:17:55.404 * RDB memory usage when created 1.00 Mb
7917:M 21 Mar 2021 06:17:55.404 * DB loaded from disk: 0.002 seconds
7917:M 21 Mar 2021 06:17:55.404 * Ready to accept connections

If we use redis-cli as a command line tool here, we get the following.

$ redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set test "HELLO, World!!"
OK
127.0.0.1:6379> get test
"HELLO, World!!"

Carmine is a Clojure wrapper for this Redis. leiningen’s dependencies are

Use [com.taoensso/carmine “3.1.0”].

is used. The sample code is as follows.

(ns redis  
  (:require [taoensso.carmine :as car :refer (wcar)]))  

(def server1-conn {:pool {} :sepc {:host "127.0.0.1" :port 6379}})  
(defmacro wcar* [& body] `(car/wcar server1-conn ~@body))

(defn setter  
  [key value]  
  (wcar* (car/set key value)))  

(defn getter  
  [key]  
  (wcar* (car/get key)))  

(defn add-list
  [key value]
  (wcar* (car/lpush key value)))

(defn all-list
  [key]
  (wcar* (car/lrange key 0 -1)))

Using these, it is easy to implement something similar to core.async’s channel. Also, pipeline processing with branching is possible by using the pub/sub function of Redis.In the next article, I will discuss redis in more detail.

コメント

タイトルとURLをコピーしました