//redis,add,begin bool QTSServerInterface::ConRedis()//连接redis服务器 { if(fIfConSucess) return true; struct timeval timeout = { 0, 500000 }; // 0.5 seconds fRedisCon = redisConnectWithTimeout(fRedisIP.c_str(), fRedisPort, timeout);//test,redis的ip和端口应该在xml中指定 if (fRedisCon == NULL || fRedisCon->err) { if (fRedisCon) { qtss_printf("INFO:Connect redis failed,%s\n", fRedisCon->errstr); redisFree(fRedisCon); } else { qtss_printf("INFO:Connect redis failed,can't allocate redis context\n"); } fIfConSucess=false; } else { fIfConSucess=true; struct timeval timeoutEx = { 1, 0 }; // 1seconds,设置socket接收和发送超时 redisSetTimeout(fRedisCon,timeoutEx); RedisInit();//可能在这个函数的执行过程中,redis连接又失败了,所以真正的连接失败或者成功要看fIfConSucess qtss_printf("INFO:Connect redis sucess\n"); } return fIfConSucess; }
redisContext* RedisConnPool::Connect() { struct timeval tv; tv.tv_sec = conn_timeout_ms_/1000; tv.tv_usec = (conn_timeout_ms_%1000)*1000; redisContext *c = redisConnectWithTimeout(host_.c_str(), port_, tv); if(c == NULL) { return NULL; } if(c->err) { log_error("connect redis failed. ip=[%s] port=[%d],c->strerr=%s", host_.c_str(), port_, c->errstr); redisFree(c); c = NULL; return NULL; } struct timeval rtv; rtv.tv_sec = recv_timeout_ms_/1000; rtv.tv_usec = (recv_timeout_ms_%1000)*1000; redisSetTimeout(c, rtv); return c; }
int RedisConn::reconnect() { if ( NULL == m_pconf || '\0' == m_pconf->ip[0] || 0 == m_pconf->port) return FAIL; finiConn(); m_conn = redisConnectWithTimeout(m_pconf->ip, m_pconf->port, m_pconf->conn_timeout); //redis server默认端口 if ( NULL == m_conn ) { printf("redis connection init error: conn==NULL\n"); return CONNECT_ERROR; } if(m_conn->err){ printf("redis connection error: %s\n", m_conn->errstr); finiConn(); return CONNECT_ERROR; } if(redisSetTimeout(m_conn, m_pconf->rdwr_timeout)) { printf("redis set time error\n"); finiConn(); return CONNECT_ERROR; } return OK; }
int RedisProxy::connect(const char* host, uint32_t port) { if (NULL == host || '\0' == host[0]) { LOG(WARNING) << "redis proxy: illegal host"; return 1; } if (NULL != _redis_context) { LOG(WARNING) << "redis proxy: illegal host"; return 1; } _host = host; _port = port; _redis_context = redisConnect(host, port); if (NULL == _redis_context) { LOG(WARNING) << "redis proxy: create redis context error"; return 1; } if (_redis_context->err) { LOG(WARNING) << "redis proxy: init connect error, msg[" << __get_err_msg() <<"]"; redisFree(_redis_context); _redis_context = NULL; return 1; } struct timeval tv; tv.tv_sec = _timeout / 1000; tv.tv_usec = (_timeout % 1000) * 1000000; if(REDIS_ERR == redisSetTimeout(_redis_context, tv)) { LOG(WARNING) << "redis proxy: set redis timeout error, timeout[" << _timeout << "]"; redisFree(_redis_context); _redis_context = NULL; return 1; } _redis_context->reader->maxbuf = 0; return 0; }
static void rc_validate_connection(rconn_t *c, int optional) { if (!c->rc && (c->flags & RCF_RECONNECT)) { struct timeval tv; tv.tv_sec = (int) c->timeout; tv.tv_usec = (c->timeout - (double)tv.tv_sec) * 1000000.0; if (c->port < 1) c->rc = redisConnectUnixWithTimeout(c->host, tv); else c->rc = redisConnectWithTimeout(c->host, c->port, tv); if (!c->rc) { if (optional) return; Rf_error("disconnected connection and re-connect to redis failed (NULL context)"); } if (c->rc->err){ SEXP es = Rf_mkChar(c->rc->errstr); redisFree(c->rc); c->rc = 0; if (optional) return; Rf_error("disconnected connection and re-connect to redis failed: %s", CHAR(es)); } redisSetTimeout(c->rc, tv); /* re-connect succeeded */ } if (!c->rc && !optional) Rf_error("disconnected redis connection"); }
static void test_blocking_connection_timeouts(struct config config) { redisContext *c; redisReply *reply; ssize_t s; const char *cmd = "DEBUG SLEEP 3\r\n"; struct timeval tv; c = connect(config); test("Successfully completes a command when the timeout is not exceeded: "); reply = redisCommand(c,"SET foo fast"); freeReplyObject(reply); tv.tv_sec = 0; tv.tv_usec = 10000; redisSetTimeout(c, tv); reply = redisCommand(c, "GET foo"); test_cond(reply != NULL && reply->type == REDIS_REPLY_STRING && memcmp(reply->str, "fast", 4) == 0); freeReplyObject(reply); disconnect(c, 0); c = connect(config); test("Does not return a reply when the command times out: "); s = write(c->fd, cmd, strlen(cmd)); tv.tv_sec = 0; tv.tv_usec = 10000; redisSetTimeout(c, tv); reply = redisCommand(c, "GET foo"); test_cond(s > 0 && reply == NULL && c->err == REDIS_ERR_IO && strcmp(c->errstr, "Resource temporarily unavailable") == 0); freeReplyObject(reply); test("Reconnect properly reconnects after a timeout: "); redisReconnect(c); reply = redisCommand(c, "PING"); test_cond(reply != NULL && reply->type == REDIS_REPLY_STATUS && strcmp(reply->str, "PONG") == 0); freeReplyObject(reply); test("Reconnect properly uses owned parameters: "); config.tcp.host = "foo"; config.unix.path = "foo"; redisReconnect(c); reply = redisCommand(c, "PING"); test_cond(reply != NULL && reply->type == REDIS_REPLY_STATUS && strcmp(reply->str, "PONG") == 0); freeReplyObject(reply); disconnect(c, 0); }
static void test_blocking_io_errors(struct config config) { redisContext *c; redisReply *reply; void *_reply; int major, minor; /* Connect to target given by config. */ c = connect(config); { /* Find out Redis version to determine the path for the next test */ const char *field = "redis_version:"; char *p, *eptr; reply = redisCommand(c,"INFO"); p = strstr(reply->str,field); major = strtol(p+strlen(field),&eptr,10); p = eptr+1; /* char next to the first "." */ minor = strtol(p,&eptr,10); freeReplyObject(reply); } test("Returns I/O error when the connection is lost: "); reply = redisCommand(c,"QUIT"); if (major > 2 || (major == 2 && minor > 0)) { /* > 2.0 returns OK on QUIT and read() should be issued once more * to know the descriptor is at EOF. */ test_cond(strcasecmp(reply->str,"OK") == 0 && redisGetReply(c,&_reply) == REDIS_ERR); freeReplyObject(reply); } else { test_cond(reply == NULL); } /* On 2.0, QUIT will cause the connection to be closed immediately and * the read(2) for the reply on QUIT will set the error to EOF. * On >2.0, QUIT will return with OK and another read(2) needed to be * issued to find out the socket was closed by the server. In both * conditions, the error will be set to EOF. */ assert(c->err == REDIS_ERR_EOF && strcmp(c->errstr,"Server closed the connection") == 0); redisFree(c); c = connect(config); test("Returns I/O error on socket timeout: "); struct timeval tv = { 0, 1000 }; assert(redisSetTimeout(c,tv) == REDIS_OK); test_cond(redisGetReply(c,&_reply) == REDIS_ERR && c->err == REDIS_ERR_IO && errno == EAGAIN); redisFree(c); }
bool Redis::Connect(string &host, int port, int timeout, string passwd, bool auth_enable) { this->host = host; this->port = port; this->timeout = timeout; this->passwd = passwd; this->auth_enable = auth_enable; struct timeval timeout_tv = {0, 0}; log4cpp::Category& _logger = log4cpp::Category::getInstance("MAIN_LOG"); timeout_tv.tv_sec = timeout; for(int i = 0; i < MAX_TRY_COUNT; i++){ connect_ = redisConnectWithTimeout(host.c_str(), port, timeout_tv); if(!connect_ || connect_->err){ continue; } //Maybe,the auth phase will be needed if(auth_enable){ redisReply* reply = NULL; redisSetTimeout(connect_, timeout_tv); reply = (redisReply*)redisCommand(connect_, "AUTH %s", passwd.c_str()); if (NULL == reply || REDIS_REPLY_ERROR == reply->type) { _logger.error("redis AUTH failed"); if (NULL != reply) { freeReplyObject(reply); } redisFree(connect_); connect_ = NULL; continue; } else { _logger.notice(" redis auth success"); freeReplyObject(reply); } } return RESULT_OK; } _logger.error("connect redis error"); return RESULT_ERROR; }
SEXP cr_connect(SEXP sHost, SEXP sPort, SEXP sTimeout, SEXP sReconnect, SEXP sRetry) { const char *host = "localhost"; double tout = Rf_asReal(sTimeout); int port = Rf_asInteger(sPort), reconnect = (Rf_asInteger(sReconnect) > 0), retry = (Rf_asInteger(sRetry) > 0); redisContext *ctx; rconn_t *c; SEXP res; struct timeval tv; if (TYPEOF(sHost) == STRSXP && LENGTH(sHost) > 0) host = CHAR(STRING_ELT(sHost, 0)); tv.tv_sec = (int) tout; tv.tv_usec = (tout - (double)tv.tv_sec) * 1000000.0; if (port < 1) ctx = redisConnectUnixWithTimeout(host, tv); else ctx = redisConnectWithTimeout(host, port, tv); if (!ctx) Rf_error("connect to redis failed (NULL context)"); if (ctx->err){ SEXP es = Rf_mkChar(ctx->errstr); redisFree(ctx); Rf_error("connect to redis failed: %s", CHAR(es)); } c = malloc(sizeof(rconn_t)); if (!c) { redisFree(ctx); Rf_error("unable to allocate connection context"); } c->rc = ctx; c->flags = (reconnect ? RCF_RECONNECT : 0) | (retry ? RCF_RETRY : 0); c->host = strdup(host); c->port = port; c->timeout = tout; redisSetTimeout(ctx, tv); res = PROTECT(R_MakeExternalPtr(c, R_NilValue, R_NilValue)); Rf_setAttrib(res, R_ClassSymbol, Rf_mkString("redisConnection")); R_RegisterCFinalizer(res, rconn_fin); UNPROTECT(1); return res; }
static void test_blocking_connection(void) { redisContext *c; redisReply *reply; int major, minor; test("Returns error when host cannot be resolved: "); c = redisConnect((char*)"idontexist.local", 6379); test_cond(c->err == REDIS_ERR_OTHER && strcmp(c->errstr,"Can't resolve: idontexist.local") == 0); redisFree(c); test("Returns error when the port is not open: "); c = redisConnect((char*)"localhost", 56380); test_cond(c->err == REDIS_ERR_IO && strcmp(c->errstr,"Connection refused") == 0); redisFree(c); __connect(&c); test("Is able to deliver commands: "); reply = redisCommand(c,"PING"); test_cond(reply->type == REDIS_REPLY_STATUS && strcasecmp(reply->str,"pong") == 0) freeReplyObject(reply); /* Switch to DB 9 for testing, now that we know we can chat. */ reply = redisCommand(c,"SELECT 9"); freeReplyObject(reply); /* Make sure the DB is emtpy */ reply = redisCommand(c,"DBSIZE"); if (reply->type != REDIS_REPLY_INTEGER || reply->integer != 0) { printf("Database #9 is not empty, test can not continue\n"); exit(1); } freeReplyObject(reply); test("Is a able to send commands verbatim: "); reply = redisCommand(c,"SET foo bar"); test_cond (reply->type == REDIS_REPLY_STATUS && strcasecmp(reply->str,"ok") == 0) freeReplyObject(reply); test("%%s String interpolation works: "); reply = redisCommand(c,"SET %s %s","foo","hello world"); freeReplyObject(reply); reply = redisCommand(c,"GET foo"); test_cond(reply->type == REDIS_REPLY_STRING && strcmp(reply->str,"hello world") == 0); freeReplyObject(reply); test("%%b String interpolation works: "); reply = redisCommand(c,"SET %b %b","foo",3,"hello\x00world",11); freeReplyObject(reply); reply = redisCommand(c,"GET foo"); test_cond(reply->type == REDIS_REPLY_STRING && memcmp(reply->str,"hello\x00world",11) == 0) test("Binary reply length is correct: "); test_cond(reply->len == 11) freeReplyObject(reply); test("Can parse nil replies: "); reply = redisCommand(c,"GET nokey"); test_cond(reply->type == REDIS_REPLY_NIL) freeReplyObject(reply); /* test 7 */ test("Can parse integer replies: "); reply = redisCommand(c,"INCR mycounter"); test_cond(reply->type == REDIS_REPLY_INTEGER && reply->integer == 1) freeReplyObject(reply); test("Can parse multi bulk replies: "); freeReplyObject(redisCommand(c,"LPUSH mylist foo")); freeReplyObject(redisCommand(c,"LPUSH mylist bar")); reply = redisCommand(c,"LRANGE mylist 0 -1"); test_cond(reply->type == REDIS_REPLY_ARRAY && reply->elements == 2 && !memcmp(reply->element[0]->str,"bar",3) && !memcmp(reply->element[1]->str,"foo",3)) freeReplyObject(reply); /* m/e with multi bulk reply *before* other reply. * specifically test ordering of reply items to parse. */ test("Can handle nested multi bulk replies: "); freeReplyObject(redisCommand(c,"MULTI")); freeReplyObject(redisCommand(c,"LRANGE mylist 0 -1")); freeReplyObject(redisCommand(c,"PING")); reply = (redisCommand(c,"EXEC")); test_cond(reply->type == REDIS_REPLY_ARRAY && reply->elements == 2 && reply->element[0]->type == REDIS_REPLY_ARRAY && reply->element[0]->elements == 2 && !memcmp(reply->element[0]->element[0]->str,"bar",3) && !memcmp(reply->element[0]->element[1]->str,"foo",3) && reply->element[1]->type == REDIS_REPLY_STATUS && strcasecmp(reply->element[1]->str,"pong") == 0); freeReplyObject(reply); { /* Find out Redis version to determine the path for the next test */ const char *field = "redis_version:"; char *p, *eptr; reply = redisCommand(c,"INFO"); p = strstr(reply->str,field); major = strtol(p+strlen(field),&eptr,10); p = eptr+1; /* char next to the first "." */ minor = strtol(p,&eptr,10); freeReplyObject(reply); } test("Returns I/O error when the connection is lost: "); reply = redisCommand(c,"QUIT"); if (major >= 2 && minor > 0) { /* > 2.0 returns OK on QUIT and read() should be issued once more * to know the descriptor is at EOF. */ test_cond(strcasecmp(reply->str,"OK") == 0 && redisGetReply(c,(void**)&reply) == REDIS_ERR); freeReplyObject(reply); } else { test_cond(reply == NULL); } /* On 2.0, QUIT will cause the connection to be closed immediately and * the read(2) for the reply on QUIT will set the error to EOF. * On >2.0, QUIT will return with OK and another read(2) needed to be * issued to find out the socket was closed by the server. In both * conditions, the error will be set to EOF. */ assert(c->err == REDIS_ERR_EOF && strcmp(c->errstr,"Server closed the connection") == 0); redisFree(c); __connect(&c); test("Returns I/O error on socket timeout: "); struct timeval tv = { 0, 1000 }; assert(redisSetTimeout(c,tv) == REDIS_OK); test_cond(redisGetReply(c,(void**)&reply) == REDIS_ERR && c->err == REDIS_ERR_IO && errno == EAGAIN); redisFree(c); /* Context should be connected */ __connect(&c); }
int redisc_init(void) { char addr[256], pass[256], unix_sock_path[256]; unsigned int port, db, sock = 0, haspass = 0; redisc_server_t *rsrv=NULL; param_t *pit = NULL; struct timeval tv_conn; struct timeval tv_cmd; tv_conn.tv_sec = (int) redis_connect_timeout_param / 1000; tv_conn.tv_usec = (int) (redis_connect_timeout_param % 1000) * 1000; tv_cmd.tv_sec = (int) redis_cmd_timeout_param / 1000; tv_cmd.tv_usec = (int) (redis_cmd_timeout_param % 1000) * 1000; if(_redisc_srv_list==NULL) { LM_ERR("no redis servers defined\n"); return -1; } for(rsrv=_redisc_srv_list; rsrv; rsrv=rsrv->next) { port = 6379; db = 0; haspass = 0; sock = 0; memset(addr, 0, sizeof(addr)); memset(pass, 0, sizeof(pass)); memset(unix_sock_path, 0, sizeof(unix_sock_path)); for (pit = rsrv->attrs; pit; pit=pit->next) { if(pit->name.len==4 && strncmp(pit->name.s, "unix", 4)==0) { snprintf(unix_sock_path, sizeof(unix_sock_path)-1, "%.*s", pit->body.len, pit->body.s); sock = 1; } else if(pit->name.len==4 && strncmp(pit->name.s, "addr", 4)==0) { snprintf(addr, sizeof(addr)-1, "%.*s", pit->body.len, pit->body.s); } else if(pit->name.len==4 && strncmp(pit->name.s, "port", 4)==0) { if(str2int(&pit->body, &port) < 0) port = 6379; } else if(pit->name.len==2 && strncmp(pit->name.s, "db", 2)==0) { if(str2int(&pit->body, &db) < 0) db = 0; } else if(pit->name.len==4 && strncmp(pit->name.s, "pass", 4)==0) { snprintf(pass, sizeof(pass)-1, "%.*s", pit->body.len, pit->body.s); haspass = 1; } } if(sock != 0) { LM_DBG("Connecting to unix socket: %s\n", unix_sock_path); rsrv->ctxRedis = redisConnectUnixWithTimeout(unix_sock_path, tv_conn); } else { LM_DBG("Connecting to %s:%d\n", addr, port); rsrv->ctxRedis = redisConnectWithTimeout(addr, port, tv_conn); } LM_DBG("rsrv->ctxRedis = %p\n", rsrv->ctxRedis); if(!rsrv->ctxRedis) { LM_ERR("Failed to create REDIS-Context.\n"); goto err; } if (rsrv->ctxRedis->err) { LM_ERR("Failed to create REDIS returned an error: %s\n", rsrv->ctxRedis->errstr); goto err2; } if ((haspass != 0) && redisc_check_auth(rsrv, pass)) { LM_ERR("Authentication failed.\n"); goto err2; } if (redisSetTimeout(rsrv->ctxRedis, tv_cmd)) { LM_ERR("Failed to set timeout.\n"); goto err2; } if (redisCommandNR(rsrv->ctxRedis, "PING")) { LM_ERR("Failed to send PING (REDIS returned %s).\n", rsrv->ctxRedis->errstr); goto err2; } if ((redis_cluster_param == 0) && redisCommandNR(rsrv->ctxRedis, "SELECT %i", db)) { LM_ERR("Failed to send \"SELECT %i\" (REDIS returned \"%s\", and not in cluster mode).\n", db, rsrv->ctxRedis->errstr); goto err2; } } return 0; err2: if (sock != 0) { LM_ERR("error communicating with redis server [%.*s]" " (unix:%s db:%d): %s\n", rsrv->sname->len, rsrv->sname->s, unix_sock_path, db, rsrv->ctxRedis->errstr); } else { LM_ERR("error communicating with redis server [%.*s] (%s:%d/%d): %s\n", rsrv->sname->len, rsrv->sname->s, addr, port, db, rsrv->ctxRedis->errstr); } if (init_without_redis==1) { LM_WARN("failed to initialize redis connections, but initializing" " module anyway.\n"); return 0; } return -1; err: if (sock != 0) { LM_ERR("failed to connect to redis server [%.*s] (unix:%s db:%d)\n", rsrv->sname->len, rsrv->sname->s, unix_sock_path, db); } else { LM_ERR("failed to connect to redis server [%.*s] (%s:%d/%d)\n", rsrv->sname->len, rsrv->sname->s, addr, port, db); } if (init_without_redis==1) { LM_WARN("failed to initialize redis connections, but initializing" " module anyway.\n"); return 0; } return -1; }
int redisc_reconnect_server(redisc_server_t *rsrv) { char addr[256], pass[256], unix_sock_path[256]; unsigned int port, db, sock = 0, haspass = 0; param_t *pit = NULL; struct timeval tv_conn; struct timeval tv_cmd; tv_conn.tv_sec = (int) redis_connect_timeout_param / 1000; tv_conn.tv_usec = (int) (redis_connect_timeout_param % 1000) * 1000; tv_cmd.tv_sec = (int) redis_cmd_timeout_param / 1000; tv_cmd.tv_usec = (int) (redis_cmd_timeout_param % 1000) * 1000; memset(addr, 0, sizeof(addr)); port = 6379; db = 0; memset(pass, 0, sizeof(pass)); memset(unix_sock_path, 0, sizeof(unix_sock_path)); for (pit = rsrv->attrs; pit; pit=pit->next) { if(pit->name.len==4 && strncmp(pit->name.s, "unix", 4)==0) { snprintf(unix_sock_path, sizeof(unix_sock_path)-1, "%.*s", pit->body.len, pit->body.s); sock = 1; } else if(pit->name.len==4 && strncmp(pit->name.s, "addr", 4)==0) { snprintf(addr, sizeof(addr)-1, "%.*s", pit->body.len, pit->body.s); } else if(pit->name.len==4 && strncmp(pit->name.s, "port", 4)==0) { if(str2int(&pit->body, &port) < 0) port = 6379; } else if(pit->name.len==2 && strncmp(pit->name.s, "db", 2)==0) { if(str2int(&pit->body, &db) < 0) db = 0; } else if(pit->name.len==4 && strncmp(pit->name.s, "pass", 4)==0) { snprintf(pass, sizeof(pass)-1, "%.*s", pit->body.len, pit->body.s); haspass = 1; } } LM_DBG("rsrv->ctxRedis = %p\n", rsrv->ctxRedis); if(rsrv->ctxRedis!=NULL) { redisFree(rsrv->ctxRedis); rsrv->ctxRedis = NULL; } if(sock != 0) { rsrv->ctxRedis = redisConnectUnixWithTimeout(unix_sock_path, tv_conn); } else { rsrv->ctxRedis = redisConnectWithTimeout(addr, port, tv_conn); } LM_DBG("rsrv->ctxRedis = %p\n", rsrv->ctxRedis); if(!rsrv->ctxRedis) goto err; if (rsrv->ctxRedis->err) goto err2; if ((haspass) && redisc_check_auth(rsrv, pass)) goto err2; if (redisSetTimeout(rsrv->ctxRedis, tv_cmd)) goto err2; if (redisCommandNR(rsrv->ctxRedis, "PING")) goto err2; if ((redis_cluster_param == 0) && redisCommandNR(rsrv->ctxRedis, "SELECT %i", db)) goto err2; if (redis_flush_on_reconnect_param) if (redisCommandNR(rsrv->ctxRedis, "FLUSHALL")) goto err2; return 0; err2: if (sock != 0) { LM_ERR("error communicating with redis server [%.*s]" " (unix:%s db:%d): %s\n", rsrv->sname->len, rsrv->sname->s, unix_sock_path, db, rsrv->ctxRedis->errstr); } else { LM_ERR("error communicating with redis server [%.*s] (%s:%d/%d): %s\n", rsrv->sname->len, rsrv->sname->s, addr, port, db, rsrv->ctxRedis->errstr); } err: if (sock != 0) { LM_ERR("failed to connect to redis server [%.*s] (unix:%s db:%d)\n", rsrv->sname->len, rsrv->sname->s, unix_sock_path, db); } else { LM_ERR("failed to connect to redis server [%.*s] (%s:%d/%d)\n", rsrv->sname->len, rsrv->sname->s, addr, port, db); } return -1; }
int main(void) { unsigned int j; redisContext *c; redisReply *reply; struct timeval timeout = { 1, 500000 }; // 1.5 seconds WSADATA wsaData; WSAStartup(MAKEWORD( 2, 2 ), &wsaData); c = redisConnectWithTimeout((char*)"192.168.66.131", 6379, timeout); if (c->err) { printf("Connection error: %s\n", c->errstr); exit(1); } redisSetTimeout(c, timeout); /* PING server */ reply = redisCommand(c,"PING"); printf("PING: %s\n", reply->str); freeReplyObject(reply); /* Set a key */ reply = redisCommand(c,"SET %s %s", "foo", "hello world"); printf("SET: %s\n", reply->str); freeReplyObject(reply); /* Set a key using binary safe API */ reply = redisCommand(c,"SET %b %b", "bar", 3, "hello", 5); printf("SET (binary API): %s\n", reply->str); freeReplyObject(reply); /* Try a GET and two INCR */ reply = redisCommand(c,"GET foo"); printf("GET foo: %s\n", reply->str); freeReplyObject(reply); reply = redisCommand(c,"INCR counter"); printf("INCR counter: %lld\n", reply->integer); freeReplyObject(reply); /* again ... */ reply = redisCommand(c,"INCR counter"); printf("INCR counter: %lld\n", reply->integer); freeReplyObject(reply); /* Create a list of numbers, from 0 to 9 */ reply = redisCommand(c,"DEL mylist"); freeReplyObject(reply); for (j = 0; j < 10; j++) { char buf[64]; snprintf(buf,64,"%d",j); reply = redisCommand(c,"LPUSH mylist element-%s", buf); freeReplyObject(reply); } /* Let's check what we have inside the list */ reply = redisCommand(c,"LRANGE mylist 0 -1"); if (reply->type == REDIS_REPLY_ARRAY) { for (j = 0; j < reply->elements; j++) { printf("%u) %s\n", j, reply->element[j]->str); } } freeReplyObject(reply); return 0; }
/* Internal host connect - Sync or Async */ static int _host_connect( host_t *h, eredis_reader_t *r ) { redisContext *c; if (r) { /* Sync - not in EV context */ c = (h->port) ? redisConnect( h->target, h->port ) : redisConnectUnix( h->target ); if (! c) { fprintf(stderr, "eredis: error: connect sync %s NULL\n", h->target); return 0; } if (c->err) { #if EREDIS_VERBOSE>0 printf( "eredis: error: connect sync %s %d\n", h->target, c->err); #endif redisFree( c ); return 0; } r->ctx = c; r->host = h; } else { redisAsyncContext *ac; /* ASync - in EV context */ ac = (h->port) ? redisAsyncConnect( h->target, h->port ) : redisAsyncConnectUnix( h->target ); if (! ac) { printf( "eredis: error: connect async %s undef\n", h->target); return 0; } if (ac->err) { #if EREDIS_VERBOSE>0 printf( "eredis: error: connect async %s %d\n", h->target, ac->err); #endif redisAsyncFree( ac ); return 0; } h->async_ctx = ac; /* data for _redis_*_cb */ ac->data = h; /* Order is important here */ /* attach */ redisLibevAttach( h->e->loop, ac ); /* set callbacks */ redisAsyncSetDisconnectCallback( ac, _redis_disconnect_cb ); redisAsyncSetConnectCallback( ac, _redis_connect_cb ); c = (redisContext*) ac; } /* Apply keep-alive */ #ifdef HOST_TCP_KEEPALIVE if (h->port) { redisEnableKeepAlive( c ); if (r && (h->e->sync_to.tv_sec||h->e->sync_to.tv_usec)) { redisSetTimeout( c, h->e->sync_to ); } } #endif /* Override the maxbuf */ c->reader->maxbuf = EREDIS_READER_MAX_BUF; return 1; }