Execute a redis Command on Connnection. Next, bind Reply to the
returned result. Command is a callable term whose functor is the
name of the Redis command and whose arguments are translated to
Redis arguments according to the rules below. Note that all text is
always represented using UTF-8 encoding.
- Atomic values are emitted verbatim
- A term A:B:... where all arguments are either atoms,
strings or integers (no floats) is translated into
a string
"A:B:..."
. This is a common shorthand for
representing Redis keys.
- A term Term as prolog is emitted as "\u0000T\u0000" followed
by Term in canonical form.
- Any other term is emitted as write/1.
Reply is either a plain term (often a variable) or a term Value as
Type
. In the latter form, Type dictates how the Redis bulk
reply is translated to Prolog. The default equals to auto
, i.e.,
as a number of the content satisfies the Prolog number syntax and
as an atom otherwise.
status(Atom)
Returned if the server replies with + Status
. Atom
is the textual value of Status converted to lower case,
e.g., status(ok)
or status(pong)
.
nil
This atom is returned for a NIL/NULL value. Note that if
the reply is only nil
, redis/3 fails. The nil
value
may be embedded inside lists or maps.
- A number
Returned if the server replies an integer (":Int"), double
(",Num") or big integer ("(Num")
- A string
Returned on a bulk reply. Bulk replies are supposed to be
in UTF-8 encoding. The the bulk reply starts with
"\u0000T\u0000" it is supposed to be a Prolog term.
Note that this intepretation means it is not possible
to read arbitrary binary blobs.
- A list of replies. A list may also contain
nil
. If Reply
as a whole would be nil
the call fails.
- A list of pairs. This is returned for the redis version 3
protocol "%Map". Both the key and value respect the same
rules as above.
Redis bulk replies are translated depending on the as
Type as
explained above.
- string
- string(Encoding)
- Create a SWI-Prolog string object interpreting the blob as
following Encoding. Encoding is a restricted set of SWI-Prolog's
encodings:
bytes
(iso_latin_1
), utf8
and text
(the
current locale translation).
- atom
- atom(Encoding)
- As above, producing an atom.
- codes
- codes(Encoding)
- As above, producing a list of integers (Unicode code points)
- chars
- chars(Encoding)
- As above, producing a list of one-character atoms.
- integer
- float
- rational
- number
- Interpret the bytes as a string representing a number. If
the string does not represent a number of the requested type
a
type_error(Type, String)
is raised.
- tagged_integer
- Same as integer, but demands the value to be between the Prolog
flags
min_tagged_integer
and max_tagged_integer
, allowing
the value to be used as a dict key.
- auto
- Same as
auto(atom, number)
- auto(AsText, AsNumber)
- If the bulk string confirms the syntax of AsNumber, convert
the value to the requested numberical type. Else convert
the value to text according to AsText. This is similar to
the Prolog predicate name/2.
- dict_key
- Alias for
auto(atom,tagged_integer)
. This allows the value
to be used as a key for a SWI-Prolog dict.
- pairs(AsKey, AsValue)
- Convert a map or array of even length into pairs for which the
key satisfies AsKey and the value AsValue. The
pairs
type
can also be applied to a Redis array. In this case the array
length must be even. This notably allows fetching a Redis
hash as pairs using HGETALL
using version 2 of the
Redis protocol.
- dict(AsKey, AsValue)
- Similar to
pairs(AsKey, AsValue)
, but convert the resulting
pair list into a SWI-Prolog dict. AsKey must convert to a
valid dict key, i.e., an atom or tagged integer. See dict_key
.
- dict(AsValue)
- Shorthand for
dict(dict_key, AsValue)
.
Here are some simple examples
?- redis(default, set(a, 42), X).
X = status("OK").
?- redis(default, get(a), X).
X = "42".
?- redis(default, get(a), X as integer).
X = 42.
?- redis(default, get(a), X as float).
X = 42.0.
?- redis(default, set(swipl:version, 8)).
true.
?- redis(default, incr(swipl:version), X).
X = 9.
- Errors
- -
redis_error(Code, String)