From fafa2b40d24da19a20e00a1413e1cb4946945638 Mon Sep 17 00:00:00 2001 From: Veit Heller Date: Sat, 11 Apr 2026 11:20:25 +0200 Subject: [PATCH] use new sockets lib --- README.md | 2 +- docs/Redis.html | 10 ++++----- docs/index.html | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ gendocs.carp | 2 +- redis.carp | 26 ++++++++++++--------- 5 files changed, 82 insertions(+), 18 deletions(-) create mode 100644 docs/index.html diff --git a/README.md b/README.md index 1a2fbc4..7e3929e 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ A Redis client library for Carp, supporting Redis 7.x. ## Installation ```clojure -(load "https://git.veitheller.de/carpentry/redis.git@0.1.0") +(load "https://git.veitheller.de/carpentry/redis.git@0.2.0") ``` ## Usage diff --git a/docs/Redis.html b/docs/Redis.html index a081668..a97d5fe 100644 --- a/docs/Redis.html +++ b/docs/Redis.html @@ -3103,7 +3103,7 @@ respectively), and contains thin wrappers around all Redis commands through 7.2. instantiate

- (Fn [Socket] Redis) + (Fn [TcpStream] Redis)

@@ -4988,7 +4988,7 @@ respectively), and contains thin wrappers around all Redis commands through 7.2. instantiate

- (Fn [Redis, Socket] Redis) + (Fn [Redis, TcpStream] Redis)

@@ -5008,7 +5008,7 @@ respectively), and contains thin wrappers around all Redis commands through 7.2. instantiate

- (Fn [(Ref Redis a), Socket] ()) + (Fn [(Ref Redis a), TcpStream] ())

@@ -5322,7 +5322,7 @@ respectively), and contains thin wrappers around all Redis commands through 7.2. instantiate

- (Fn [(Ref Redis a)] (Ref Socket a)) + (Fn [(Ref Redis a)] (Ref TcpStream a))

@@ -5824,7 +5824,7 @@ respectively), and contains thin wrappers around all Redis commands through 7.2. instantiate

- (Fn [Redis, (Ref (Fn [Socket] Socket a) b)] Redis) + (Fn [Redis, (Ref (Fn [TcpStream] TcpStream a) b)] Redis)

diff --git a/docs/index.html b/docs/index.html new file mode 100644 index 0000000..2fcfd4d --- /dev/null +++ b/docs/index.html @@ -0,0 +1,60 @@ + + + + + + + + + +
+ + +
+

+ redis +

+

is a Redis client library for Carp, supporting Redis 7.x.

+

Installation

+
(load "https://git.veitheller.de/carpentry/redis.git@0.2.0")
+
+

Example

+
(defn main []
+  (match (Redis.open "127.0.0.1")
+    (Result.Success r)
+      (do
+        (println* &(Redis.set &r @"key" @"value"))
+        (println* &(Redis.get &r @"key"))
+        (println* &(Redis.lrange &r @"mylist" @"0" @"-1"))
+        (match (Redis.get &r @"key")
+          (Result.Success resp)
+            (match resp
+              (RESP.Str s) (println* "got: " &s)
+              (RESP.Null) (println* "not found")
+              _ (println* "unexpected type"))
+          (Result.Error e) (println* "error: " &e))
+        (Redis.close r))
+    (Result.Error err) (IO.errorln &err)))
+
+ +
+ +
+ + diff --git a/gendocs.carp b/gendocs.carp index ab199ee..ba618f4 100644 --- a/gendocs.carp +++ b/gendocs.carp @@ -10,7 +10,7 @@ ## Installation ``` -(load \"https://git.veitheller.de/carpentry/redis.git@0.1.0\") +(load \"https://git.veitheller.de/carpentry/redis.git@0.2.0\") ``` ## Example diff --git a/redis.carp b/redis.carp index 862e599..9948226 100644 --- a/redis.carp +++ b/redis.carp @@ -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 (Null []) @@ -117,18 +117,17 @@ ) (deftype Redis [ - sock Socket + sock TcpStream ]) (defmodule Redis - (use-all Array Result Socket) + (use-all Array Result) (doc open-on "opens the connection to Redis on port `port`.") (defn open-on [host port] - (let [s (setup-client host port)] - (if (valid? &s) - (Success (init s)) - (Error (fmt "Couldn’t connect to %s:%d" host port))))) + (match (TcpStream.connect host port) + (Result.Success s) (Success (init s)) + (Result.Error e) (Error (fmt "Couldn’t connect to %s:%d: %s" host port &e)))) (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)) (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.") (defn send [r cmd args] - (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)])))))) + (let [cmd-parts (copy-map &(fn [x] (Box.init (to-redis @x))) &(Pattern.split #" " &cmd)) + 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.") - (defn close [r] (Socket.close @(sock &r))) + (defn close [r] (TcpStream.close @(sock &r))) ) (defndynamic rtreat- [s]