RedisX v0.9
A simple, light-weight Redis database client
Loading...
Searching...
No Matches
redisx-client.c File Reference

Macros

#define REDIS_SIMPLE_STRING_SIZE   256
 (bytes) Only store up to this many characters from Redis confirms and errors.
 

Functions

int redisxAbortBlockAsync (RedisClient *cl)
 
int redisxClearAttributesAsync (RedisClient *cl)
 
RESPredisxExecBlockAsync (RedisClient *cl, int *pStatus)
 
const RESPredisxGetAttributesAsync (const RedisClient *cl)
 
RedisClientredisxGetClient (Redis *redis, enum redisx_channel channel)
 
RedisClientredisxGetLockedConnectedClient (Redis *redis, enum redisx_channel channel)
 
int redisxIgnoreReplyAsync (RedisClient *cl)
 
int redisxLockClient (RedisClient *cl)
 
int redisxLockConnected (RedisClient *cl)
 
RESPredisxReadReplyAsync (RedisClient *cl, int *pStatus)
 
int redisxResetClient (RedisClient *cl)
 
int redisxSendArrayRequestAsync (RedisClient *cl, const char **args, const int *lengths, int n)
 
int redisxSendRequestAsync (RedisClient *cl, const char *command, const char *arg1, const char *arg2, const char *arg3)
 
int redisxSkipReplyAsync (RedisClient *cl)
 
int redisxStartBlockAsync (RedisClient *cl)
 
int redisxUnlockClient (RedisClient *cl)
 

Detailed Description

Date
Created on Aug 26, 2024
Author
Attila Kovacs

Basic I/O (send/receive) functions for the RedisX library.

Function Documentation

◆ redisxAbortBlockAsync()

int redisxAbortBlockAsync ( RedisClient cl)

Abort an atomic transaction block. It sends DISCARD.

Parameters
clPointer to a Redis client
Returns
X_SUCCESS (0) if successful, or X_NULL if the client is NULL, or X_NO_SERVICE if not connected ot the client or if send() failed, or X_NO_INIT if the client was not initialized.
See also
redisxStartBlockAsync()

References redisxIgnoreReplyAsync(), TRUE, x_error(), X_NO_INIT, and X_SUCCESS.

◆ redisxClearAttributesAsync()

int redisxClearAttributesAsync ( RedisClient cl)

Clears the attributes for the specified client. The caller should have an exclusive lock on the client's mutex prior to making this call.

Typically a user migh call this function prior to calling redisxReadReplyAsync() on the same client, to ensure that any attributes that are available after the read will be the ones that were sent with the last response from the server.

Parameters
clThe Redis client instance
Returns
X_SUCCESS (0) if successful, or else X_NULL if the client is NULL.
See also
redisxGetAttributesAsync()
redisxReadReplyAsync()
redisxLockClient()

References X_SUCCESS.

◆ redisxExecBlockAsync()

RESP * redisxExecBlockAsync ( RedisClient cl,
int *  pStatus 
)

Finish and execute an atomic transaction block. It sends EXEC, skips through all OK and QUEUED acknowledgements, and returns the reply to the transaction block itself.

Parameters
clPointer to a Redis client
pStatusPointer to int in which to return error status. or NULL if not required.
Returns
The array RESP returned by EXEC, or NULL if there was an error.
See also
redisxStartBlockAsync()
redisxAbortBlockAsync()

References redisxDestroyRESP(), redisxReadReplyAsync(), redisxSkipReplyAsync(), RESP_ARRAY, RESP_ERROR, TRUE, RESP::type, x_error(), X_SUCCESS, and x_trace_null().

◆ redisxGetAttributesAsync()

const RESP * redisxGetAttributesAsync ( const RedisClient cl)

Returns the attributes (if any) that were last sent along a response to the client. This function should be called only if the caller has an exclusive lock on the client's mutex. Also, there are a few rules the caller should follow:

  • The caller should not block the client for long and return quickly. If it has blocking calls, or requires extensive processing, it should make a copy of the RESP first, and release the lock immediately after.
  • The caller must not attempt to call free() on the returned RESP

Normally the user would typically call this function right after a redisxReadReplyAsync() call, for which atributes are expected. The caller might also want to call redisxClearAttributeAsync() before attempting to read the response to ensure that the attributes returned are for the same reply from the server.

Parameters
clThe Redis client instance
Returns
The attributes last received (possibly NULL).
See also
redisxGetAttributes()
redisxClearAttributesAsync()
redisxReadReplyAsync()
redisxLockClient()

References x_error().

◆ redisxGetClient()

RedisClient * redisxGetClient ( Redis redis,
enum redisx_channel  channel 
)

