我是靠谱客的博主 完美朋友,这篇文章主要介绍 Redis之hiredis API (String)String,现在分享给大家,希望可以做个参考。

String

复制代码
1
2
3
4
5
6
7
8
9
// // Created by zhangrongxiang on 2018/3/7 13:48 // File string2 // #include <hiredis/hiredis.h> #include <stdlib.h> #include <string.h> #include <unistd.h>

连接redis服务

复制代码
1
2
3
4
5
6
7
8
9
10
11
int main() { redisContext *context = redisConnect("127.0.0.1", 6379); if (context == NULL || context->err) { if (context) { printf("%sn", context->errstr); } else { printf("redisConnect errorn"); } exit(EXIT_FAILURE); } printf("-----------------connect success--------------------n");

SET key value

复制代码
1
2
3
4
5
6
7
8
9
char *key = "str"; char *val = "Hello World"; /*SET key value */ redisReply *reply = redisCommand(context, "SET %s %s", key, val); if (reply->type == REDIS_REPLY_STATUS) { /*SET str Hello World*/ printf("SET %s %sn", key, val); } freeReplyObject(reply);

GET key

复制代码
1
2
3
4
5
6
7
8
9
/*GET key*/ reply = redisCommand(context, "GET %s", key); if (reply->type == REDIS_REPLY_STRING) { /*GET str Hello World*/ printf("GET str %sn", reply->str); /*GET len 11*/ printf("GET len %dn", reply->len); } freeReplyObject(reply);

APPEND key value

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*APPEND key value*/ char *append = " I am your GOD"; reply = redisCommand(context, "APPEND %s %s", key, append); if (reply->type == REDIS_REPLY_INTEGER) { printf("APPEND %s %s n", key, append); } freeReplyObject(reply); /*GET key*/ reply = redisCommand(context, "GET %s", key); if (reply->type == REDIS_REPLY_STRING) { //GET Hello World I am your GOD printf("GET %sn", reply->str); } freeReplyObject(reply);

INCR key

复制代码
1
2
3
4
5
6
7
8
9
10
11
/*INCR key*/ reply = redisCommand(context, "INCR counter"); if (reply->type == REDIS_REPLY_INTEGER) { printf("INCR counter %lldn", reply->integer); } freeReplyObject(reply); reply = redisCommand(context, "INCR counter"); if (reply->type == REDIS_REPLY_INTEGER) { printf("INCR counter %lldn", reply->integer); } freeReplyObject(reply);

DECR key

复制代码
1
2
3
4
5
6
7
8
9
10
11
/*DECR key*/ reply = redisCommand(context, "DECR counter"); if (reply->type == REDIS_REPLY_INTEGER) { printf("DECR counter %lldn", reply->integer); } freeReplyObject(reply); reply = redisCommand(context, "DECR counter"); if (reply->type == REDIS_REPLY_INTEGER) { printf("DECR counter %lldn", reply->integer); } freeReplyObject(reply);

DECRBY key decrement

复制代码
1
2
3
4
5
6
7
8
9
10
11
/*DECRBY key decrement*/ reply = redisCommand(context, "DECRBY counter 5"); if (reply->type == REDIS_REPLY_INTEGER) { printf("DECRBY counter %lldn", reply->integer); } freeReplyObject(reply); reply = redisCommand(context, "DECRBY counter 5"); if (reply->type == REDIS_REPLY_INTEGER) { printf("DECRBY counter %lldn", reply->integer); } freeReplyObject(reply);

INCRBY key increment

复制代码
1
2
3
4
5
6
7
8
9
10
11
/*INCRBY key increment*/ reply = redisCommand(context, "INCRBY counter 5"); if (reply->type == REDIS_REPLY_INTEGER) { printf("INCRBY counter %lldn", reply->integer); } freeReplyObject(reply); reply = redisCommand(context, "INCRBY counter 5"); if (reply->type == REDIS_REPLY_INTEGER) { printf("INCRBY counter %lldn", reply->integer); } freeReplyObject(reply);

ETRANGE key start end

复制代码
1
2
3
4
5
6
7
/*GETRANGE key start end*/ reply = redisCommand(context, "GETRANGE str 0 5"); if (reply->type == REDIS_REPLY_STRING) { /*GETRANGE str Hello*/ printf("GETRANGE %s %sn", key, reply->str); } freeReplyObject(reply);

GETSET key value

复制代码
1
2
3
4
5
6
7
8
9
10
11
/*GETSET key value*/ reply = redisCommand(context, "GETSET %s %s", key, val); if (reply->type == REDIS_REPLY_STRING) { /*GETSET str Hello World I am your GOD*/ printf("GETSET %s %sn", key, reply->str); } /*INCRBYFLOAT key increment*/ reply = redisCommand(context, "INCRBYFLOAT f 2.1"); if (reply->type == REDIS_REPLY_STRING) { printf("INCRBYFLOAT counter %sn", reply->str); }

MSET key value [key value ...]

复制代码
1
2
3
4
5
6
/*MSET key value [key value ...]*/ reply = redisCommand(context, "MSET k1 hello k2 world k3 good"); if (reply->type == REDIS_REPLY_STATUS) { printf("MSET k1 hello k2 world k3 goodn"); } freeReplyObject(reply);

MGET key [key ...]

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*MGET key [key ...]*/ reply = redisCommand(context, "MGET k1 k2 k3"); if (reply->type == REDIS_REPLY_ARRAY) { printf("MGET k1 k2 k3 n"); redisReply **pReply = reply->element; int i = 0; size_t len = reply->elements; //hello world good for (; i < len; ++i) { printf("%s ", pReply[i]->str); } printf("n"); } freeReplyObject(reply);

STRLEN key

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*STRLEN key*/ reply = redisCommand(context, "STRLEN str"); if (reply->type == REDIS_REPLY_INTEGER) { //1 printf("STRLEN str %lld n", reply->integer); } /*SETEX key seconds value*/ reply = redisCommand(context, "SETEX s 30 30seconds"); if (reply->type == REDIS_REPLY_STATUS) { printf("SETEX s 30 30secondsn"); freeReplyObject(reply); int i = 0; while (i++ < 32) { reply = redisCommand(context, "GET s"); if (reply->type == REDIS_REPLY_STRING) { printf("%d s %sn", i, reply->str); } else if (reply->type == REDIS_REPLY_NIL) { printf("%d s niln", i); } freeReplyObject(reply); sleep(1); /* * 29 s 30seconds * 30 s 30seconds * 31 s nil * 32 s nil */ } }

redisFree

复制代码
1
2
3
redisFree(context); return EXIT_SUCCESS; }

struct

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#define REDIS_REPLY_STRING 1 #define REDIS_REPLY_ARRAY 2 #define REDIS_REPLY_INTEGER 3 #define REDIS_REPLY_NIL 4 #define REDIS_REPLY_STATUS 5 #define REDIS_REPLY_ERROR 6 /* This is the reply object returned by redisCommand() */ typedef struct redisReply { int type; /* REDIS_REPLY_* */ long long integer; /* The integer when type is REDIS_REPLY_INTEGER */ int len; /* Length of string */ char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */ size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */ struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */ } redisReply;

All rights reserved

最后

以上就是完美朋友最近收集整理的关于 Redis之hiredis API (String)String的全部内容,更多相关内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部