Ejemplo n.º 1
0
AbstractMemCachedService NewMemCachedService(char * host, int port1, char* host2, int port2) {
	AbstractMemCachedService abstractMemCachedService;
	if ((abstractMemCachedService = malloc(sizeof(struct ConcreteMemCachedService)))) {
		abstractMemCachedService->memc = memcached_create(NULL);
		memcached_server_add(abstractMemCachedService->memc, host, port1);
		abstractMemCachedService->memc2 = memcached_create(NULL);
		memcached_server_add(abstractMemCachedService->memc2, host2, port2);
	}
	return abstractMemCachedService;
}
Ejemplo n.º 2
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);
}
Ejemplo n.º 3
0
int main(void)
{
  memcached_st *memc= memcached_create(NULL);
  memcached_free(memc);

  return EXIT_SUCCESS;
}
Ejemplo n.º 4
0
void
BeatBoard::ApiMemcached::setUpMemcached( const std::string& memcached_host,
                                         const int memcached_port )
{
  memcached_server_st *servers;
  memcached_return rc;

  memcached_status = false;
  memc = memcached_create(NULL);
  servers = memcached_server_list_append(NULL, memcached_host.c_str(),
                                         memcached_port, &rc);
  if (rc != MEMCACHED_SUCCESS) {
    std::cerr << memcached_strerror(memc, rc) << std::endl;
    return;
  }

  rc = memcached_server_push(memc, servers);
  if (rc != MEMCACHED_SUCCESS) {
    std::cerr << memcached_strerror(memc, rc) << std::endl;
    return;
  }

  memcached_server_list_free(servers);
  memcached_status = true;
}
Ejemplo n.º 5
0
void *logpool_memcache_init(logpool_t *ctx, logpool_param_t *p)
{
    struct logpool_param_memcache *args = cast(struct logpool_param_memcache *, p);
    const char *host = args->host;
    long port = args->port;
    mc_t *mc = cast(mc_t *, logpool_string_init(ctx, p));
    memcached_return_t rc;
    memcached_server_list_st servers;

    mc->st = memcached_create(NULL);
#if 0
    memcached_behavior_set (mc->st, MEMCACHED_BEHAVIOR_NO_BLOCK, 1);
#endif
#ifdef USE_BUFFER_REQ
    memcached_behavior_set (mc->st, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
#endif
    if (unlikely(mc->st == NULL)) {
        /* TODO Error */
        abort();
    }
    servers = memcached_server_list_append(NULL, host, port, &rc);
    if (unlikely(rc != MEMCACHED_SUCCESS)) {
        /* TODO Error */
        fprintf(stderr, "Error!! '%s'\n", memcached_strerror(mc->st, rc));
        abort();
    }
    rc = memcached_server_push(mc->st, servers);
    if (unlikely(rc != MEMCACHED_SUCCESS)) {
        /* TODO Error */
        fprintf(stderr, "Error!! '%s'\n", memcached_strerror(mc->st, rc));
        abort();
    }
    memcached_server_list_free(servers);
    return cast(void *, mc);
}
Ejemplo n.º 6
0
int main(int argc, char **argv) 
{
  memcached_server_st *servers = NULL; 
  memcached_st        *memc; 
  memcached_return     rc; 

  char  *key, *value;
  size_t value_length; 

  memc = memcached_create(NULL); 

  servers = memcached_server_list_append(servers, "localhost", 11211, &rc); 
  rc = memcached_server_push(memc, servers); 
  if (rc != MEMCACHED_SUCCESS) 
  {
    fprintf(stderr, "memcache: error: couldn't add server: %s\n.",
                                        memcached_strerror(memc, rc)); 
    memcached_free(memc); 
    return 1; 
  }

  if (argc == 3) // Insert (key, value) pair 
  {
    key = argv[1];
    value = argv[2]; 
    rc = memcached_set(memc, key, strlen(key), value, strlen(value), 
             (time_t)0, (uint32_t)0);

    if (rc != MEMCACHED_SUCCESS)
    {
      fprintf(stderr, "memcache: error: couldn't store key: %s\n.", 
           memcached_strerror(memc, rc)); 
      memcached_free(memc);
      return 2; 
    }
  }

  else if (argc == 2) // Get value given key
  {
    key = argv[1]; 
    value = memcached_get(memc, key, strlen(key), &value_length, 
                                                  (uint32_t)0, &rc); 
    if (rc != MEMCACHED_SUCCESS)
    {
      fprintf(stderr, "memcache: error: couldn't get key: %s.\n", 
                                        memcached_strerror(memc, rc)); 
      memcached_free(memc);
      return 3; 
    }
    printf("%s\n", value); 
  }

  else 
  {
    fprintf(stderr, "usage: memcache <key> [<value>]\n");
  }

  memcached_free(memc);
  return 0; 
}
Ejemplo n.º 7
0
memcached_st *php_memc_create_str (const char *str, size_t str_len)
{
#ifdef HAVE_LIBMEMCACHED_MEMCACHED
	return memcached (str, str_len);
#else
	memcached_return rc;
	memcached_st *memc;
	memcached_server_st *servers;

	memc = memcached_create(NULL);

	if (!memc) {
		return NULL;
	}

	servers = memcached_servers_parse (str);

	if (!servers) {
		memcached_free (memc);
		return NULL;
	}

	rc = memcached_server_push (memc, servers);
	memcached_server_free (servers);

	if (rc != MEMCACHED_SUCCESS) {
		memcached_free (memc);
		return NULL;
	}
	return memc;
#endif
}
static
memcached_st *s_init_mod_data (const memcached_server_list_st servers, zend_bool is_persistent)
{
	void *buffer;
	php_memcached_user_data *user_data;
	memcached_st *memc;

	buffer = pecalloc(1, sizeof(memcached_st), is_persistent);
	memc   = memcached_create (buffer);

	if (!memc) {
		php_error_docref(NULL, E_ERROR, "failed to allocate memcached structure");
		/* not reached */
	}

	memcached_set_memory_allocators(memc, s_pemalloc_fn, s_pefree_fn, s_perealloc_fn, s_pecalloc_fn, NULL);

	user_data                = pecalloc(1, sizeof(php_memcached_user_data), is_persistent);
	user_data->is_persistent = is_persistent;
	user_data->has_sasl_data = 0;
	user_data->lock_key      = NULL;
	user_data->is_locked     = 0;

	memcached_set_user_data(memc, user_data);
	memcached_server_push (memc, servers);
	memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_VERIFY_KEY, 1);
	return memc;
}
Ejemplo n.º 9
0
Memcache_Res CMemcache:: Memcac_Create(string strIp, unsigned int uPort)
{
	Memcache_Res mRes 		= MEMCACHED_SUCCESS;
	Memcache_Handler htMc		= NULL;
	memcached_server_st* pServ	= NULL;
	char pMem_Server_Addr[MEM_SERV_ADDR_SIZE]	= {0};

	if(strIp.empty() || 0 == uPort)
		return  static_cast<Memcache_Res>(MEMCACHED_INVALID_ARGUMENTS);

	// create memcache server address
	sprintf(pMem_Server_Addr, "%s:%d", strIp.c_str(), uPort);
	
	// create memcache
	htMc = memcached_create(NULL);

	pServ = memcached_servers_parse(pMem_Server_Addr);
	if((mRes = memcached_server_push(htMc, pServ)) != MEMCACHED_SUCCESS)
		return mRes;
	memcached_server_list_free(pServ);

	if((mRes = memcached_behavior_set(htMc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 0)) != MEMCACHED_SUCCESS)
		return mRes;

	// return
	if(NULL != m_hMc)
		Memcac_Free();

	pthread_mutex_lock(&m_hmc_mutex);
	m_hMc = htMc;
	pthread_mutex_unlock(&m_hmc_mutex);

	return MEMCACHED_SUCCESS;
}
Ejemplo n.º 10
0
int32_t CVDCMemcache::init_m_memc()
{
	//如果m_memc对象不为空则先清空
	if(NULL != m_memc)
	{
		memcached_free(m_memc);
		m_memc = NULL;
	}

	//重新创建m_memc对象
	m_memc = memcached_create(NULL);

	//创建失败则返回错误
	if(NULL == m_memc)
	{
		g_FrameLogEngine.WriteBaseLog( enmLogLevel_Error, "[%s:%d]Error: initialize m_memc failed! errorcode=0x%08X\n",
			__FILE__, __LINE__, E_DBPROXY_MALLOC_MEMC);
//		g_bMemcacheConnected = false;
		return E_DBPROXY_MALLOC_MEMC;
	}
	g_FrameLogEngine.WriteBaseLog( enmLogLevel_Notice, "[%s:%d]initialize m_memc success!\n", __FILE__, __LINE__);

	//设置为非阻塞
	memcached_behavior_set(m_memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1);

	return S_OK;
}
Ejemplo n.º 11
0
static mrb_value mrb_memcached_init(mrb_state *mrb, mrb_value self)
{
  mrb_memcached_data *data;
  memcached_server_st *msv;
  memcached_st *mst;
  memcached_return mrt;
  const char *servers;

  data = (mrb_memcached_data *)DATA_PTR(self);
  if (data) {
    mrb_free(mrb, data);
  }
  DATA_TYPE(self) = &mrb_memcached_data_type;
  DATA_PTR(self) = NULL;

  mrb_get_args(mrb, "z", &servers);

  mst = memcached_create(NULL);  
  msv = memcached_servers_parse(servers);
  mrt = memcached_server_push(mst, msv);
  if (mrt != MEMCACHED_SUCCESS) {
    mrb_raisef(mrb, E_RUNTIME_ERROR, "libmemcached error: %S"
      , mrb_str_new_cstr(mrb, memcached_strerror(mst, mrt)));
  }
  memcached_server_list_free(msv);

  data = (mrb_memcached_data *)mrb_malloc(mrb, sizeof(mrb_memcached_data));
  data->mst = mst;
  DATA_PTR(self) = data;

  return self;
}
Ejemplo n.º 12
0
void
cut_setup(void)
{
  GError *error = NULL;
  memcached_return rc;

  tmp_directory = g_build_filename(grn_test_get_base_dir(), "tmp", NULL);
  cut_remove_path(tmp_directory, NULL);
  if (g_mkdir_with_parents(tmp_directory, 0700) == -1) {
    cut_assert_errno();
  }

  egg = gcut_egg_new(GROONGA, "-s",
                     "-p", GROONGA_TEST_PORT,
                     cut_take_printf("%s%s%s",
                                     tmp_directory,
                                     G_DIR_SEPARATOR_S,
                                     "memcached.db"),
                     NULL);
  gcut_egg_hatch(egg, &error);
  gcut_assert_error(error);

  sleep(1); /* wait for groonga daemon */
  memc = memcached_create(NULL);
  memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);

  servers = memcached_servers_parse("localhost:" GROONGA_TEST_PORT);
  rc = memcached_server_push(memc, servers);

  cut_set_message("memcached server connect failed.");
  cut_assert_equal_int(MEMCACHED_SUCCESS, rc);

  memcached_flush(memc, 0); /* flush immediately for debug daemon */
}
Ejemplo n.º 13
0
Archivo: mmc_test.c Proyecto: ysl/util
/*
 * mmc_init()
 */
