Redis之hiredis API (String)

本文通过C语言示例代码展示了如何使用Hiredis库连接Redis并执行字符串相关的操作,包括设置值、获取值、追加值等。同时,还演示了计数器的递增与递减操作以及字符串长度获取等功能。

String

//
// 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服务

int main() {
    redisContext *context = redisConnect("127.0.0.1", 6379);
    if (context == NULL || context->err) {
        if (context) {
            printf("%s\n", context->errstr);
        } else {
            printf("redisConnect error\n");
        }
        exit(EXIT_FAILURE);
    }
    printf("-----------------connect success--------------------\n");

SET key value

    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 %s\n", key, val);
    }
    freeReplyObject(reply);

GET key

    /*GET key*/
    reply = redisCommand(context, "GET %s", key);
    if (reply->type == REDIS_REPLY_STRING) {
        /*GET str Hello World*/
        printf("GET str %s\n", reply->str);
        /*GET len 11*/
        printf("GET len %d\n", reply->len);
    }
    freeReplyObject(reply);

APPEND key value

    /*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 %s\n", reply->str);
    }
    freeReplyObject(reply);

INCR key

    /*INCR key*/
    reply = redisCommand(context, "INCR counter");
    if (reply->type == REDIS_REPLY_INTEGER) {
        printf("INCR counter %lld\n", reply->integer);
    }
    freeReplyObject(reply);
    reply = redisCommand(context, "INCR counter");
    if (reply->type == REDIS_REPLY_INTEGER) {
        printf("INCR counter %lld\n", reply->integer);
    }
    freeReplyObject(reply);

DECR key

    /*DECR key*/
    reply = redisCommand(context, "DECR counter");
    if (reply->type == REDIS_REPLY_INTEGER) {
        printf("DECR counter %lld\n", reply->integer);
    }
    freeReplyObject(reply);
    reply = redisCommand(context, "DECR counter");
    if (reply->type == REDIS_REPLY_INTEGER) {
        printf("DECR counter %lld\n", reply->integer);
    }
    freeReplyObject(reply);

DECRBY key decrement

    /*DECRBY key decrement*/
    reply = redisCommand(context, "DECRBY counter 5");
    if (reply->type == REDIS_REPLY_INTEGER) {
        printf("DECRBY counter %lld\n", reply->integer);
    }
    freeReplyObject(reply);
    reply = redisCommand(context, "DECRBY counter 5");
    if (reply->type == REDIS_REPLY_INTEGER) {
        printf("DECRBY counter %lld\n", reply->integer);
    }
    freeReplyObject(reply);

INCRBY key increment

    /*INCRBY key increment*/
    reply = redisCommand(context, "INCRBY counter 5");
    if (reply->type == REDIS_REPLY_INTEGER) {
        printf("INCRBY counter %lld\n", reply->integer);
    }
    freeReplyObject(reply);
    reply = redisCommand(context, "INCRBY counter 5");
    if (reply->type == REDIS_REPLY_INTEGER) {
        printf("INCRBY counter %lld\n", reply->integer);
    }
    freeReplyObject(reply);

ETRANGE key start end

    /*GETRANGE key start end*/
    reply = redisCommand(context, "GETRANGE str 0 5");
    if (reply->type == REDIS_REPLY_STRING) {
        /*GETRANGE str Hello*/
        printf("GETRANGE %s %s\n", key, reply->str);
    }
    freeReplyObject(reply);

GETSET key value

    /*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 %s\n", key, reply->str);
    }
    /*INCRBYFLOAT key increment*/
    reply = redisCommand(context, "INCRBYFLOAT f 2.1");
    if (reply->type == REDIS_REPLY_STRING) {
        printf("INCRBYFLOAT counter %s\n", reply->str);
    }

MSET key value [key value ...]

    /*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 good\n");
    }
    freeReplyObject(reply);

MGET key [key ...]

    /*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

    /*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 30seconds\n");
        freeReplyObject(reply);
        int i = 0;
        while (i++ < 32) {
            reply = redisCommand(context, "GET s");
            if (reply->type == REDIS_REPLY_STRING) {
                printf("%d s %s\n", i, reply->str);
            } else if (reply->type == REDIS_REPLY_NIL) {
                printf("%d s nil\n", i);
            }
            freeReplyObject(reply);
            sleep(1);
            /*
             * 29 s 30seconds
             * 30 s 30seconds
             * 31 s nil
             * 32 s nil
             */
        }
    }

redisFree

    redisFree(context);
    return EXIT_SUCCESS;
}

struct

#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

转载于:https://www.cnblogs.com/zhangrxiang/p/8522934.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值