码迷,mamicode.com
首页 > 其他好文 > 详细

clion redis-demo

时间:2020-02-04 18:24:51      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:and   cli   github   auth   world   add   name   cut   nec   

1. github hiredis安装

redis-demo.c

#include <stdio.h>
#include <stdlib.h>
#include <hiredis.h>

int main(int argc, char **argv) {
//    if (argc < 3) {
//        printf("usage: connRedis hostname port password\n");
//        return 1;
//    }
//    char *hostname = argv[1];
//    int port = atoi(argv[2]);
//    char *password = argv[3];

    char *hostname = "127.0.0.1";
    int port = 6379;
    char *password = "";

    redisContext *conn;
    redisReply *reply;
    struct timeval timeout = {1, 500000};
    conn = redisConnectWithTimeout(hostname, port, timeout);
    // conn erro
    if (conn == NULL || conn->err) {
        if (conn) {
            printf("connection error %s\n", conn->errstr);
            exit(1);
        } else {
            printf("cannot alloc redis context\n");
            exit(1);
        }
    }

    // auth
    reply = redisCommand(conn, "AUTH %s", password);
    printf("auth is %s\n", reply->str);
    freeReplyObject(reply);

    // set
    reply = redisCommand(conn, "set %s %s", "name", "hello world11111");
    printf("set is %s\n", reply->str);
    freeReplyObject(reply);

    // get
    reply = redisCommand(conn, "get name");
    printf("name :%s\n", reply->str);
    freeReplyObject(reply);

    // free conn
    redisFree(conn);
    return 0;
}

  cmklist.txt

include_directories(/usr/local/include/hiredis)
link_directories(/usr/local/lib)
link_libraries(hiredis)
add_executable(redis-demo redis-demo.c)

  

clion redis-demo

标签:and   cli   github   auth   world   add   name   cut   nec   

原文地址:https://www.cnblogs.com/luckygxf/p/12260325.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!