int mmc_init (char *server_list)
{
  memcached_server_st   *servers;
  memcached_return      ret;

  pmmc = memcached_create(NULL);
  if (!pmmc) {
    printf("Cannot create mmc object.\n");
    return -1;
  }

  /*
   * Add server.
   */
  servers = memcached_servers_parse(server_list);
  ret = memcached_server_push(pmmc, servers);
  if (ret != MEMCACHED_SUCCESS) {
    printf("Cannot push the mmc server list. [%s]\n",
           memcached_strerror(pmmc, ret));
    return -1;
  }

  /*
   * Set options.
   */
  memcached_behavior_set(pmmc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, 5000);
  memcached_behavior_set(pmmc, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, 5000);
  memcached_behavior_set(pmmc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1);

  memcached_server_list_free(servers);

  return 0;
}
Ejemplo n.º 14
0
/* added by Wakamori */
static void opt_logcached(int mode, const char *optstr)
{
#if defined(HAVE_LIBMEMCACHED)
	knh_syslog = memcached_syslog;
	knh_vsyslog = memcached_vsyslog;

	kbytes_t host_port = B(optstr);
	kbytes_t host = knh_bytes_head(host_port, ':');
	kint_t port;
	if (!knh_bytes_parseint(knh_bytes_next(host_port, ':'), &port)) {
		KNH_DIE("konoha: invalid arguments.");
	}
	memc = memcached_create(NULL);
	char tmp = host.buf[host.len];
	host.buf[host.len] = '\0';

	memcached_return rc;
	memcached_server_list_st servers;
	servers = memcached_server_list_append(NULL, host.text, port, &rc);
	if (rc != MEMCACHED_SUCCESS) {
		KNH_DIE("konoha: cannot append a memcached server.");
	}
	host.buf[host.len] = tmp;
	rc = memcached_server_push(memc, servers);
	if (rc != MEMCACHED_SUCCESS) {
		KNH_DIE("konoha: cannot push a memcached server.");
	}
	memcached_server_list_free(servers);
#else
	KNH_DIE("konoha: memcached library is not installed.");
#endif /* defined(HAVE_LIBMEMCACHED) */
}
Ejemplo n.º 15
0
/* create a memcached structure */
static memcached_st *memc_new()
{
  char config_string[1024];
  memcached_st *memc = NULL;
  memcached_server_st *servers = NULL;
  unsigned long long getter;
  memcached_return_t rc;

  pthread_mutex_lock (&printmutex);
  sprintf(config_string, "--SERVER=%s --BINARY-PROTOCOL", serverip);
  printf("config_string = %s\n", config_string);
  //memc = memcached(config_string, strlen(config_string));
  memc = memcached_create(NULL);
  servers = memcached_server_list_append(servers, serverip, 11211, &rc);
  rc = memcached_server_push(memc, servers);
  if (rc == MEMCACHED_SUCCESS)
	  fprintf(stderr, "Added server successfully\n");
  else
	  fprintf(stderr, "Couldnt add server: %s\n", serverip);

  getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
  printf("No block: %lld\n", getter);
  getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
  printf("Socket send size: %lld\n", getter);
  getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
  printf("Socket recv size: %lld\n", getter);
  memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);

  pthread_mutex_unlock (&printmutex);
  return memc;
}
Ejemplo n.º 16
0
    memcached_protocol_t(const char *conn_str) {

        memcached_create(&memcached);

        // NOTE: we don't turn on noreply behavior because the stress
        // client is designed to emulate real-world behavior, which
        // commonly requires a reply. In order to do this properly we
        // should pipeline sets according to batch factor, and then
        // wait for store notifications for the batch.

        //memcached_behavior_set(&memcached, MEMCACHED_BEHAVIOR_NOREPLY, 1);

        // Parse the host string
        char _host[MAX_HOST];
        strncpy(_host, conn_str, MAX_HOST);

        char *_port = NULL;

        _port = strchr(_host, ':');
        if(_port) {
            *_port = '\0';
            _port++;
        } else {
            fprintf(stderr, "Please use host string of the form host:port.\n");
            exit(-1);
        }

        int port = atoi(_port);

        // Connect to server
        memcached_server_add(&memcached, _host, port);
    }
