void flushdb()
 {
     Redis::Context* cxt = Application::context_map()->get(Prefix::user);
     redisContext* rdb = redisConnect(cxt->host.c_str(),cxt->port);
     redisReply* r = (redisReply*) redisCommand(rdb, "select %d", cxt->db_num);
     freeReplyObject(r);
     r = (redisReply*) redisCommand(rdb,"flushdb");
     freeReplyObject(r);
     redisFree(rdb);
 }
//-------------------------------------------------------------------------------------
bool DBInterfaceRedis::detach()
{
	if(pRedisContext_)
	{
		redisFree(pRedisContext_);
		pRedisContext_ = NULL;
	}
	
	return true;
}
Exemple #3
0
void get(char *key){
    redisContext* c = redisConnect("127.0.0.1", 6379);
    redisReply* r = (redisReply*)redisCommand(c,"get %s",key);
    if(r->type == REDIS_REPLY_STRING){
        printf("get value = %s",r->str);
    }

    freeReplyObject(r);
    redisFree(c);
}
Exemple #4
0
void hiredis_backend__free(git_odb_backend *_backend)
{
    hiredis_backend *backend;
    assert(_backend);
    backend = (hiredis_backend *) _backend;

    redisFree(backend->db);

    free(backend);
}
Exemple #5
0
bool RedisDriver::Reconnect()
{
	if (m_pContext != NULL)
	{
		redisFree(m_pContext);
		m_pContext = NULL;
	}
	XLog(LEVEL_ERROR, "[Redis]: reconnecting...\n");
	return Connect(m_sIP, m_nPort, m_sPwd, m_nConnTimeout);
}
Exemple #6
0
int git_refdb_backend_hiredis(git_refdb_backend **backend_out, const char* prefix, const char* path, const char *host, int port, char* password)
{
	hiredis_refdb_backend *backend;
	redisReply *reply;

	backend = calloc(1, sizeof(hiredis_refdb_backend));
	if (backend == NULL)
		return GITERR_NOMEMORY;

	if (sharedConnection == NULL) {
		sharedConnection = redisConnect(host, port);
		if (sharedConnection->err) {
			free(backend);
			giterr_set_str(GITERR_REFERENCE, "Redis refdb storage couldn't connect to redis server");
			return GIT_ERROR;
		}

		if (password != NULL) {
			reply = redisCommand(sharedConnection, "AUTH %s", password);
			if (reply->type == REDIS_REPLY_ERROR) {
				freeReplyObject(reply);
				redisFree(sharedConnection);
				free(backend);
				giterr_set_str(GITERR_REFERENCE, "Redis refdb storage authentication with redis server failed");
				return GIT_ERROR;
			}
			freeReplyObject(reply);
		}
	}

	backend->db = sharedConnection;

	backend->prefix = strdup(prefix);
	backend->repo_path = strdup(path);

	backend->parent.exists = &hiredis_refdb_backend__exists;
	backend->parent.lookup = &hiredis_refdb_backend__lookup;
	backend->parent.iterator = &hiredis_refdb_backend__iterator;
	backend->parent.write = &hiredis_refdb_backend__write;
	backend->parent.del = &hiredis_refdb_backend__del;
	backend->parent.rename = &hiredis_refdb_backend__rename;
	backend->parent.compress = NULL;
	backend->parent.free = &hiredis_refdb_backend__free;

	backend->parent.has_log = &hiredis_refdb_backend__has_log;
	backend->parent.ensure_log = &hiredis_refdb_backend__ensure_log;
	backend->parent.reflog_read = &hiredis_refdb_backend__reflog_read;
	backend->parent.reflog_write = &hiredis_refdb_backend__reflog_write;
	backend->parent.reflog_rename = &hiredis_refdb_backend__reflog_rename;
	backend->parent.reflog_delete = &hiredis_refdb_backend__reflog_delete;

	*backend_out = (git_refdb_backend *) backend;

	return GIT_OK;
}
static int check_contents(request_rec *req)
{
    llzr_config *conf = ap_get_module_config (c->base_server->module_config,  &llzr_module);
    GTable *post_data = get_post_data(req);
    redis = conf->redisconn;
    reply = conf->redisreply;

    /*
        Things of interest:

        request_rec->uri
        request_rec->useragent_ip
        request_rec->request_time
        request_rec->method
    */

    char *request_remote_ip         = recq->useragent_ip;
    apr_time_t request_timestamp    = recq->request_time;
    char *request_uri               = recq->uri;
    char *request_method            = recq->method;

   /*

        @TODO: look through post request for username and password

        if they are included, create a token id, and insert into redis database
        send back a 402 payment required or 307 to /login/token
        ask the user to authorize login...
        on resubmission ony the token is passed and if exists, login is sucessful
        if not, the entry is logged and evaluated

   */

    /*
    ...
     */
    if (post_data) {

        /* ... hash the password to be secure in memory even.. */
        size_t length = sizeof(post_data['USERNAME_FIELD']);
        unsigned char hash[SHA_DIGEST_LENGTH];
        SHA1(post_data['USERNAME_FIELD'], length, hash);

        reply = redisCommand(redis, "HMSET %s %s %s", request_remote_ip, username, hash);

    }
    /*
    ...
     */

    redisFreeReply(reply);
    redisFree(redis);

    return OK;
}
Exemple #8
0
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;
}
int main()
{
    redisContext* r {redisConnect("localhost",6379)};
    freeReplyObject(redisCommand(r, "flushall"));
    redisFree(r);

    populate_users();
    Test::TextOutput o(Test::TextOutput::Verbose);
    GMTS test {};
    return test.run(o) ? EXIT_SUCCESS : EXIT_FAILURE;
}
Exemple #10
0
bool CRedisClient::ReConnect()
{
	LockerGuard guard(*m_locker);
	if( m_pRedisContext )
	{
		redisFree(m_pRedisContext);
		m_pRedisContext = NULL;
	}
	struct timeval timeout = { 0, 500000 }; // 0.5 seconds 重连
	return OnConnect(&timeout);
}
Exemple #11
0
void init_thread(evhtp_t *htp, evthr_t *thread, void *arg)
{
    thr_arg_t *thr_args;
    thr_args = calloc(1, sizeof(thr_arg_t));
    thr_args->thread = thread;

    char mserver[32];

    if(settings.cache_on == true)
    {
        memcached_st *memc = memcached_create(NULL);
        snprintf(mserver, 32, "%s:%d", settings.cache_ip, settings.cache_port);
        memcached_server_st *servers = memcached_servers_parse(mserver);
        memcached_server_push(memc, servers);
        memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
        memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1); 
        memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, 1); 
        memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE, 1); 
        thr_args->cache_conn = memc;
        LOG_PRINT(LOG_DEBUG, "Memcached Connection Init Finished.");
        memcached_server_list_free(servers);
    }

    if(settings.mode == 2)
    {
        memcached_st *beans = memcached_create(NULL);
        snprintf(mserver, 32, "%s:%d", settings.beansdb_ip, settings.beansdb_port);
        memcached_server_st *servers = memcached_servers_parse(mserver);
        servers = memcached_servers_parse(mserver);
        memcached_server_push(beans, servers);
        memcached_behavior_set(beans, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 0);
        memcached_behavior_set(beans, MEMCACHED_BEHAVIOR_NO_BLOCK, 1); 
        memcached_behavior_set(beans, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE, 1); 
        thr_args->beansdb_conn = beans;
        LOG_PRINT(LOG_DEBUG, "beansdb Connection Init Finished.");
        memcached_server_list_free(servers);
    }
    else if(settings.mode == 3)
    {
        redisContext* c = redisConnect(settings.ssdb_ip, settings.ssdb_port);
        if(c->err)
        {
            redisFree(c);
            LOG_PRINT(LOG_DEBUG, "Connect to ssdb server faile");
        }
        else
        {
            thr_args->ssdb_conn = c;
            LOG_PRINT(LOG_DEBUG, "Connect to ssdb server Success");
        }
    }

    evthr_set_aux(thread, thr_args);
}
void RedisClientPool::Destroy(redisContext* item)
{
	if (item)
	{
		stringstream stream;
		stream << "free redis context";
		LOG(INFO, stream.str());

		redisFree(item);
	}
}
Exemple #13
0
RedisOperator::~RedisOperator()
{
    if(this->ctx)
    {
        redisFree(this->ctx);
        this->ctx = NULL;
    }
    this->host.resize(0);
    this->port = -1;
    this->password.resize(0);
}
Exemple #14
0
HashNodeGenerator::HashNodeGenerator (const std::string& node)
{
	redisContext* rdb = redisConnect(REDIS_HOST, REDIS_PORT);
	__reply = (redisReply*) redisCommand(rdb, "type %s", node.c_str());
	assert(strcmp(__reply->str,"hash") == 0);
	freeReplyObject(__reply);

	__reply = (redisReply*) redisCommand(rdb, "hgetall %s", node.c_str());
	redisFree(rdb);
	current =0;
}
Exemple #15
0
/* Internal methods */
static redisContext* redis_connection(struct redis_handler *handle)
{
	redisContext* context = NULL;
	struct timeval timeout = { 1, 500000 }; // 1.5 seconds
	context = redisConnectWithTimeout((char*)handle->redis_ip, handle->redis_port, timeout);
	if (context == NULL || context->err) {
		if (context != NULL) {
			redisFree(context);
		}
		return NULL;
	}
	/* make keepalive connection */
	if (redisEnableKeepAlive(context) != REDIS_OK){
		if (context != NULL) {
			redisFree(context);
		}
		return NULL;
	}
	return context;
}
Exemple #16
0
void init_banned_syscall(){
    redisContext* r= redisConnect(REDIS_IP,REDIS_PORT) ;
    redisReply *re = redisCommand(r,"lrange lambda:banned_syscall 0 -1") ;
    int i ;
    for(i=0;i<re->elements;i++){
	int curr_sysnum = atoi(re->element[i]->str) ;
	syscall_white_list[curr_sysnum] = 1 ;
    }
    freeReplyObject(re);
    redisFree(r) ;
}
Exemple #17
0
static void cleanup(void) {
    redisContext *c = blocking_context;
    redisReply *reply;

    /* Make sure we're on DB 9 */
    reply = redisCommand(c,"SELECT 9");
    assert(reply != NULL); freeReplyObject(reply);
    reply = redisCommand(c,"FLUSHDB");
    assert(reply != NULL); freeReplyObject(reply);
    redisFree(c);
}
Exemple #18
0
static int pushConnection(lua_State *L, redisContext *con) {
  if (con != NULL && con->err) {
    luaL_error(L, "Error: %s\n", con->errstr);
    lua_pushnil(L);
    lua_pushstring(L, con->errstr);
    redisFree(con);
    return 2;
  }
  lua_pushlightuserdata(L, con);
  return 1;
};
static int _mod_conn_free(REDISSOCK *dissocket)
{
	redisFree(dissocket->conn);

	if (dissocket->reply) {
		freeReplyObject(dissocket->reply);
		dissocket->reply = NULL;
	}

	return 0;
}
Exemple #20
0
/* Helper function to free the context. */
static void __redisAsyncFree(redisAsyncContext *ac) {
    redisContext *c = &(ac->c);
    redisCallback cb;
    dictIterator *it;
    dictEntry *de;

    // 执行所有等待队列中回调函数,将空回复传进去
    /* Execute pending callbacks with NULL reply. */
    while (__redisShiftCallback(&ac->replies,&cb) == REDIS_OK)
        __redisRunCallback(ac,&cb,NULL);

    // 执行所有等待队列中回调函数,将空回复传进去
    // 这里是无效命令的回调函数
    /* Execute callbacks for invalid commands */
    while (__redisShiftCallback(&ac->sub.invalid,&cb) == REDIS_OK)
        __redisRunCallback(ac,&cb,NULL);

    // 执行所有等待队列中回调函数,将空回复传进去
    // 这里是订阅发布的回调函数
    /* Run subscription callbacks callbacks with NULL reply */
    it = dictGetIterator(ac->sub.channels);
    while ((de = dictNext(it)) != NULL)
        __redisRunCallback(ac,dictGetEntryVal(de),NULL);
    dictReleaseIterator(it);
    dictRelease(ac->sub.channels);

    // 执行所有等待队列中回调函数,将空回复传进去
    // 这里是订阅发布的回调函数
    it = dictGetIterator(ac->sub.patterns);
    while ((de = dictNext(it)) != NULL)
        __redisRunCallback(ac,dictGetEntryVal(de),NULL);
    dictReleaseIterator(it);
    dictRelease(ac->sub.patterns);

    // 注销连接上的读写事件
    /* Signal event lib to clean up */
    _EL_CLEANUP(ac);

    // 执行连接关闭回调函数,可做一些清理工作
    /* Execute disconnect callback. When redisAsyncFree() initiated destroying
     * this context, the status will always be REDIS_OK. */
    if (ac->onDisconnect && (c->flags & REDIS_CONNECTED)) {
        if (c->flags & REDIS_FREEING) {
            ac->onDisconnect(ac,REDIS_OK);
        } else {
            ac->onDisconnect(ac,(ac->err == 0) ? REDIS_OK : REDIS_ERR);
        }
    }

    // 释放 redisContext 中的内存
    /* Cleanup self */
    redisFree(c);
}
MHIREDIS_T MHiRedis::cleanUp() {
    // free redis resource
    redisFree(context_);

    // reset members
    context_  = NULL;
    host_     = "";
    port_     = 0;
    password_ = "";
    db_       = -1;
    timeout_  = 0;
}
Exemple #22
0
void justice_cmd::cancel()
{

    if (!m_executor)
        return;

    redisContext *c = redisConnect("127.0.0.1", 6379);
    if (!c)
        return;
    if (c->err != 0) {
        redisFree(c);
        return;
    }

    void *reply  = redisCommand(c, "PUBLISH %s %s", qUtf8Printable(m_info_chnl), "end");
    if (reply)
        freeReplyObject(reply);
    redisFree(c);


}
Exemple #23
0
static void test_free_null(void) {
    void *redisCtx = NULL;
    void *reply = NULL;

    test("Don't fail when redisFree is passed a NULL value: ");
    redisFree(redisCtx);
    test_cond(redisCtx == NULL);

    test("Don't fail when freeReplyObject is passed a NULL value: ");
    freeReplyObject(reply);
    test_cond(reply == NULL);
}
Exemple #24
0
static void test_blocking_connection_errors(void) {
    redisContext *c;

    test("Returns error when host cannot be resolved: ");
    c = redisConnect((char*)"idontexist.local", 6379);
    test_cond(c->err == REDIS_ERR_OTHER &&
        (strcmp(c->errstr,"Name or service not known") == 0 ||
         strcmp(c->errstr,"Can't resolve: idontexist.local") == 0));
    redisFree(c);

    test("Returns error when the port is not open: ");
    c = redisConnect((char*)"localhost", 1);
    test_cond(c->err == REDIS_ERR_IO &&
        strcmp(c->errstr,"Connection refused") == 0);
    redisFree(c);

    test("Returns error when the unix socket path doesn't accept connections: ");
    c = redisConnectUnix((char*)"/tmp/idontexist.sock");
    test_cond(c->err == REDIS_ERR_IO); /* Don't care about the message... */
    redisFree(c);
}
Exemple #25
0
static void adjustClosedConnections( proxyContext *p, redisContext *c ) {
    if( c == NULL )
        return;

    for( int i = 0; i < p->max_count; i++ ) {
        if( p->contexts[i] == c ) {
            redisFree(p->contexts[i]);
            p->contexts[i] = NULL;
            break;
        }
    }
}
static void freeClient(client c) {
    listNode *ln;
    aeDeleteFileEvent(config.el,c->context->fd,AE_WRITABLE);
    aeDeleteFileEvent(config.el,c->context->fd,AE_READABLE);
    redisFree(c->context);
    sdsfree(c->obuf);
    zfree(c);
    config.liveclients--;
    ln = listSearchKey(config.clients,c);
    assert(ln != NULL);
    listDelNode(config.clients,ln);
}
Exemple #27
0
static void disconnectRedis(redisContext *c) {
    redisReply *reply;

    /* Make sure we're on DB 9. */
    reply = redisCommand(c,"SELECT 9");
    freeReplyObject(reply);
    reply = redisCommand(c,"FLUSHDB");
    freeReplyObject(reply);

    /* Free the context as well. */
    redisFree(c);
}
Exemple #28
0
void init_lang_config()
{
    redisContext *r = redisConnect(REDIS_IP,REDIS_PORT) ;
    int i ;
    for(i=0;i<=CPP;i++){
	compiler[i] = redisCommand(r,"get lambda:%s:compiler",support_language[i]);
	execute[i] = redisCommand(r,"get lambda:%s:execute",support_language[i]);
	compile_args[i] = redisCommand(r,"lrange lambda:%s:compile_args 0 -1",support_language[i]) ;
	execute_args[i] = redisCommand(r,"lrange lambda:%s:execute_args 0 -1",support_language[i]) ;
    }
    redisFree(r) ;
}
Exemple #29
0
int rs_redis_append_command(rs_slave_info_t *si, const char *fmt, ...) 
{
    va_list         args;
    redisContext    *c;
    int             i, err;

    i = 0;
    err = 0;
    c = si->c;

    for( ;; ) {

        if(c == NULL) {

            /* retry connect*/
            c = redisConnect(si->redis_addr, si->redis_port);

            if(c->err) {
                if(i % 60 == 0) {
                    i = 0;
                    rs_log_error(RS_LOG_ERR, rs_errno, "redisConnect(\"%s\", "
                            "%d) failed, %s" , si->redis_addr, si->redis_port, 
                            c->errstr);
                }

                redisFree(c);
                c = NULL;

                i += RS_REDIS_CONNECT_RETRY_SLEEP_SEC;
                sleep(RS_REDIS_CONNECT_RETRY_SLEEP_SEC); 

                continue;
            }
        }

        va_start(args, fmt);
        err = redisvAppendCommand(c, fmt, args);
        va_end(args);

        break;
    }

    si->c = c;

    if(err != REDIS_OK) {
        rs_log_error(RS_LOG_ERR, rs_errno, "redisvAppendCommand() failed");
        return RS_ERR;
    }

    si->cmdn++;

    return RS_OK;
}
Exemple #30
0
void rs_free_slave(void *data)
{
    int             err;
    rs_slave_info_t *si;

    si = data == NULL ? rs_slave_info : (rs_slave_info_t *) data;

    if(si != NULL) {

        if(si->io_thread != 0) {

            if((err = pthread_cancel(si->io_thread)) != 0) {
                rs_log_err(err, "pthread_cancel() failed, io_thread");
            }
        }

        if(si->svr_fd != -1) {
            rs_close(si->svr_fd);
        }

        if(si->redis_thread != 0) {

            if((err = pthread_cancel(si->redis_thread)) != 0) {
                rs_log_err(err, "pthread_cancel() failed, redis_thread");
            }
        }

        if(si->c != NULL) {
            redisFree(si->c); 
        }

        if(si->ring_buf != NULL) {
            rs_free_ring_buffer2(si->ring_buf);
            free(si->ring_buf);
        }

        /* close slave info file */
        if(si->info_fd != -1) {
            rs_close(si->info_fd);
        }

        if((err = pthread_attr_destroy(&(si->thread_attr))) != 0) {
            rs_log_err(err, "pthread_attr_destroy() failed, thread_attr");
        }

        rs_free_conf(&(si->conf));
        free(si->conf.kv);

        free(si);
    }

    rs_slave_info = NULL;
}