-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
85 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
SRC := examples | ||
|
||
include config.mk | ||
|
||
LDFLAGS += -L$(LIB) -lredisx | ||
LD_LIBRARY_PATH := $(LIB):$(LD_LIBRARY_PATH) | ||
|
||
.PHONY: all | ||
all: $(BIN)/hello | ||
|
||
include build.mk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#include <redisx.h> | ||
|
||
|
||
/* | ||
* Pings a Redis server with a hello message, and prints the response, or else an error. | ||
* | ||
* Usage: hello [host] | ||
* | ||
* Options: | ||
* host The Redis server host (default: "127.0.0.1") | ||
* | ||
* Exit status: | ||
* 0 Successful completion | ||
* 1 Could not connect to Redis server | ||
* 2 Bad response from server | ||
* | ||
* Author: Attila Kovacs | ||
* Version: 19 December 2024 | ||
*/ | ||
int main(int argc, char *argv[]) { | ||
// Initialize for Redis server at 'my-server.com'. | ||
Redis *redis; | ||
char *host = "127.0.0.1"; | ||
|
||
if(argc > 1) host = argv[1]; | ||
|
||
redis = redisxInit(host); | ||
|
||
// Connect to redis | ||
if(redis != NULL && redisxConnect(redis, FALSE) == X_SUCCESS) { | ||
// Run 'GET my-key' query on the server | ||
RESP *reply = redisxRequest(redis, "PING", "Hello World!", NULL, NULL, NULL); | ||
|
||
// Check that we got a response of the expected type (bulk string of any length) | ||
if(redisxCheckRESP(reply, RESP_BULK_STRING, 0) == X_SUCCESS) | ||
printf("%s\n", (char *) reply->value); | ||
else { | ||
fprintf(stderr, "ERROR! bad response\n"); | ||
return 2; | ||
} | ||
|
||
// Clean up | ||
redisxDestroyRESP(reply); | ||
redisxDisconnect(redis); | ||
} | ||
else { | ||
perror("ERROR! could not connect"); | ||
return 1; | ||
} | ||
|
||
// Free up the resources used by the 'redis' instance. | ||
redisxDestroy(redis); | ||
|
||
return 0; | ||
} |