Beispiel #1
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);
}
Beispiel #2
0
/*
 * 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;
}
Beispiel #3
0
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;
}
Beispiel #4
0
IoObject *IoMemcached_rawClone(IoMemcached *proto)
{
	IoObject *self = IoObject_rawClonePrimitive(proto);
	IoObject_setDataPointer_(self, calloc(1, sizeof(IoMemcachedData)));

	DATA(self)->mc = memcached_create(NULL);
	memcached_behavior_set(DATA(self)->mc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1);
	memcached_behavior_set(DATA(self)->mc, MEMCACHED_BEHAVIOR_HASH, MEMCACHED_HASH_FNV1A_32);
	memcached_behavior_set(DATA(self)->mc, MEMCACHED_BEHAVIOR_DISTRIBUTION, MEMCACHED_DISTRIBUTION_CONSISTENT);
	memcached_behavior_set(DATA(self)->mc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 0);

	return self;
}
Beispiel #5
0
// return:
// 	0:succ	1:fail
int get_to_mc(char *mc_ip, int mc_port, char *mc_key, char *mc_value, size_t mc_value_size)
{
	size_t nval = 0;
    uint32_t flag = 0;
    char *result = NULL;
    int ret = 0;
    
    memcached_st        *memc = memcached_create(NULL);
    memcached_return    mrc;
    memcached_server_st *mc_servers;
    
    memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1);
    memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, mc_timeout);
    
    mc_servers= memcached_server_list_append(NULL, mc_ip, atoi(mc_port), &mrc);
    log_debug("memcached_server_list_append[%d]:%s:%d", mrc, mc_ip, mc_port);
    
    if (MEMCACHED_SUCCESS == mrc) {
    	mrc= memcached_server_push(memc, mc_servers);
    	memcached_server_list_free(mc_servers);
    	if (MEMCACHED_SUCCESS == mrc) {
    		result = memcached_get(memc, mc_key, strlen(mc_key), (size_t *)&nval, &flag, &mrc);
    		if (MEMCACHED_SUCCESS == mrc) {
    			log_debug("get mc key:%s val:%s succ", mc_key, result);
    			
    			snprintf(mc_value, mc_value_size, "%s", result);
    			
    			ret = 0;
    		} else {
    			log_error("get mc key:%s val:%s failed:%s", mc_key, mc_value, memcached_strerror(memc, mrc));
    			
    			ret = 1;
    		}
    		
    		if (result != NULL) {
    			free(result);
    			result = NULL;
    		}
    			
    		memcached_free(memc);

    		return ret; 
    	}
    } 
    
    log_error("set_to_mc:%s:%d connect fail:%s", mc_ip, mc_port, memcached_strerror(memc, mrc));
	
	memcached_free(memc);
	
	return 1;
}
Cache::Cache( const MC2String& hosts )
      : m_pimpl( new Implementation ) {
   m_pimpl->memc.reset( memcached_create( NULL ) );

   if ( m_pimpl->memc.get() == NULL ) {
      throw MemcachedException( "Couldn't create memcached_st structure" );
   }

   // Enable asynchronous IO mode
   memcached_return rc = 
      memcached_behavior_set( m_pimpl->memc.get(),
                              MEMCACHED_BEHAVIOR_NO_BLOCK,
                              1 /* 1 = enable */ );

   throwOnFail( rc, m_pimpl->memc.get(), "Couldn't set asynchronous IO mode" );

   uint32 timeout = Properties::getUint32Property( "MEMCACHED_TIMEOUT",
                                                   1000 // default, ms
                                                   );
   // Set poll timeout
   rc = memcached_behavior_set( m_pimpl->memc.get(),
                                MEMCACHED_BEHAVIOR_POLL_TIMEOUT,
                                timeout );

   throwOnFail( rc, m_pimpl->memc.get(), "Couldn't set poll timeout" );

   // Set connect timeout
   rc = memcached_behavior_set( m_pimpl->memc.get(),
                                MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT,
                                timeout );

   throwOnFail( rc, m_pimpl->memc.get(), "Couldn't set connect timeout" );

   // work around the fact that memcached_servers_parse 
   // doesn't take a const char*
   char* hostsDup = strdup( hosts.c_str() );
   memcached_server_st* serverList = memcached_servers_parse( hostsDup );
   free( hostsDup );

   if ( serverList == NULL ) {
      throw MemcachedException( "Couldn't parse server string" );
   }

   rc = memcached_server_push( m_pimpl->memc.get(), 
                               serverList );
   memcached_server_list_free( serverList );

   throwOnFail( rc, m_pimpl->memc.get(), "Couldn't add servers" );
}
Beispiel #7
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;
}
Beispiel #8
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 */
}
Beispiel #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;
}
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;
}
Beispiel #11
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;
}
Beispiel #12
0
/**
  @note This should be testing to see if the server really supports the binary protocol.
*/
static test_return_t pre_binary(memcached_st *memc)
{
    memcached_return_t rc= MEMCACHED_FAILURE;
    memcached_st *memc_clone;
    memcached_server_instance_st instance;

    memc_clone= memcached_clone(NULL, memc);
    test_true(memc_clone);
    // The memcached_version needs to be done on a clone, because the server
    // will not toggle protocol on an connection.
    memcached_version(memc_clone);

    instance= memcached_server_instance_by_position(memc_clone, 0);

    if (instance->major_version >= 1 && instance->minor_version > 2)
    {
        rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
        test_true(rc == MEMCACHED_SUCCESS);
        test_true(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1);
    }

    memcached_free(memc_clone);

    return rc == MEMCACHED_SUCCESS ? TEST_SUCCESS : TEST_SKIPPED;
}
Beispiel #13
0
void process_hash_option(memcached_st *memc, char *opt_hash)
{
  uint64_t set;
  memcached_return_t rc;

  if (opt_hash == NULL)
    return;

  set= MEMCACHED_HASH_DEFAULT; /* Just here to solve warning */
  if (!strcasecmp(opt_hash, "CRC"))
    set= MEMCACHED_HASH_CRC;
  else if (!strcasecmp(opt_hash, "FNV1_64"))
    set= MEMCACHED_HASH_FNV1_64;
  else if (!strcasecmp(opt_hash, "FNV1A_64"))
    set= MEMCACHED_HASH_FNV1A_64;
  else if (!strcasecmp(opt_hash, "FNV1_32"))
    set= MEMCACHED_HASH_FNV1_32;
  else if (!strcasecmp(opt_hash, "FNV1A_32"))
    set= MEMCACHED_HASH_FNV1A_32;
  else
  {
    fprintf(stderr, "hash: type not recognized %s\n", opt_hash);
    exit(1);
  }

  rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
  if (rc != MEMCACHED_SUCCESS)
  {
    fprintf(stderr, "hash: memcache error %s\n", memcached_strerror(memc, rc));
    exit(1);
  }
}
Beispiel #14
0
void set_mode(memcached_st *st){
	if (getenv("MEM_MODE") == NULL || strcmp(getenv("MEM_MODE"), "ASCII") != 0){
		puts("<Usando protocolo binario>");
		 memcached_behavior_set(st, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
	} else {
		puts("<Usando protoclo ASCII>");
	}
}
Beispiel #15
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;
}
Beispiel #16
0
static test_return_t set_udp_behavior_test(memcached_st *memc)
{

    memcached_quit(memc);
    memc->number_of_hosts= 0;
    memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, memc->distribution);
    test_true(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, 1) == MEMCACHED_SUCCESS);
    test_true(memc->flags.use_udp);
    test_true(memc->flags.no_reply);

    test_true(memcached_server_count(memc) == 0);

    memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP,0);
    test_true(! (memc->flags.use_udp));
    memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY,0);
    test_true(! (memc->flags.no_reply));

    return TEST_SUCCESS;
}
Beispiel #17
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;
}
Beispiel #18
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);
}
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;
}
void
vmod_behavior_set(struct sess *sp, struct vmod_priv *priv, const char *flag, const char *data)
{
	memcached_return rc;
	memcached_st *mc = get_memcached(priv->priv);
	if (!mc) return;

    int flag_i = str_to_mc_enum(flag);
    int data_i = str_to_mc_enum(data);

	rc = memcached_behavior_set(mc, flag_i, data_i);
}
rate_limiter::rate_limiter(const boost::program_options::variables_map &options)
{
  if (options.count("memcache") &&
      (ptr = memcached_create(NULL)) != NULL)
  {
    memcached_server_st *server_list;

    memcached_behavior_set(ptr, MEMCACHED_BEHAVIOR_NO_BLOCK, 1);
    memcached_behavior_set(ptr, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
//    memcached_behavior_set(ptr, MEMCACHED_BEHAVIOR_SUPPORT_CAS, 1);

    server_list = memcached_servers_parse(options["memcache"].as<std::string>().c_str());

    memcached_server_push(ptr, server_list);

    memcached_server_list_free(server_list);
  }
  else
  {
    ptr = NULL;
  }

  if (options.count("ratelimit"))
  {
    bytes_per_sec = options["ratelimit"].as<int>();
  }
  else
  {
    bytes_per_sec = 100 * 1024;
  }

  if (options.count("maxdebt"))
  {
    max_bytes = options["maxdebt"].as<int>() * 1024 * 1024;
  }
  else
  {
    max_bytes = 250 * 1024 * 1024;
  }
}
Beispiel #22
0
//init Server
int ssMemcached::initServer(char *pServerInfo, int nPort) {
    servers_ = memcached_server_list_append(servers_, pServerInfo, nPort, &retCode_);
    retCode_ = memcached_server_push(memc_, servers_);

    if (retCode_ == MEMCACHED_SUCCESS) {
        //fprintf(stderr,"Added server successfully\n");
    }
    else {
        fprintf(stderr, "Couldn't add server: %s\n", memcached_strerror(memc_, retCode_));
        return 0;
    }

    //connect timeout
    memcached_behavior_set(memc_, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, 100);
    //read timeout
    memcached_behavior_set(memc_, MEMCACHED_BEHAVIOR_RCV_TIMEOUT, 200);
    //write timeout
    memcached_behavior_set(memc_, MEMCACHED_BEHAVIOR_SND_TIMEOUT, 200);
    memcached_behavior_set(memc_, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, 400);

    return 1;
}
Beispiel #23
0
c_Memcache::c_Memcache(Class* cb) :
    ExtObjectData(cb), m_memcache(), m_compress_threshold(0),
    m_min_compress_savings(0.2) {
  memcached_create(&m_memcache);

  if (MEMCACHEG(hash_strategy) == "consistent") {
    // need to hook up a global variable to set this
    memcached_behavior_set(&m_memcache, MEMCACHED_BEHAVIOR_DISTRIBUTION,
                           MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA);
  } else {
    memcached_behavior_set(&m_memcache, MEMCACHED_BEHAVIOR_DISTRIBUTION,
                           MEMCACHED_DISTRIBUTION_MODULA);
  }

  if (MEMCACHEG(hash_function) == "fnv") {
    memcached_behavior_set(&m_memcache, MEMCACHED_BEHAVIOR_HASH,
                           MEMCACHED_HASH_FNV1A_32);
  } else {
    memcached_behavior_set(&m_memcache, MEMCACHED_BEHAVIOR_HASH,
                           MEMCACHED_HASH_CRC);
  }
}
Beispiel #24
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);
}
Beispiel #25
0
/**
 * @brief retry_cache Reconnect to the cache server.
 *
 * @param thr_arg Thread arg.
 */