Ejemplo n.º 17
0
 Memcache() 
   : 
     memc(),
     result()
 {
   memcached_create(&memc);
 }
Ejemplo n.º 18
0
int main(int argc, char **argv)
{
    if(argc < 2) {
        fprintf(stderr, "usage: %s 'script.k'\n", argv[0]);
        return 1;
    }
    memcached_st *mc = memcached_create(NULL);
    memcached_server_list_st servers;
    memcached_return_t rc;
    servers = memcached_server_list_append(NULL, "127.0.0.1", 11211, &rc);
    if(rc != MEMCACHED_SUCCESS) {
        fprintf(stderr, "memcached_server_list_append failed\n");
    }
    rc = memcached_server_Push(mc, servers);
    memcached_server_list_Free(servers);

    logpool_t *logpool = logpool_open_client(NULL, "0.0.0.0", 14801);

    size_t script_len;
    char *script = loadFile(argv[1], &script_len);
    memcached_set(mc, "dump_Init", strlen("dump_Init"), script, script_len, 0, 0);
    logpool_procedure(logpool, "dump", strlen("dump"));
    struct Log *logbuf = alloca(sizeof(struct Log) + 256);
    while(1) {
        if(logpool_client_get(logpool, logbuf, 256) == NULL) {
            break;
        }
        log_dump(stderr, "log=(", logbuf, ")\n", 1);
        usleep(1);
    }
    logpool_close(logpool);
    free(script);
    return 0;
}
Ejemplo n.º 19
0
int main(int argc, char* argv[])
{
	if(argc < 4) { usage(); }

	char* host = argv[1];

	unsigned short port = atoi(argv[2]);
	if(port == 0) { usage(); }

	uint32_t num = atoi(argv[3]);
	if(num == 0) { usage(); }

	memcached_st* mc = memcached_create(NULL);
	if(mc == NULL) { pexit("memcached_create"); }

	memcached_server_add(mc, host, port);

//	memcached_behavior_set(mc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);

	char kbuf[strlen(KEY_PREFIX) + 11];
	char vbuf[strlen(VAL_PREFIX) + 11];

while(1) {
	uint32_t i;
	for(i=0; i < num; ++i) {
		int klen = sprintf(kbuf, KEY_PREFIX "%d", i);
		int vlen = sprintf(vbuf, VAL_PREFIX "%d", i);
		printf("set '%s' = '%s'\n", kbuf, vbuf);
		memcached_set(mc, kbuf, klen, vbuf, vlen, 0, 0);
	}

	for(i=0; i < num; ++i) {
		int klen = sprintf(kbuf, KEY_PREFIX "%d", i);
		int vlen = sprintf(vbuf, VAL_PREFIX "%d", i);
		size_t vallen;
		uint32_t flags;
		memcached_return rc;
		char* val = memcached_get(mc, kbuf, klen, &vallen, &flags, &rc);
		if(!val) {
			fprintf(stderr, "** key '%s' not found **\n", kbuf);
		} else if(vallen != vlen || memcmp(val, vbuf, vlen) != 0) {
			fprintf(stderr, "** key '%s' not match ** '", kbuf);
			fwrite(val, vallen, 1, stderr);
			fprintf(stderr, "'\n");
		} else {
			printf("get '%s' = '", kbuf);
			fwrite(val, vallen, 1, stdout);
			printf("'\n");
		}
	}

	for(i=0; i < num; ++i) {
		int klen = sprintf(kbuf, KEY_PREFIX "%d", i);
		printf("delete '%s'\n", kbuf);
		memcached_delete(mc, kbuf, klen, 0);
	}
}

	return 0;
}
Ejemplo n.º 20
0
    Memc()
    {
        _memc= memcached_create(NULL);

        if (_memc == NULL)
        {
            throw "memcached_create() failed";
        }
    }
Ejemplo n.º 21
0
//
// Configurators, and Register.
// 
static void* config_create(apr_pool_t* p, char* path)
{
  resize_conf* conf = (resize_conf*)apr_palloc(p, sizeof(resize_conf));
  conf->enabled = FALSE;
  conf->expire = 600;
  conf->memc = memcached_create(NULL);

  return conf;
}
Ejemplo n.º 22
0
int main(int argc, char *argv[])
{
  memcached_st *memc;
  memcached_return rc;
  memcached_server_st *servers;

  options_parse(argc, argv);

  if (!opt_servers)
  {
    char *temp;

    if ((temp= getenv("MEMCACHED_SERVERS")))
      opt_servers= strdup(temp);
    else
    {
      fprintf(stderr, "No Servers provided\n");
      exit(1);
    }
  }

  memc= memcached_create(NULL);
  process_hash_option(memc, opt_hash);

  servers= memcached_servers_parse(opt_servers);
  memcached_server_push(memc, servers);
  memcached_server_list_free(servers);
  memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, opt_binary);
  
  while (optind < argc) 
  {
    if (opt_verbose) 
      printf("key: %s\nexpires: %llu\n", argv[optind], (unsigned long long)opt_expire);
    rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire);

    if (rc != MEMCACHED_SUCCESS) 
    {
      fprintf(stderr, "memrm: %s: memcache error %s", 
	      argv[optind], memcached_strerror(memc, rc));
      if (memc->cached_errno)
	fprintf(stderr, " system error %s", strerror(memc->cached_errno));
      fprintf(stderr, "\n");
    }

    optind++;
  }

  memcached_free(memc);

  if (opt_servers)
    free(opt_servers);
  if (opt_hash)
    free(opt_hash);

  return 0;
}
Ejemplo n.º 23
0
    Memc(in_port_t arg)
    {
        _memc= memcached_create(NULL);

        if (_memc == NULL)
        {
            throw "memcached_create() failed";
        }
        memcached_server_add(_memc, "localhost", arg);
    }