Returns the redis client for a given connection type in a Redis instance.

Parameters
redisPointer to a Redis instance.
channelREDISX_INTERACTIVE_CHANNEL, REDISX_PIPELINE_CHANNEL, or REDISX_SUBSCRIPTION_CHANNEL
Returns
Pointer to the matching Redis client, or NULL if redis is null (EINVAL) or not initialized (EAGAIN) or if the channel argument is invalid (ECHRNG).
See also
redisxGetLockedConnectedClient()

References REDISX_CHANNELS, redisxCheckValid(), x_error(), X_SUCCESS, and x_trace_null().

◆ redisxGetLockedConnectedClient()

RedisClient * redisxGetLockedConnectedClient ( Redis redis,
enum redisx_channel  channel 
)

Returns the redis client for a given connection type in a Redis instance, with the exclusive access lock if the client is valid and is connected, or else NULL. It is effectively the combination of redisxGetClient() followed by redisxLockConnected().

Parameters
redisPointer to a Redis instance.
channelREDISX_INTERACTIVE_CHANNEL, REDISX_PIPELINE_CHANNEL, or REDISX_SUBSCRIPTION_CHANNEL
Returns
The locked client, if it is enabled, or NULL if the redis argument is NULL, the channel is invalid, or the requested client is not currently connected.
See also
redisxGetClient()
redisxUnlockClient()
redisxLockConnected()

References redisxGetClient(), redisxLockConnected(), X_SUCCESS, and x_trace_null().

◆ redisxIgnoreReplyAsync()

int redisxIgnoreReplyAsync ( RedisClient cl)

Silently consumes a reply from the specified Redis channel.

Parameters
clPointer to a Redis channel.
Returns
X_SUCCESS if a response was successfully consumed, or REDIS_NULL if a valid response could not be obtained.

References REDIS_NULL, redisxDestroyRESP(), redisxReadReplyAsync(), X_SUCCESS, and x_trace().

◆ redisxLockClient()

int redisxLockClient ( RedisClient cl)

Get exclusive write access to the specified Redis channel.

Parameters
clPointer to the Redis client instance.
Returns
X_SUCCESS if the exclusive lock for the channel was successfully obtained, or X_FAILURE if pthread_mutex_lock() returned an error, or X_NULL if the client is NULL, or X_NO_INIT if the client was not initialized.
See also
redisxLockConnected()
redisxUnlockClient()

References x_error(), X_FAILURE, X_NO_INIT, and X_SUCCESS.

◆ redisxLockConnected()

int redisxLockConnected ( RedisClient cl)

Lock a channel, but only if it has been enabled for communication.

Parameters
clPointer to the Redis client instance
Returns
X_SUCCESS (0) if an excusive lock to the channel has been granted, or X_FAILURE if pthread_mutex_lock() returned an error, or X_NULL if the client is NULL, or X_NO_INIT if the client was not initialized.
See also
redisxLockClient()
redisxUnlockClient()
redisxGetLockedConnectedClient()

References redisxLockClient(), redisxUnlockClient(), x_error(), X_NO_INIT, X_NO_SERVICE, and X_SUCCESS.

◆ redisxReadReplyAsync()

RESP * redisxReadReplyAsync ( RedisClient cl,
int *  pStatus 
)

Reads a response from Redis and returns it.

Parameters
clPointer to a Redis channel
pStatusPointer to int in which to return an error status, or NULL if not required.
Returns
The RESP structure for the reponse received from Redis, or NULL if an error was encountered (errno will be set to describe the error, which may either be an errno produced by recv() or EBADMSG if the message was corrupted and/or unparseable. If the error is irrecoverable i.e., other than a timeout, the client will be disabled.)

References FALSE, RedisMap::key, RESP::n, rCloseClientAsync(), REDIS_INCOMPLETE_TRANSFER, REDIS_SIMPLE_STRING_SIZE, REDIS_UNEXPECTED_RESP, redisxAppendRESP(), redisxDestroyRESP(), redisxHasComponents(), redisxReadReplyAsync(), RESP3_ATTRIBUTE, RESP3_BIG_NUMBER, RESP3_BLOB_ERROR, RESP3_BOOLEAN, RESP3_CONTINUED, RESP3_DOUBLE, RESP3_MAP, RESP3_NULL, RESP3_PUSH, RESP3_SET, RESP3_VERBATIM_STRING, RESP_ARRAY, RESP_BULK_STRING, RESP_ERROR, RESP_INT, RESP_SIMPLE_STRING, TRUE, RESP::type, RESP::value, RedisMap::value, x_error(), X_FAILURE, X_NO_INIT, X_NO_SERVICE, X_PARSE_ERROR, X_SUCCESS, x_trace_null(), xParseDouble(), and xStringCopyOf().

