use new sockets lib

This commit is contained in:
2026-04-11 11:20:25 +02:00
parent 692fe51728
commit fafa2b40d2
5 changed files with 82 additions and 18 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ A Redis client library for Carp, supporting Redis 7.x.
## Installation ## Installation
```clojure ```clojure
(load "https://git.veitheller.de/carpentry/redis.git@0.1.0") (load "https://git.veitheller.de/carpentry/redis.git@0.2.0")
``` ```
## Usage ## Usage
+5 -5
View File
@@ -3103,7 +3103,7 @@ respectively), and contains thin wrappers around all Redis commands through 7.2.
instantiate instantiate
</div> </div>
<p class="sig"> <p class="sig">
(Fn [Socket] Redis) (Fn [TcpStream] Redis)
</p> </p>
<span> <span>
@@ -4988,7 +4988,7 @@ respectively), and contains thin wrappers around all Redis commands through 7.2.
instantiate instantiate
</div> </div>
<p class="sig"> <p class="sig">
(Fn [Redis, Socket] Redis) (Fn [Redis, TcpStream] Redis)
</p> </p>
<span> <span>
@@ -5008,7 +5008,7 @@ respectively), and contains thin wrappers around all Redis commands through 7.2.
instantiate instantiate
</div> </div>
<p class="sig"> <p class="sig">
(Fn [(Ref Redis a), Socket] ()) (Fn [(Ref Redis a), TcpStream] ())
</p> </p>
<span> <span>
@@ -5322,7 +5322,7 @@ respectively), and contains thin wrappers around all Redis commands through 7.2.
instantiate instantiate
</div> </div>
<p class="sig"> <p class="sig">
(Fn [(Ref Redis a)] (Ref Socket a)) (Fn [(Ref Redis a)] (Ref TcpStream a))
</p> </p>
<span> <span>
@@ -5824,7 +5824,7 @@ respectively), and contains thin wrappers around all Redis commands through 7.2.
instantiate instantiate
</div> </div>
<p class="sig"> <p class="sig">
(Fn [Redis, (Ref (Fn [Socket] Socket a) b)] Redis) (Fn [Redis, (Ref (Fn [TcpStream] TcpStream a) b)] Redis)
</p> </p>
<span> <span>
+60
View File
@@ -0,0 +1,60 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<link rel="stylesheet" href="../style.css">
</head>
<body>
<div class="content">
<a href="https://git.veitheller.de/carpentry/redis">
<div class="logo">
<img src="" alt="Logo">
<div class="index">
<ul>
<li>
<a href="RESP.html">
RESP
</a>
</li>
<li>
<a href="Redis.html">
Redis
</a>
</li>
</ul>
</div>
</div>
<div>
<h1>
redis
</h1>
<p>is a Redis client library for Carp, supporting Redis 7.x.</p>
<h2>Installation</h2>
<pre><code>(load &quot;https://git.veitheller.de/carpentry/redis.git@0.2.0&quot;)
</code></pre>
<h2>Example</h2>
<pre><code>(defn main []
(match (Redis.open &quot;127.0.0.1&quot;)
(Result.Success r)
(do
(println* &amp;(Redis.set &amp;r @&quot;key&quot; @&quot;value&quot;))
(println* &amp;(Redis.get &amp;r @&quot;key&quot;))
(println* &amp;(Redis.lrange &amp;r @&quot;mylist&quot; @&quot;0&quot; @&quot;-1&quot;))
(match (Redis.get &amp;r @&quot;key&quot;)
(Result.Success resp)
(match resp
(RESP.Str s) (println* &quot;got: &quot; &amp;s)
(RESP.Null) (println* &quot;not found&quot;)
_ (println* &quot;unexpected type&quot;))
(Result.Error e) (println* &quot;error: &quot; &amp;e))
(Redis.close r))
(Result.Error err) (IO.errorln &amp;err)))
</code></pre>
</div>
</a>
</div>
</body>
</html>
+1 -1
View File
@@ -10,7 +10,7 @@
## Installation ## Installation
``` ```
(load \"https://git.veitheller.de/carpentry/redis.git@0.1.0\") (load \"https://git.veitheller.de/carpentry/redis.git@0.2.0\")
``` ```
## Example ## Example
+15 -11
View File
@@ -1,4 +1,4 @@
(load "git@github.com:carpentry-org/sockets@0.0.2") (load "git@github.com:carpentry-org/socket@0.1.1")
(deftype RESP (deftype RESP
(Null []) (Null [])
@@ -117,18 +117,17 @@
) )
(deftype Redis [ (deftype Redis [
sock Socket sock TcpStream
]) ])
(defmodule Redis (defmodule Redis
(use-all Array Result Socket) (use-all Array Result)
(doc open-on "opens the connection to Redis on port `port`.") (doc open-on "opens the connection to Redis on port `port`.")
(defn open-on [host port] (defn open-on [host port]
(let [s (setup-client host port)] (match (TcpStream.connect host port)
(if (valid? &s) (Result.Success s) (Success (init s))
(Success (init s)) (Result.Error e) (Error (fmt "Couldnt connect to %s:%d: %s" host port &e))))
(Error (fmt "Couldnt connect to %s:%d" host port)))))
(doc open "opens the connection to Redis on port 6379. (doc open "opens the connection to Redis on port 6379.
@@ -136,14 +135,19 @@ For variable port numbers please check out [`open-on`](#open-on).")
(defn open [host] (open-on host 6379)) (defn open [host] (open-on host 6379))
(doc read "reads a `RESP` object from Redis.") (doc read "reads a `RESP` object from Redis.")
(defn read [r] (RESP.from-string &(Socket.read (sock r)))) (defn read [r]
(match (the (Result String String) (TcpStream.read (sock r)))
(Result.Success s) (RESP.from-string &s)
(Result.Error e) (Error e)))
(doc send "sends the command `cmd` with the arguments `args` to Redis.") (doc send "sends the command `cmd` with the arguments `args` to Redis.")
(defn send [r cmd args] (defn send [r cmd args]
(let [cmd-parts (copy-map &(fn [x] (Box.init (to-redis @x))) &(Pattern.split #" " &cmd))] (let [cmd-parts (copy-map &(fn [x] (Box.init (to-redis @x))) &(Pattern.split #" " &cmd))
(Socket.send (sock r) &(str &(RESP.Arr (concat &[cmd-parts (copy-map &(fn [x] (Box.init @x)) args)])))))) msg (str &(RESP.Arr (concat &[cmd-parts (copy-map &(fn [x] (Box.init @x)) args)])))]
(ignore (TcpStream.send (sock r) &msg))))
(doc close "closes the connection to Redis.") (doc close "closes the connection to Redis.")
(defn close [r] (Socket.close @(sock &r))) (defn close [r] (TcpStream.close @(sock &r)))
) )
(defndynamic rtreat- [s] (defndynamic rtreat- [s]