1、hiredis 分布式锁 测试代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

#include <hiredis.h>
/*
#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
*/

int main(int argc, char **argv) {
        unsigned int j;
        redisContext *c;
        redisReply *reply;
        const char *hostname = (argc > 1) ? argv[1] : "127.0.0.1";
        int port = (argc > 2) ? atoi(argv[2]) : 6379;

        struct timeval timeout = { 1, 500000 }; // 1.5 seconds
        c = redisConnectWithTimeout(hostname, port, timeout);
        if (c == NULL || c->err) {
                if (c) {
                        printf("Connection error: %s\n", c->errstr);
                        redisFree(c);
                } else {
                        printf("Connection error: can't allocate redis context\n");
                }
                exit(1);
        }

        //********************** set lock **********************
        srand((unsigned)time(NULL));
        int randomvalue = rand()%1000000;
        char s[10];
        sprintf(s, "%d", randomvalue);
        while(1)
        {
                reply = redisCommand(c,"set lock %d ex %d nx", randomvalue, 20);    // 20s
                printf("SET lock: type=%d, integer=%lld, str=%s\n", reply->type, reply->integer, reply->str);
                if(reply->type != REDIS_REPLY_NIL && strcmp(reply->str, "OK") == 0){
                        printf("SET lock ok\n");
                        freeReplyObject(reply);
                        break;
                }
                else{
                        printf("set lock failed\n");
                        freeReplyObject(reply);
                        sleep(1);
                        continue;
                }
        }
        //*****************************************************
        //********************** Process **********************
        // get lock
        reply = redisCommand(c,"GET lock");
        printf("GET lock: integer=%lld, str=%s\n", reply->integer, reply->str);
        freeReplyObject(reply);

        // ttl lock
        reply = redisCommand(c,"TTL lock");
        printf("TTL lock: integer=%lld, str=%s\n", reply->integer, reply->str);
        freeReplyObject(reply);

        sleep(5);
        //*****************************************************
        //********************** del lock *********************
        reply = redisCommand(c, "GET lock");
        if(strcmp(reply->str, s) == 0){
                reply = redisCommand(c,"DEL lock");
                printf("DEL lock: integer=%lld, str=%s\n", reply->integer, reply->str);
        }
        freeReplyObject(reply);
        //*****************************************************

        // Disconnects and frees the context
        redisFree(c);

        return 0;

}

2、测试输出

(1)redis-clie1

SET lock: type=5, integer=0, str=OK
SET lock ok
GET lock: integer=0, str=397618
TTL lock: integer=20, str=(null)
DEL lock: integer=1, str=(null)

(2)redis-clie2

SET lock: type=4, integer=0, str=(null)
set lock failed
SET lock: type=4, integer=0, str=(null)
set lock failed
SET lock: type=4, integer=0, str=(null)
set lock failed
SET lock: type=4, integer=0, str=(null)
set lock failed
SET lock: type=5, integer=0, str=OK
SET lock ok
GET lock: integer=0, str=763395
TTL lock: integer=20, str=(null)
DEL lock: integer=1, str=(null)
12-23 15:40