void retry_cache(thr_arg_t *thr_arg)
{
    if(thr_arg->cache_conn != NULL)
        memcached_free(thr_arg->cache_conn);

    memcached_st *memc;
    memc= memcached_create(NULL);

    char mserver[32];
    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_server_list_free(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_arg->cache_conn = memc;

    evthr_set_aux(thr_arg->thread, thr_arg);
}
	hashWrapper::hashWrapper(const std::string &config, const vecHostInfo &hosts)
	{
		this->pMemc = memcached(config.c_str(), config.length());
		
		if(!this->pMemc )
			throw MemcacheConstructionException();

		serverCount = 0;
		for(citerVecHostInfo host = hosts.begin(); host != hosts.end(); host++)
			if(memcached_server_add(this->pMemc, (*host).first.c_str(), (*host).second) == MEMCACHED_SUCCESS)
			   serverCount++;
		//
		//  Do not change ordering of options below; it is significant
		//
		memcached_behavior_set(this->pMemc, MEMCACHED_BEHAVIOR_KETAMA, 0);
		memcached_behavior_set(this->pMemc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 0);
		memcached_behavior_set(this->pMemc, MEMCACHED_BEHAVIOR_KETAMA_HASH, MEMCACHED_HASH_DEFAULT);
		memcached_behavior_set(this->pMemc, MEMCACHED_BEHAVIOR_DISTRIBUTION, MEMCACHED_DISTRIBUTION_CONSISTENT);
		memcached_behavior_set(this->pMemc, MEMCACHED_BEHAVIOR_HASH, MEMCACHED_HASH_FNV1A_32);
	
		this->hashType = HASHKIT_HASH_DEFAULT;
	}
Beispiel #27
0
static VALUE mc_initialize(VALUE self, VALUE opts) {
  memcached_st *mc;
  VALUE servers_aryv, prefixv, hashv, distributionv;

  Data_Get_Struct(self, memcached_st, mc);
  hashv         = rb_hash_aref(opts, sym_hash);
  distributionv = rb_hash_aref(opts, sym_distribution);
  prefixv       = rb_hash_aref(opts, sym_prefix);
  servers_aryv  = rb_hash_aref(opts, sym_servers);

  if (!NIL_P(hashv)) {
    memcached_behavior_set(mc, MEMCACHED_BEHAVIOR_HASH,        hash_behavior(hashv));
    memcached_behavior_set(mc, MEMCACHED_BEHAVIOR_KETAMA_HASH, hash_behavior(hashv));
  }

  if (!NIL_P(distributionv))
    memcached_behavior_set_distribution(mc, distribution_behavior(distributionv));

  if (RTEST( rb_hash_aref(opts, sym_ketama) ))
    memcached_behavior_set(mc, MEMCACHED_BEHAVIOR_KETAMA, true);

  if (RTEST( rb_hash_aref(opts, sym_ketama_weighted) ))
    memcached_behavior_set(mc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, true);

  if (RTEST( rb_hash_aref(opts, sym_hash_with_prefix) ))
    memcached_behavior_set(mc, MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY, true);

  if (RTEST( rb_hash_aref(opts, sym_binary) ))
    memcached_behavior_set(mc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, true);

  if (!NIL_P(prefixv))
    memcached_callback_set(mc, MEMCACHED_CALLBACK_PREFIX_KEY, StringValuePtr(prefixv));

  if (!NIL_P(servers_aryv)) {
    char* server;
    int i;

    for (i = 0; i < RARRAY_LEN(servers_aryv); i++) {
      server    = StringValuePtr(RARRAY_PTR(servers_aryv)[i]);
      memcached_server_push(mc, memcached_servers_parse(server));
    }
  } else {
    VALUE hostv, portv, weightv;
    char* host;
    int   port, weight;

    hostv   = rb_hash_aref(opts, sym_host);
    portv   = rb_hash_aref(opts, sym_port);
    weightv = rb_hash_aref(opts, sym_weight);
    host    = StringValuePtr(hostv);
    port    = NIL_P(portv) ? MEMCACHED_DEFAULT_PORT : NUM2INT(portv);
    weight  = NIL_P(weightv) ? 0 : NUM2INT(weightv);

    memcached_server_add_with_weight(mc, StringValuePtr(hostv), port, weight);
  }

  return self;
}
static mrb_value mrb_memcached_behavior_set(mrb_state *mrb, mrb_value self)
{
  mrb_memcached_data *data = DATA_PTR(self);
  uint64_t fdata = 1;
  memcached_behavior_t flag;
  memcached_return mrt;

  mrb_get_args(mrb, "i|i", &flag, &fdata);
  mrt = memcached_behavior_set(data->mst, flag, fdata);
  if (mrt != MEMCACHED_SUCCESS) {
    return mrb_nil_value();
  }
  return mrb_fixnum_value(mrt);
}
/**
 * Initialize children
 */
static int child_init(int rank)
{

	memcached_return  rc;
	memcached_server_st *server_list;

	mem_server *cur;

	if(rank == PROC_MAIN || rank == PROC_TCP_MAIN)
	{
		return 0;
	}

	
	/* for each cache system and each child initialize the
	*  memcached_st structure that was previously allocated
	*/

	cur = servers;

	while(cur)
	{
		cur->memc = memcached_create(cur->memc);
		server_list = memcached_servers_parse( cur->servers );
		rc = memcached_server_push(cur->memc, server_list);

		if( rc != MEMCACHED_SUCCESS)
		{
			LM_ERR("Push:%s\n",memcached_strerror(cur->memc,rc));
			return -1;
		}


		rc = memcached_behavior_set(cur->memc,
			MEMCACHED_BEHAVIOR_NO_BLOCK,1);

		if( rc != MEMCACHED_SUCCESS)
		{
			LM_ERR("Behavior Set:%s\n",
				memcached_strerror(cur->memc,rc));
			return -1;
		}

		cur = cur->next;
	}

	return 0;
}
Beispiel #30
0
bool
Server::init()
{
    if(m_server_type == SERVER_TYPE_USER){
        m_com_contacts = new CompanyContactsKeeper(m_mongodb_addr,m_company_list_filename);
        if(m_com_contacts->init()==false){
            return false;
        }
    }
    if(m_server_type == SERVER_TYPE_JOB){
        m_com_score = new CompanyScoreKeeper(m_company_score_filename);
        if(m_com_score->init()==false){
            return false;
        }
    }
    m_tag_scorer = new TagScorer(1.0);
    memcached_create(&m_memcached_conn);
    memcached_behavior_set(&m_memcached_conn,MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,1);
    //memcached_behavior_set(&m_memcached_conn, MEMCACHED_BEHAVIOR_USE_UDP,1);
    if(m_memcache_addr!=""){
        memcached_server_st *servers;
        servers= memcached_servers_parse(m_memcache_addr.c_str());
        memcached_return_t rc = memcached_server_push(&m_memcached_conn, servers);
        memcached_server_free(servers);
        if(rc!=MEMCACHED_SUCCESS){
            TB_ERROR("connect to memcached failed, error="<<memcached_strerror(NULL, rc));
            return false;
        }else{
            TB_INFO("connected to memcached on "<<m_memcache_addr);
        }
    }
    pthread_mutex_init(&m_cache_lock,0);
    pthread_mutex_init(&m_db_lock,0);
    pthread_cond_init(&m_db_cond,0);
    for(size_t i=0;i<db_pool_size;i++){
        m_dbs.push_back(new Xapian::Database(m_index_dir));
        m_dbs_busy.push_back(false);
        m_dbs_last_reload_ts.push_back(time(0));
    }
    TB_INFO("database doccount: "<<m_dbs[0]->get_doccount());
    Session* new_session = create_session();
    new_session->init();
    acceptor_.async_accept(new_session->socket(),
		boost::bind(&Server::handle_accept, this, new_session,
		boost::asio::placeholders::error));
   return true; 
}