| 1 |
|
| 2 |
# guile-redis |
| 3 |
|
| 4 |
guile-redis is a Guile module for the [Redis](https://redis.io) key-value data |
| 5 |
store. It provides all commands up to Redis 7.0 and supports multiple commands, |
| 6 |
pipelining and Pub/Sub. |
| 7 |
|
| 8 |
# Installation |
| 9 |
|
| 10 |
Download the latest tarball and untar it: |
| 11 |
|
| 12 |
- [guile-redis-2.2.0.tar.gz](https://download.savannah.gnu.org/releases/guile-redis/guile-redis-2.2.0.tar.gz) |
| 13 |
|
| 14 |
If you are cloning the repository make sure you run this first: |
| 15 |
|
| 16 |
$ autoreconf -vif |
| 17 |
|
| 18 |
Then, run the typical sequence: |
| 19 |
|
| 20 |
$ ./configure --prefix=<guile-prefix> |
| 21 |
$ make |
| 22 |
$ sudo make install |
| 23 |
|
| 24 |
Where `<guile-prefix>` should preferably be the same as your system Guile |
| 25 |
installation directory (e.g. /usr). |
| 26 |
|
| 27 |
If everything installed successfully you should be up and running: |
| 28 |
|
| 29 |
$ guile |
| 30 |
scheme@(guile-user)> (use-modules (redis)) |
| 31 |
scheme@(guile-user)> |
| 32 |
|
| 33 |
It might be that you installed guile-redis somewhere differently than |
| 34 |
your system's Guile. If so, you need to indicate Guile where to find |
| 35 |
guile-redis, for example: |
| 36 |
|
| 37 |
$ GUILE_LOAD_PATH=/usr/local/share/guile/site guile |
| 38 |
|
| 39 |
# Usage |
| 40 |
|
| 41 |
## Procedures |
| 42 |
|
| 43 |
The main interface to the Redis server is really simply. It consists |
| 44 |
on three procedures, the most important one being /redis-send/ which |
| 45 |
basically sends commands to the server. |
| 46 |
|
| 47 |
- (**redis-connect** #:key host port) : Establish a connection to the redis |
| 48 |
server at the given *host* and *port* and return a Redis connection. |
| 49 |
|
| 50 |
- *host* : it defaults to 127.0.0.1. |
| 51 |
- *port* : it defaults to 6379. |
| 52 |
|
| 53 |
- (**redis-close** connection) : Close the *connection* to the Redis server. |
| 54 |
|
| 55 |
- (**redis-send** connection commands) : Send one or more *commands* to the |
| 56 |
Redis *connection*. When sending multiple commands a list of all the replies |
| 57 |
is returned. |
| 58 |
|
| 59 |
**Returns** : A reply or a list of replies if multiple commands were sent. |
| 60 |
|
| 61 |
**Throws** |
| 62 |
|
| 63 |
- *redis-error* : if the Redis server returns an error. The exception has |
| 64 |
the error string as an argument. |
| 65 |
|
| 66 |
- *redis-invalid* : if the user arguments or server's answer cannot be |
| 67 |
parsed (hopefully, this is unlikely to happen). |
| 68 |
|
| 69 |
|
| 70 |
## Redis commands |
| 71 |
|
| 72 |
All commands in guile-redis are defined with lower-case and hyphenated in the |
| 73 |
case of commands that have two or more words. For example, the command |
| 74 |
"*CLIENT LIST*" is defined as *client-list*. |
| 75 |
|
| 76 |
The commands take exaclty the same arguments as defined in the Redis manual |
| 77 |
and all the arguments (if any) need to be passed as a single list. For |
| 78 |
example: |
| 79 |
|
| 80 |
(bitfield '(mykey INCRBY i5 100 1 GET u4 0)) |
| 81 |
|
| 82 |
Note that, internally, guile-redis will automatically convert symbols and |
| 83 |
numbers to strings before sending the command to Redis. |
| 84 |
|
| 85 |
|
| 86 |
## Redis Pub/Sub |
| 87 |
|
| 88 |
guile-redis >= 2.0.0 adds proper support for [Redis |
| 89 |
Pub/Sub](https://redis.io/topics/pubsub). The Pub/Sub commands don't follow |
| 90 |
the approach of the rest of commands (except the *PUBSUB* command), instead |
| 91 |
there's a procedure for each of them: |
| 92 |
|
| 93 |
- (**redis-publish** connection channel message) : Publish the given *message* |
| 94 |
to *channel* using the provided Redis client *connection*. All subscribers |
| 95 |
to that channel will receive the message. |
| 96 |
|
| 97 |
**Returns** : number of clients that received the message. |
| 98 |
|
| 99 |
- (**redis-subscribe** connection channels) : Subscribe to the given list of |
| 100 |
*channels* using the Redis client *connection*. |
| 101 |
|
| 102 |
**Returns** : a list of pairs where the first value is the subscribed |
| 103 |
channel and the second value is the total number of subscribed channels |
| 104 |
(when that channel was subscribed). |
| 105 |
|
| 106 |
- (**redis-unsubscribe** connection [channels]) : Unsubscribe from the given |
| 107 |
optional list of *channels* using the Redis client *connection*. If |
| 108 |
*channels* is not specified it will unsubscribe from all the subscribed |
| 109 |
channels. |
| 110 |
|
| 111 |
**Returns** : a list of pairs where the first value is the unsubscribed |
| 112 |
channel and the second value is the total number of remaining subscribed |
| 113 |
channels (when that channel was unsubscribed). |
| 114 |
|
| 115 |
- (**redis-psubscribe** connection patterns) : Subscribe to the given list of |
| 116 |
channel *patterns* using the Redis client *connection*. |
| 117 |
|
| 118 |
**Returns** : a list of pairs where the first value is the subscribed |
| 119 |
pattern and the second value is the total number of subscribed channel |
| 120 |
patterns (when that pattern was subscribed). |
| 121 |
|
| 122 |
- (**redis-punsubscribe** connection [patterns]) : Unsubscribe from the given |
| 123 |
optional list of channel *patterns* using the Redis client *connection*. If |
| 124 |
*patterns* is not specified it will unsubscribe from all the subscribed |
| 125 |
patterns. |
| 126 |
|
| 127 |
**Returns** : a list of pairs where the first value is the unsubscribed |
| 128 |
pattern and the second value is the total number of remaining subscribed |
| 129 |
channel patterns (when that patterns was unsubscribed). |
| 130 |
|
| 131 |
- (**redis-subscribe-read** connection) : Read the next message from one of |
| 132 |
the subscribed channels on the given *connection*. This is a blocking |
| 133 |
operation until a message is received. |
| 134 |
|
| 135 |
**Returns**: a couple of values, the channel where the message was received |
| 136 |
and the actual message. |
| 137 |
|
| 138 |
# Examples |
| 139 |
|
| 140 |
- Load the module: |
| 141 |
|
| 142 |
``` |
| 143 |
> (use-modules (redis)) |
| 144 |
``` |
| 145 |
|
| 146 |
- Create a connection: |
| 147 |
|
| 148 |
``` |
| 149 |
> (define conn (redis-connect)) |
| 150 |
``` |
| 151 |
|
| 152 |
- Send a single *PING* command: |
| 153 |
|
| 154 |
``` |
| 155 |
> (redis-send conn (ping)) |
| 156 |
"PONG" |
| 157 |
``` |
| 158 |
|
| 159 |
- Send a couple of *PING* commands: |
| 160 |
|
| 161 |
``` |
| 162 |
> (redis-send conn (list (ping) (ping '("hello from guile-redis")))) |
| 163 |
("PONG" "hello from guile-redis") |
| 164 |
``` |
| 165 |
|
| 166 |
- Set a couple of keys: |
| 167 |
|
| 168 |
``` |
| 169 |
> (redis-send conn (mset '(hello "world" foo "bar"))) |
| 170 |
"OK" |
| 171 |
``` |
| 172 |
|
| 173 |
- Retrieve the keys just set above: |
| 174 |
|
| 175 |
``` |
| 176 |
> (redis-send conn (mget '(hello foo))) |
| 177 |
("world" "bar") |
| 178 |
``` |
| 179 |
|
| 180 |
- Subscribe to the *news* channel: |
| 181 |
|
| 182 |
``` |
| 183 |
> (redis-subscribe conn '("news")) |
| 184 |
(("news" . 1)) |
| 185 |
``` |
| 186 |
|
| 187 |
- Publish message to the *news* channel (from another REPL): |
| 188 |
|
| 189 |
``` |
| 190 |
> (redis-publish conn "news" "hello from guile-redis") |
| 191 |
1 |
| 192 |
``` |
| 193 |
|
| 194 |
- Read next message from subscribed channel: |
| 195 |
|
| 196 |
``` |
| 197 |
> (redis-subscribe-read conn) |
| 198 |
"news" |
| 199 |
"hello from guile-redis" |
| 200 |
``` |
| 201 |
|
| 202 |
- Unsubscribe from all channels: |
| 203 |
|
| 204 |
``` |
| 205 |
> (redis-unsubscribe conn) |
| 206 |
(("news" . 0)) |
| 207 |
``` |
| 208 |
|
| 209 |
- Finally, close the connection: |
| 210 |
|
| 211 |
``` |
| 212 |
> (redis-close conn) |
| 213 |
``` |
| 214 |
|
| 215 |
# License |
| 216 |
|
| 217 |
Copyright (C) 2013-2022 Aleix Conchillo Flaque <[email protected]> |
| 218 |
|
| 219 |
This file is part of guile-redis. |
| 220 |
|
| 221 |
guile-redis is free software: you can redistribute it and/or modify it |
| 222 |
under the terms of the GNU General Public License as published by the |
| 223 |
Free Software Foundation; either version 3 of the License, or (at your |
| 224 |
option) any later version. |
| 225 |
|
| 226 |
guile-redis is distributed in the hope that it will be useful, but |
| 227 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 228 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 229 |
General Public License for more details. |
| 230 |
|
| 231 |
You should have received a copy of the GNU General Public License |
| 232 |
along with guile-redis. If not, see https://www.gnu.org/licenses/. |