Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
hiredis
Commits
d1565508
Commit
d1565508
authored
Sep 24, 2010
by
Pieter Noordhuis
Browse files
Rename fd to the more appropriate c
parent
817d26b8
Changes
2
Hide whitespace changes
Inline
Side-by-side
example.c
View file @
d1565508
...
@@ -5,57 +5,57 @@
...
@@ -5,57 +5,57 @@
#include "hiredis.h"
#include "hiredis.h"
int
main
(
void
)
{
int
main
(
void
)
{
redisContext
*
fd
;
unsigned
int
j
;
unsigned
int
j
;
redisContext
*
c
;
redisReply
*
reply
;
redisReply
*
reply
;
fd
=
redisConnect
((
char
*
)
"127.0.0.1"
,
6379
,
NULL
);
c
=
redisConnect
((
char
*
)
"127.0.0.1"
,
6379
,
NULL
);
if
(
fd
->
error
!=
NULL
)
{
if
(
c
->
error
!=
NULL
)
{
printf
(
"Connection error: %s"
,
((
redisReply
*
)
fd
->
error
)
->
reply
);
printf
(
"Connection error: %s"
,
((
redisReply
*
)
c
->
error
)
->
reply
);
exit
(
1
);
exit
(
1
);
}
}
/* PING server */
/* PING server */
reply
=
redisCommand
(
fd
,
"PING"
);
reply
=
redisCommand
(
c
,
"PING"
);
printf
(
"PONG: %s
\n
"
,
reply
->
reply
);
printf
(
"PONG: %s
\n
"
,
reply
->
reply
);
freeReplyObject
(
reply
);
freeReplyObject
(
reply
);
/* Set a key */
/* Set a key */
reply
=
redisCommand
(
fd
,
"SET %s %s"
,
"foo"
,
"hello world"
);
reply
=
redisCommand
(
c
,
"SET %s %s"
,
"foo"
,
"hello world"
);
printf
(
"SET: %s
\n
"
,
reply
->
reply
);
printf
(
"SET: %s
\n
"
,
reply
->
reply
);
freeReplyObject
(
reply
);
freeReplyObject
(
reply
);
/* Set a key using binary safe API */
/* Set a key using binary safe API */
reply
=
redisCommand
(
fd
,
"SET %b %b"
,
"bar"
,
3
,
"hello"
,
5
);
reply
=
redisCommand
(
c
,
"SET %b %b"
,
"bar"
,
3
,
"hello"
,
5
);
printf
(
"SET (binary API): %s
\n
"
,
reply
->
reply
);
printf
(
"SET (binary API): %s
\n
"
,
reply
->
reply
);
freeReplyObject
(
reply
);
freeReplyObject
(
reply
);
/* Try a GET and two INCR */
/* Try a GET and two INCR */
reply
=
redisCommand
(
fd
,
"GET foo"
);
reply
=
redisCommand
(
c
,
"GET foo"
);
printf
(
"GET foo: %s
\n
"
,
reply
->
reply
);
printf
(
"GET foo: %s
\n
"
,
reply
->
reply
);
freeReplyObject
(
reply
);
freeReplyObject
(
reply
);
reply
=
redisCommand
(
fd
,
"INCR counter"
);
reply
=
redisCommand
(
c
,
"INCR counter"
);
printf
(
"INCR counter: %lld
\n
"
,
reply
->
integer
);
printf
(
"INCR counter: %lld
\n
"
,
reply
->
integer
);
freeReplyObject
(
reply
);
freeReplyObject
(
reply
);
/* again ... */
/* again ... */
reply
=
redisCommand
(
fd
,
"INCR counter"
);
reply
=
redisCommand
(
c
,
"INCR counter"
);
printf
(
"INCR counter: %lld
\n
"
,
reply
->
integer
);
printf
(
"INCR counter: %lld
\n
"
,
reply
->
integer
);
freeReplyObject
(
reply
);
freeReplyObject
(
reply
);
/* Create a list of numbers, from 0 to 9 */
/* Create a list of numbers, from 0 to 9 */
reply
=
redisCommand
(
fd
,
"DEL mylist"
);
reply
=
redisCommand
(
c
,
"DEL mylist"
);
freeReplyObject
(
reply
);
freeReplyObject
(
reply
);
for
(
j
=
0
;
j
<
10
;
j
++
)
{
for
(
j
=
0
;
j
<
10
;
j
++
)
{
char
buf
[
64
];
char
buf
[
64
];
snprintf
(
buf
,
64
,
"%d"
,
j
);
snprintf
(
buf
,
64
,
"%d"
,
j
);
reply
=
redisCommand
(
fd
,
"LPUSH mylist element-%s"
,
buf
);
reply
=
redisCommand
(
c
,
"LPUSH mylist element-%s"
,
buf
);
freeReplyObject
(
reply
);
freeReplyObject
(
reply
);
}
}
/* Let's check what we have inside the list */
/* Let's check what we have inside the list */
reply
=
redisCommand
(
fd
,
"LRANGE mylist 0 -1"
);
reply
=
redisCommand
(
c
,
"LRANGE mylist 0 -1"
);
if
(
reply
->
type
==
REDIS_REPLY_ARRAY
)
{
if
(
reply
->
type
==
REDIS_REPLY_ARRAY
)
{
for
(
j
=
0
;
j
<
reply
->
elements
;
j
++
)
{
for
(
j
=
0
;
j
<
reply
->
elements
;
j
++
)
{
printf
(
"%u) %s
\n
"
,
j
,
reply
->
element
[
j
]
->
reply
);
printf
(
"%u) %s
\n
"
,
j
,
reply
->
element
[
j
]
->
reply
);
...
...
test.c
View file @
d1565508
...
@@ -25,32 +25,32 @@ static void __connect(redisContext **c) {
...
@@ -25,32 +25,32 @@ static void __connect(redisContext **c) {
}
}
int
main
(
void
)
{
int
main
(
void
)
{
redisContext
*
fd
;
int
i
,
tests
=
0
,
fails
=
0
;
int
i
,
tests
=
0
,
fails
=
0
;
long
long
t1
,
t2
;
long
long
t1
,
t2
;
redisContext
*
c
;
redisReply
*
reply
;
redisReply
*
reply
;
void
*
reader
;
void
*
reader
;
__connect
(
&
fd
);
__connect
(
&
c
);
test
(
"Returns I/O error when the connection is lost: "
);
test
(
"Returns I/O error when the connection is lost: "
);
reply
=
redisCommand
(
fd
,
"QUIT"
);
reply
=
redisCommand
(
c
,
"QUIT"
);
test_cond
(
reply
->
type
==
REDIS_ERROR
&&
test_cond
(
reply
->
type
==
REDIS_ERROR
&&
strcmp
(
reply
->
reply
,
"Server closed the connection"
)
==
0
);
strcmp
(
reply
->
reply
,
"Server closed the connection"
)
==
0
);
freeReplyObject
(
reply
);
freeReplyObject
(
reply
);
__connect
(
&
fd
);
/* reconnect */
__connect
(
&
c
);
/* reconnect */
test
(
"Is able to deliver commands: "
);
test
(
"Is able to deliver commands: "
);
reply
=
redisCommand
(
fd
,
"PING"
);
reply
=
redisCommand
(
c
,
"PING"
);
test_cond
(
reply
->
type
==
REDIS_REPLY_STRING
&&
test_cond
(
reply
->
type
==
REDIS_REPLY_STRING
&&
strcasecmp
(
reply
->
reply
,
"pong"
)
==
0
)
strcasecmp
(
reply
->
reply
,
"pong"
)
==
0
)
freeReplyObject
(
reply
);
freeReplyObject
(
reply
);
/* Switch to DB 9 for testing, now that we know we can chat. */
/* Switch to DB 9 for testing, now that we know we can chat. */
reply
=
redisCommand
(
fd
,
"SELECT 9"
);
reply
=
redisCommand
(
c
,
"SELECT 9"
);
freeReplyObject
(
reply
);
freeReplyObject
(
reply
);
/* Make sure the DB is emtpy */
/* Make sure the DB is emtpy */
reply
=
redisCommand
(
fd
,
"DBSIZE"
);
reply
=
redisCommand
(
c
,
"DBSIZE"
);
if
(
reply
->
type
!=
REDIS_REPLY_INTEGER
||
if
(
reply
->
type
!=
REDIS_REPLY_INTEGER
||
reply
->
integer
!=
0
)
{
reply
->
integer
!=
0
)
{
printf
(
"Sorry DB 9 is not empty, test can not continue
\n
"
);
printf
(
"Sorry DB 9 is not empty, test can not continue
\n
"
);
...
@@ -61,23 +61,23 @@ int main(void) {
...
@@ -61,23 +61,23 @@ int main(void) {
freeReplyObject
(
reply
);
freeReplyObject
(
reply
);
test
(
"Is a able to send commands verbatim: "
);
test
(
"Is a able to send commands verbatim: "
);
reply
=
redisCommand
(
fd
,
"SET foo bar"
);
reply
=
redisCommand
(
c
,
"SET foo bar"
);
test_cond
(
reply
->
type
==
REDIS_REPLY_STRING
&&
test_cond
(
reply
->
type
==
REDIS_REPLY_STRING
&&
strcasecmp
(
reply
->
reply
,
"ok"
)
==
0
)
strcasecmp
(
reply
->
reply
,
"ok"
)
==
0
)
freeReplyObject
(
reply
);
freeReplyObject
(
reply
);
test
(
"%%s String interpolation works: "
);
test
(
"%%s String interpolation works: "
);
reply
=
redisCommand
(
fd
,
"SET %s %s"
,
"foo"
,
"hello world"
);
reply
=
redisCommand
(
c
,
"SET %s %s"
,
"foo"
,
"hello world"
);
freeReplyObject
(
reply
);
freeReplyObject
(
reply
);
reply
=
redisCommand
(
fd
,
"GET foo"
);
reply
=
redisCommand
(
c
,
"GET foo"
);
test_cond
(
reply
->
type
==
REDIS_REPLY_STRING
&&
test_cond
(
reply
->
type
==
REDIS_REPLY_STRING
&&
strcmp
(
reply
->
reply
,
"hello world"
)
==
0
);
strcmp
(
reply
->
reply
,
"hello world"
)
==
0
);
freeReplyObject
(
reply
);
freeReplyObject
(
reply
);
test
(
"%%b String interpolation works: "
);
test
(
"%%b String interpolation works: "
);
reply
=
redisCommand
(
fd
,
"SET %b %b"
,
"foo"
,
3
,
"hello
\x00
world"
,
11
);
reply
=
redisCommand
(
c
,
"SET %b %b"
,
"foo"
,
3
,
"hello
\x00
world"
,
11
);
freeReplyObject
(
reply
);
freeReplyObject
(
reply
);
reply
=
redisCommand
(
fd
,
"GET foo"
);
reply
=
redisCommand
(
c
,
"GET foo"
);
test_cond
(
reply
->
type
==
REDIS_REPLY_STRING
&&
test_cond
(
reply
->
type
==
REDIS_REPLY_STRING
&&
memcmp
(
reply
->
reply
,
"hello
\x00
world"
,
11
)
==
0
)
memcmp
(
reply
->
reply
,
"hello
\x00
world"
,
11
)
==
0
)
...
@@ -86,20 +86,20 @@ int main(void) {
...
@@ -86,20 +86,20 @@ int main(void) {
freeReplyObject
(
reply
);
freeReplyObject
(
reply
);
test
(
"Can parse nil replies: "
);
test
(
"Can parse nil replies: "
);
reply
=
redisCommand
(
fd
,
"GET nokey"
);
reply
=
redisCommand
(
c
,
"GET nokey"
);
test_cond
(
reply
->
type
==
REDIS_REPLY_NIL
)
test_cond
(
reply
->
type
==
REDIS_REPLY_NIL
)
freeReplyObject
(
reply
);
freeReplyObject
(
reply
);
/* test 7 */
/* test 7 */
test
(
"Can parse integer replies: "
);
test
(
"Can parse integer replies: "
);
reply
=
redisCommand
(
fd
,
"INCR mycounter"
);
reply
=
redisCommand
(
c
,
"INCR mycounter"
);
test_cond
(
reply
->
type
==
REDIS_REPLY_INTEGER
&&
reply
->
integer
==
1
)
test_cond
(
reply
->
type
==
REDIS_REPLY_INTEGER
&&
reply
->
integer
==
1
)
freeReplyObject
(
reply
);
freeReplyObject
(
reply
);
test
(
"Can parse multi bulk replies: "
);
test
(
"Can parse multi bulk replies: "
);
freeReplyObject
(
redisCommand
(
fd
,
"LPUSH mylist foo"
));
freeReplyObject
(
redisCommand
(
c
,
"LPUSH mylist foo"
));
freeReplyObject
(
redisCommand
(
fd
,
"LPUSH mylist bar"
));
freeReplyObject
(
redisCommand
(
c
,
"LPUSH mylist bar"
));
reply
=
redisCommand
(
fd
,
"LRANGE mylist 0 -1"
);
reply
=
redisCommand
(
c
,
"LRANGE mylist 0 -1"
);
test_cond
(
reply
->
type
==
REDIS_REPLY_ARRAY
&&
test_cond
(
reply
->
type
==
REDIS_REPLY_ARRAY
&&
reply
->
elements
==
2
&&
reply
->
elements
==
2
&&
!
memcmp
(
reply
->
element
[
0
]
->
reply
,
"bar"
,
3
)
&&
!
memcmp
(
reply
->
element
[
0
]
->
reply
,
"bar"
,
3
)
&&
...
@@ -109,10 +109,10 @@ int main(void) {
...
@@ -109,10 +109,10 @@ int main(void) {
/* m/e with multi bulk reply *before* other reply.
/* m/e with multi bulk reply *before* other reply.
* specifically test ordering of reply items to parse. */
* specifically test ordering of reply items to parse. */
test
(
"Can handle nested multi bulk replies: "
);
test
(
"Can handle nested multi bulk replies: "
);
freeReplyObject
(
redisCommand
(
fd
,
"MULTI"
));
freeReplyObject
(
redisCommand
(
c
,
"MULTI"
));
freeReplyObject
(
redisCommand
(
fd
,
"LRANGE mylist 0 -1"
));
freeReplyObject
(
redisCommand
(
c
,
"LRANGE mylist 0 -1"
));
freeReplyObject
(
redisCommand
(
fd
,
"PING"
));
freeReplyObject
(
redisCommand
(
c
,
"PING"
));
reply
=
(
redisCommand
(
fd
,
"EXEC"
));
reply
=
(
redisCommand
(
c
,
"EXEC"
));
test_cond
(
reply
->
type
==
REDIS_REPLY_ARRAY
&&
test_cond
(
reply
->
type
==
REDIS_REPLY_ARRAY
&&
reply
->
elements
==
2
&&
reply
->
elements
==
2
&&
reply
->
element
[
0
]
->
type
==
REDIS_REPLY_ARRAY
&&
reply
->
element
[
0
]
->
type
==
REDIS_REPLY_ARRAY
&&
...
@@ -147,22 +147,22 @@ int main(void) {
...
@@ -147,22 +147,22 @@ int main(void) {
test
(
"Throughput:
\n
"
);
test
(
"Throughput:
\n
"
);
for
(
i
=
0
;
i
<
500
;
i
++
)
for
(
i
=
0
;
i
<
500
;
i
++
)
freeReplyObject
(
redisCommand
(
fd
,
"LPUSH mylist foo"
));
freeReplyObject
(
redisCommand
(
c
,
"LPUSH mylist foo"
));
t1
=
usec
();
t1
=
usec
();
for
(
i
=
0
;
i
<
1000
;
i
++
)
for
(
i
=
0
;
i
<
1000
;
i
++
)
freeReplyObject
(
redisCommand
(
fd
,
"PING"
));
freeReplyObject
(
redisCommand
(
c
,
"PING"
));
t2
=
usec
();
t2
=
usec
();
printf
(
"
\t
(1000x PING: %.2fs)
\n
"
,
(
t2
-
t1
)
/
1000000
.
0
);
printf
(
"
\t
(1000x PING: %.2fs)
\n
"
,
(
t2
-
t1
)
/
1000000
.
0
);
t1
=
usec
();
t1
=
usec
();
for
(
i
=
0
;
i
<
1000
;
i
++
)
for
(
i
=
0
;
i
<
1000
;
i
++
)
freeReplyObject
(
redisCommand
(
fd
,
"LRANGE mylist 0 499"
));
freeReplyObject
(
redisCommand
(
c
,
"LRANGE mylist 0 499"
));
t2
=
usec
();
t2
=
usec
();
printf
(
"
\t
(1000x LRANGE with 500 elements: %.2fs)
\n
"
,
(
t2
-
t1
)
/
1000000
.
0
);
printf
(
"
\t
(1000x LRANGE with 500 elements: %.2fs)
\n
"
,
(
t2
-
t1
)
/
1000000
.
0
);
/* Clean DB 9 */
/* Clean DB 9 */
reply
=
redisCommand
(
fd
,
"FLUSHDB"
);
reply
=
redisCommand
(
c
,
"FLUSHDB"
);
freeReplyObject
(
reply
);
freeReplyObject
(
reply
);
if
(
fails
==
0
)
{
if
(
fails
==
0
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment