• C语言操作Redis总结


      1 #include "hiredis.h"
      2 
      3 #define NO_QFORKIMPL
      4 #pragma comment(lib,"hiredis.lib")
      5 #pragma comment(lib,"Win32_Interop.lib")
      6 
      7 int get_int_command(char int_command[200])
      8 {
      9     reply = (redisReply *)redisCommand(c, int_command);
     10     //printf("exists命令执行结果: %d
    ", reply->type);
     11     if (reply->type == 3)    //返回整型标识
     12     {
     13         //printf("%s命令执行结果: %d
    ", int_command, reply->integer);
     14         return reply->integer;
     15     }
     16     else if (reply->type == 4)    //返回nil对象
     17     {
     18         return -1;
     19     }
     20     else if (reply->type == 6)    //返回错误
     21     {
     22         return -2;
     23     }
     24     freeReplyObject(reply);
     25     return 0;
     26 }
     27 
     28 char* get_string_command(char string_command[200])
     29 {
     30     reply = (redisReply *)redisCommand(c, string_command);
     31     //printf("lindex MA_h1_K 0命令执行结果 reply type: %d
    ", reply->type);
     32     if (reply->type == 1)    //返回字符串标识
     33     {
     34         //printf("lindex MA_h1_K 0命令执行结果 reply type: %s
    ", reply->str);
     35         return reply->str;
     36     }
     37     else if (reply->type == 4)    //返回nil对象
     38     {
     39         return "不存在要访问的数据";
     40     }
     41     else if (reply->type == 6)    //返回错误
     42     {
     43         return reply->str;
     44     }
     45     freeReplyObject(reply);
     46     return "";
     47 }
     48 
     49 void run_command(char run_command[200])
     50 {
     51     reply = (redisReply *)redisCommand(c, run_command);
     52     //printf("reply type: %d
    ", reply->type);
     53     if (reply->type == 5)
     54     {
     55         //printf("run_command执行结果: %s
    ", reply->str);
     56     }
     57     freeReplyObject(reply);
     58 }
     59 
     60 int main()
     61 {
     62     SYSTEMTIME sys;
     63     char local_time[25] = "";
     64     
     65     c = redisConnect((char*)redis_host, redis_port);
     66     if (c->err) {    /* Error flags, 0 when there is no error */
     67         printf("连接Redis失败: %s
    ", c->errstr);
     68         exit(1);
     69     }
     70     else
     71     {
     72         printf("连接Redis成功!
    ");
     73     }
     74 
     75     reply = (redisReply *)redisCommand(c, "AUTH %s", redis_password);
     76     if (reply->type == REDIS_REPLY_ERROR) {
     77         printf("Redis认证失败!
    ");
     78     }
     79     else
     80     {
     81         printf("Redis认证成功!
    ");
     82     }
     83     freeReplyObject(reply);
     84     
     85     reply = (redisReply *)redisCommand(c, "SELECT 1");    //选择数据库
     86     printf("SELECT: 1 %s
    ", reply->str);
     87     freeReplyObject(reply);
     88     
     89     //delete命令
     90     run_command("DEL foo");
     91     
     92     //set命令
     93     run_command("SET foo hello world");
     94 
     95 
     96     //get命令
     97     printf("GET foo命令执行结果 : %s
    ", get_string_command("GET foo"));
     98 
     99     
    100     //exists命令
    101     printf("exists test1命令执行结果: %d
    ", get_int_command("exists test1"));
    102     printf("exists MA_h1_K命令执行结果: %d
    ", get_int_command("exists MA_h1_K"));
    103     
    104     
    105     //llen命令
    106     printf("llen MA_h1_K命令执行结果: %d
    ", get_int_command("llen MA_h1_K"));
    107 
    108     
    109     //lrange命令
    110     reply = (redisReply *)redisCommand(c, "lrange MA_h1_K 0 7");
    111     //printf("lrange MA_h1_K 0 7命令执行结果 reply type: %d
    ", reply->type);
    112     if (reply->type == 2)
    113     {
    114         printf("队列数量为: %d
    ", reply->elements);
    115         if (reply->element[0]->type == 1)
    116         {
    117             for (int i = 0; i < reply->elements; i++)
    118             {
    119                 printf("lrange MA_h1_K 0 7命令执行结果: %s
    ", reply->element[i]->str);
    120             }
    121         }
    122         
    123     }
    124     freeReplyObject(reply);
    125     
    126     //lindex命令
    127     printf("lindex MA_h1_K 0命令执行结果 : %s
    ", get_string_command("lindex MA_h1_K 0"));
    128 
    129     
    130     //lpush命令
    131     run_command("lpush list test1 test2 test3");
    132     
    133     //lpop命令
    134     printf("lpop list命令执行结果 : %s
    ", get_string_command("lpop list"));
    135     
    136     //rpop命令
    137     printf("rpop list命令执行结果 : %s
    ", get_string_command("rpop list"));
    138     
    139     //rpoplpush命令
    140     printf("rpoplpush list list1命令执行结果 : %s
    ", get_string_command("rpoplpush list list1"));
    141     
    142     printf("lpop list1命令执行结果 : %s
    ", get_string_command("lpop list1"));
    143     
    144     //lpush    rpush    lpop    rpop    RPOPLPUSH
    145     
    146     
    147     char test;
    148     test = getchar();
    149 }
  • 相关阅读:
    部署webapp到web容器的三种方式(这里的web容器Tomcat)
    jquery1.8.3文档的一些问题(可能是我理解错了,不喜勿喷)
    JQuery异步请求之省略dataType的设置
    Tomcat启动一闪而过的问题(我是windows系统,所以查看的是.bat的文件)
    tomcat控制台乱码问题
    IDEA常用的快捷键和代码模板
    ajax返回问题
    php大文件下载问题
    微信企业付款到银行卡
    支付宝单笔转账支付功能
  • 原文地址:https://www.cnblogs.com/hushaojun/p/7291788.html
Copyright © 2020-2023  润新知