◆ redisxResetClient()

int redisxResetClient ( RedisClient cl)

Sends a RESET request to the specified Redis client. The server will perform a reset as if the client disconnected and reconnected again.

Parameters
clThe Redis client
Returns
X_SUCCESS (0) if successful, or X_NULL if the client is NULL, or another error code (<0) from redisx.h / xchange.h.

References REDIS_UNEXPECTED_RESP, redisxCheckRESP(), redisxDestroyRESP(), redisxLockConnected(), redisxReadReplyAsync(), redisxSendRequestAsync(), redisxUnlockClient(), RESP_SIMPLE_STRING, RESP::value, x_error(), and X_SUCCESS.

◆ redisxSendArrayRequestAsync()

int redisxSendArrayRequestAsync ( RedisClient cl,
const char **  args,
const int *  lengths,
int  n 
)

Send a Redis request with an arbitrary number of arguments.

Parameters
clPointer to the Redis client.
argsThe array of string arguments to send. If you have an char ** array, you may need to cast to (const char **) to avoid compiler warnings.
lengthsArray indicating the number of bytes to send from each string argument. Zero or negative values can be used to determine the string length automatically using strlen(), and the length argument itself may be NULL to determine the lengths of all string arguments automatically.
nThe number of arguments to send.
Returns
X_SUCCESS (0) on success or X_NULL if the client is NULL, or X_NO_SERVICE if not connected to the client or if send() failed, or X_NO_INIT if the client was not initialized.

References FALSE, REDISX_CMDBUF_SIZE, TRUE, x_error(), X_NO_INIT, X_NO_SERVICE, X_SUCCESS, and xvprintf.

◆ redisxSendRequestAsync()

int redisxSendRequestAsync ( RedisClient cl,
const char *  command,
const char *  arg1,
const char *  arg2,
const char *  arg3 
)

Send a command (with up to 3 arguments) to the Redis server. The caller must have an exclusive lock on the client for this version. The arguments supplied will be used up to the first non-NULL value.

Parameters
clPointer to the Redis client instance.
commandRedis command string.
arg1Optional first string argument or NULL.
arg2Optional second string argument or NULL.
arg3Optional third string argument or NULL.
Returns
X_SUCCESS (0) on success or X_NULL if the client is NULL, or else X_NO_SERVICE if not connected to the client or if send() failed

References redisxSendArrayRequestAsync(), x_error(), X_NAME_INVALID, and X_SUCCESS.

◆ redisxSkipReplyAsync()

int redisxSkipReplyAsync ( RedisClient cl)

Instructs Redis to skip sending a reply for the next command.

Sends CLIENT REPLY SKIP

Parameters
clPointer to the Redis client to use.
Returns
X_SUCCESS (0) on success or X_NULL if the client is NULL, or else X_NO_SERVICE if not connected to the Redis server on the requested channel, or if send() failed, or else X_NO_INIT if the client was not initialized.

References TRUE, x_error(), X_NO_INIT, and X_SUCCESS.

◆ redisxStartBlockAsync()

int redisxStartBlockAsync ( RedisClient cl)

Starts an atomic Redis transaction block, by sending MULTI on the specified client connection. Redis transaction blocks behave just like scripts (in fact they are effectively improptu scripts themselves). As such the rules of Redis scripting apply, such as you cannot call LUA from within a transaction block (which is a real pity...)

Once you start a transaction block you may ignore all acknowledgedments such as OK and QUEUED responses that Redis sends back. These will be 'processed' in bulk by redisEndBlockAsync(), at the end of the transaction block.

Parameters
clPointer to a Redis client.
Returns
X_SUCCESS (0) if successful, or X_NULL if the Redis client is NULL, or X_NO_SERVICE if not connected to the client server or if send() failed, or X_NO_INIT if the client was not initialized.
See also
redisxExecBlockAsync()
redisxAbortBlockAsync()

References TRUE, x_error(), X_NO_INIT, and X_SUCCESS.

◆ redisxUnlockClient()

int redisxUnlockClient ( RedisClient cl)

Relinquish exclusive write access to the specified Redis channel

Parameters
clPointer to the Redis client instance
Returns
X_SUCCESS if the exclusive lock for the channel was successfully obtained, or X_FAILURE if pthread_mutex_lock() returned an error, or X_NULL if the client is NULL, or X_NO_INIT if the client was not initialized.
See also
redisxLockClient()
redisxLockConnected()

References x_error(), X_FAILURE, X_NO_INIT, and X_SUCCESS.