Ejemplo n.º 24
0
static VALUE mc_alloc(VALUE klass) {
  memcached_st *mc;
  VALUE obj;

  mc = memcached_create(NULL);
  memcached_behavior_set(mc, MEMCACHED_BEHAVIOR_CACHE_LOOKUPS, true);

  obj = Data_Wrap_Struct(klass, 0, mc_free, mc);
  return obj;
}
Ejemplo n.º 25
0
bool ProxyConnPool::Reset() {
  MCE_WARN("ProxyConnPool Reset(). index=" << proxy_index_);
  {
    IceUtil::Mutex::Lock lock(mutex_);
    if (reseting) {
      return true;
    }
    reseting = true;

    while (!memc_pool_.empty()) {
      memcached_free(memc_pool_.back());
      memc_pool_.pop_back();
    }
  }

  string endpoint;
  try {
    endpoint = MemcProxyAdapter::instance().GetEndpoint(proxy_index_);
  } catch(Ice::Exception& e) {
    MCE_WARN("MemcProxyAdapter::GetEndpoint Ice error : " << e);
  } catch (...) {
    MCE_WARN("MemcProxyAdapter::GetEndpoint error.");
  }

  size_t pos = endpoint.find(':');
  if (pos != string::npos) {
    proxy_addr_ = endpoint.substr(0, pos);
    proxy_port_ = boost::lexical_cast<unsigned short>(endpoint.c_str() + pos + 1);
  }

  MCE_INFO("MemcProxy " << proxy_index_ << " endpoint : " << endpoint);
  if (proxy_addr_.empty() || proxy_port_ <= 0) {
    reseting = false;
    return false;
  }

  memcached_return rc;
  memcached_server_st * servers = NULL;
  servers = memcached_server_list_append(servers, proxy_addr_.c_str(), proxy_port_, &rc);
  memcached_st * memc = memcached_create(NULL);
  rc = memcached_server_push(memc, servers); // 向服务集群添加server list
  if (rc != MEMCACHED_SUCCESS) {
    reseting = false;
    return false;
  }

  {
    IceUtil::Mutex::Lock lock(mutex_);
    memc_pool_.push_back(memc);
    reseting = false;
  }
  error_count_ = 0;

  return true;
}
Ejemplo n.º 26
0
Archivo: mcs.c Proyecto: MediaMath/moxi
mcs_st *lmc_create(mcs_st *ptr, const char *config) {
    assert(ptr);
    memset(ptr, 0, sizeof(*ptr));
    ptr->kind = MCS_KIND_LIBMEMCACHED;

    memcached_st *mst = memcached_create(NULL);
    if (mst != NULL) {
        memcached_behavior_set(mst, MEMCACHED_BEHAVIOR_NO_BLOCK, 1);
        memcached_behavior_set(mst, MEMCACHED_BEHAVIOR_KETAMA, 1);
        memcached_behavior_set(mst, MEMCACHED_BEHAVIOR_TCP_NODELAY, 1);

        memcached_server_st *mservers;

        mservers = memcached_servers_parse(config);
        if (mservers != NULL) {
            memcached_server_push(mst, mservers);

            ptr->data     = mst;
            ptr->nservers = (int) memcached_server_list_count(mservers);
            if (ptr->nservers > 0) {
                ptr->servers = calloc(sizeof(mcs_server_st), ptr->nservers);
                if (ptr->servers != NULL) {
                    for (int i = 0; i < ptr->nservers; i++) {
                        ptr->servers[i].fd = -1;
                    }

                    int j = 0;
                    for (; j < ptr->nservers; j++) {
                        strncpy(ptr->servers[j].hostname,
                                memcached_server_name(mservers + j),
                                sizeof(ptr->servers[j].hostname) - 1);
                        ptr->servers[j].port = (int) memcached_server_port(mservers + j);
                        if (ptr->servers[j].port <= 0) {
                            moxi_log_write("lmc_create failed, could not parse port: %s\n",
                                           config);
                            break;
                        }
                    }

                    if (j >= ptr->nservers) {
                        memcached_server_list_free(mservers);

                        return ptr;
                    }
                }
            }

            memcached_server_list_free(mservers);
        }
    }

    mcs_free(ptr);

    return NULL;
}
Ejemplo n.º 27
0
FRAME_DBPROXY_NAMESPACE_BEGIN

