zoukankan      html  css  js  c++  java
  • Redis接口的调用

    1、hiredis是redis数据库的C接口,目录为/redis-3.2.6/deps/hiredis
    2、示例代码如下:
        #include <stdio.h>
        #include <stdlib.h>
        #include <stddef.h>
        #include <stdarg.h>
        #include <string.h>
        #include <assert.h>
        #include "hiredis.h"
        
        int main()
        {
            //连接redis
            redisContext* c = redisConnect("127.0.0.1", 6379);
            if ( c->err)
            {
                redisFree(c);
                printf("Connect to redisServer faile
    ");
                return -1;
            }
            printf("Connect to redisServer Success
    ");
            
            const char* setCommand = "set name andy";
            redisReply* r = (redisReply*)redisCommand(c, setCommand);
            
            if( NULL == r)
            {
                printf("Execut setCommand failure
    ");
                redisFree(c);
                return -1;
            }
            if( !(r->type == REDIS_REPLY_STATUS && strcasecmp(r->str,"OK")==0))
            {
                printf("Failed to execute command[%s]
    ",setCommand);
                freeReplyObject(r);
                redisFree(c);
                return -1;
            }
            freeReplyObject(r);
            printf("Succeed to execute command[%s]
    ", setCommand);
            
            
            const char* getCommand = "get name";
            r = (redisReply*)redisCommand(c, getCommand);
            if ( r->type != REDIS_REPLY_STRING)
            {
                printf("Failed to execute command[%s]
    ",getCommand);
                freeReplyObject(r);
                redisFree(c);
                return -1;
            }
            printf("Succeed to execute command[%s]
    ", getCommand);
            printf("The value of 'name' is %s
    ", r->str);
            freeReplyObject(r);
            
            redisFree(c);
            return 0;
        }
    3、编译,运行如下:
        [root@localhost hiredis]# g++ -o main main.cpp libhiredis.a
        [root@localhost hiredis]# ./main
        Connect to redisServer Success
        Succeed to execute command[set name andy]
        Succeed to execute command[get name]
        The value of 'name' is andy
    
  • 相关阅读:
    Animation Curve运动曲线
    Unity3D Collider类的信息传输Ontrigger*与OnCollision*
    springmvc拦截器的简单了解
    JDK中的注解简单了解
    面试加分项---HashMap底层实现原理
    springmvc参数绑定
    springmvc和struts2的区别
    修改tomcat的编码方式,可以解决某些get请求乱码问题
    springmvc 怎么响应json数据
    权限控制框架---shiro入门
  • 原文地址:https://www.cnblogs.com/nzbbody/p/6389618.html
Copyright © 2011-2022 走看看