CVDCMemcache::CVDCMemcache()
{
	m_memc = memcached_create(NULL);
	m_servers = memcached_servers_parse(GET_MEMCACHECONFIG_INSTANCE().GetMemcacheIPAndPort());
	memcached_server_push(m_memc, m_servers);

	//设置为非阻塞
	memcached_behavior_set(m_memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1);
}
Ejemplo n.º 28
0
int memcached_connect_to_server(char *hostname, int port)
{
	memc = memcached_create(NULL);
	servers = memcached_server_list_append(NULL, hostname, port, NULL);
	memcached_return rc;
	rc = memcached_server_push(memc, servers);
	memcached_connected = 0;
	if (rc == MEMCACHED_SUCCESS) 
		return 0;
	return -1;
}
Ejemplo n.º 29
0
int main(int argc, char *argv[])
{
  memcached_st *memc;
  memcached_return_t rc;
  memcached_server_st *servers;

  options_parse(argc, argv);

  if (!opt_servers)
  {
    char *temp;

    if ((temp= getenv("MEMCACHED_SERVERS")))
      opt_servers= strdup(temp);
    else
    {
      fprintf(stderr, "No Servers provided\n");
      exit(1);
    }
  }

  memc= memcached_create(NULL);

  servers= memcached_servers_parse(opt_servers);
  memcached_server_push(memc, servers);
  memcached_server_list_free(servers);
  memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
                         (uint64_t) opt_binary);

  if (!initialize_sasl(memc, opt_username, opt_passwd))
  {
    memcached_free(memc);
    return 1;
  }

  rc = memcached_flush(memc, opt_expire);
  if (rc != MEMCACHED_SUCCESS)
  {
    fprintf(stderr, "memflush: memcache error %s",
	    memcached_strerror(memc, rc));
    if (memc->cached_errno)
      fprintf(stderr, " system error %s", strerror(memc->cached_errno));
    fprintf(stderr, "\n");
  }

  memcached_free(memc);

  free(opt_servers);

  shutdown_sasl();

  return 0;
}
Ejemplo n.º 30
0
int db_module_init() {
	memcached_return rc;
	//memcached_server_st *memcached_servers_parse (char *server_strings);
	internal_memc= memcached_create(NULL);
	syslog(LOG_INFO, "creating memcached server\n");
	internal_servers= memcached_server_list_append(internal_servers, "localhost"/* server name */, 11211/* port */, &rc); 
	rc= memcached_server_push(internal_memc, internal_servers);
	if (rc == MEMCACHED_SUCCESS)
		syslog(LOG_INFO, "Added server successfully\n");
	else
		syslog(LOG_ERR, "Couldn't add server: %s\n",memcached_strerror(internal_memc, rc));
	return 0;
}