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
}
Example #2
0
/** Create a new memcached handle
 *
 * @param ctx to allocate handle in.
 * @param instance data.
 */
static void *mod_conn_create(TALLOC_CTX *ctx, void *instance)
{
	rlm_cache_t			*inst = instance;
	rlm_cache_memcached_t		*driver = inst->driver;
	rlm_cache_memcached_handle_t	*mandle;

	memcached_st			*sandle;
	memcached_return_t		ret;

	sandle = memcached(driver->options, talloc_array_length(driver->options) -1);
	if (!sandle) {
		ERROR("rlm_cache_memcached: Failed creating memcached connection");

		return NULL;
	}

	ret = memcached_version(sandle);
	if (ret != MEMCACHED_SUCCESS) {
		ERROR("rlm_cache_memcached: Failed getting server info: %s: %s", memcached_strerror(sandle, ret),
		      memcached_last_error_message(sandle));
		memcached_free(sandle);
		return NULL;
	}

	mandle = talloc_zero(ctx, rlm_cache_memcached_handle_t);
	mandle->handle = sandle;
	talloc_set_destructor(mandle, _mod_conn_free);

	return mandle;
}
Example #3
0
static int cdb_init(knot_db_t **db, struct kr_cdb_opts *opts, knot_mm_t *pool)
{
	if (!db || !opts) {
		return kr_error(EINVAL);
	}

	struct memcached_cli *cli = malloc(sizeof(*cli));
	if (!cli) {
		return kr_error(ENOMEM);
	}
	memset(cli, 0, sizeof(*cli));

	/* Make sure we're running on binary protocol, as the
	 * textual protocol is broken for binary keys. */
	auto_free char *config_str = kr_strcatdup(2, opts->path, " --BINARY-PROTOCOL");
	cli->handle = memcached(config_str, strlen(config_str));
	if (!cli->handle) {
		free(cli);
		return kr_error(EIO);
	}

	/* Create result set */
	memcached_result_st *res = memcached_result_create(cli->handle, &cli->res);
	if (!res) {
		memcached_free(cli->handle);
		free(cli);
		return kr_error(ENOMEM);
	}

	*db = cli;
	return 0;
}
Example #4
0
 Memc(const std::string& arg)
 {
     _memc= memcached(arg.c_str(), arg.size());
     if (_memc == NULL)
     {
         throw "memcached() failed";
     }
 }
Example #5
0
struct storage_backend * init_storage_memcached(const char * connection_string) {

#ifndef HAVE_LIBMEMCACHED
    log_message(STORE_LOGLVL_ERR,"init_storage_memcached: Support for memcached has not been compiled into this program");
    return NULL;
#else
    struct storage_backend * store = malloc(sizeof(struct storage_backend));
    memcached_st * ctx;
    char * connection_str;
    int len;

    if (store == NULL) {
        log_message(STORE_LOGLVL_ERR,"init_storage_memcached: Failed to allocate memory for storage backend");
        return NULL;
    }
    //ctx = memcached(connection_str, strlen(connection_str));

    #ifdef USE_BINARY_PROTOCOL
        len = strlen("--server= --binary-protocol") + strlen(connection_string) - strlen("memcached://");
    #else
        len = strlen("--server=") + strlen(connection_string) - strlen("memcached://");
    #endif
    connection_str = malloc(len + 1);
    memcpy(connection_str,"--server=", strlen("--server="));
    memcpy(connection_str+strlen("--server="),connection_string + strlen("memcached://"), len-strlen("--server="));
    #ifdef USE_BINARY_PROTOCOL
      memcpy(connection_str+len-strlen(" --binary-protocol")," --binary-protocol", strlen(" --binary-protocol"));
    #endif
    connection_str[len] = 0;
 log_message(STORE_LOGLVL_DEBUG,"init_storage_memcached: initialization string: %s", connection_str);

    ctx = memcached(connection_str, len);

    if (ctx == NULL) {
        log_message(STORE_LOGLVL_ERR,"init_storage_memcached: Failed to create memcached ctx: %s", connection_str);
        free(connection_str);
        free(store);
        return NULL;
    }
    store->storage_ctx = ctx;

    store->tile_read = &memcached_tile_read;
    store->tile_stat = &memcached_tile_stat;
    store->metatile_write = &memcached_metatile_write;
    store->metatile_delete = &memcached_metatile_delete;
    store->metatile_expire = &memcached_metatile_expire;
    store->tile_storage_id = &memcached_tile_storage_id;
    store->close_storage = &memcached_close_storage;


    free (connection_str);
    return store;
#endif
}
bool Memcached::Init(const std::string& sSvrList)
{
	m_sSvrlist = sSvrList;	
	m_pMemc = memcached(sSvrList.c_str(), sSvrList.length());
	if (NULL == m_pMemc)
	{
		m_sErrMsg = "memcached failed."	;
		return false;
	}

	m_rc = memcached_behavior_set(m_pMemc, MEMCACHED_BEHAVIOR_DISTRIBUTION, MEMCACHED_DISTRIBUTION_CONSISTENT);
	if (MEMCACHED_SUCCESS != m_rc)
	{
		m_sErrMsg = "memcached_behavior_set failed.";
		return false;
	}

	return true;
}
Example #7
0
/* create a memcached structure */
static memcached_st *memc_new()
{
  char config_string[1024];
  memcached_st *memc = NULL;
  unsigned long long getter;

  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));

  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);

  pthread_mutex_unlock (&printmutex);
  return memc;
}
	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;
	}
memcached_st *
get_memcached(void *server_list)
{
	memcached_st *mc = pthread_getspecific(thread_key);
	if (!mc)
	{
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX > 0x00049000
		if (strstr(server_list, "--SERVER")) { 
			mc = memcached(server_list, strlen(server_list));
		} else {
#endif
			memcached_server_st *servers = memcached_servers_parse(server_list);
			mc = memcached_create(NULL);
			memcached_server_push(mc, (memcached_server_st *)servers);
			memcached_server_list_free(servers);
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX > 0x00049000			
		}
#endif
		pthread_setspecific(thread_key, mc);
	}
	return mc;
}
static ngx_int_t
ngx_captcha_access_filter_install(ngx_conf_t *cf) {

    ngx_http_handler_pt        *h = NULL;
    ngx_http_core_main_conf_t  *cmcf = NULL;

    cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);

    h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
    if (h == NULL) {
        return NGX_ERROR;
    }

    *h = ngx_captcha_access_filter_handler;
    
    // Configura o memcached
    memc = memcached(config_string, strlen(config_string));
    
    // FIXME Incluir código para liberar conexao do memcached
    //memcached_free(memc);
    

    return NGX_OK;
}
Example #11
0
/// Gets the set of replicas to use for a read or write operation for the
/// specified key.
const std::vector<memcached_st*>& MemcachedStore::get_replicas(const std::string& key,
                                                               Op operation)
{
  MemcachedStore::connection* conn = (connection*)pthread_getspecific(_thread_local);
  if (conn == NULL)
  {
    // Create a new connection structure for this thread.
    conn = new MemcachedStore::connection;
    pthread_setspecific(_thread_local, conn);
    conn->view_number = 0;
  }

  if (conn->view_number != _view_number)
  {
    // Either the view has changed or has not yet been set up, so set up the
    // connection and replica structures for this thread.
    for (size_t ii = 0; ii < conn->st.size(); ++ii)
    {
      memcached_free(conn->st[ii]);
      conn->st[ii] = NULL;
    }
    pthread_rwlock_rdlock(&_view_lock);

    LOG_DEBUG("Set up new view %d for thread", _view_number);

    // Create a set of memcached_st's one per server.
    conn->st.resize(_servers.size());

    for (size_t ii = 0; ii < _servers.size(); ++ii)
    {
      // Create a new memcached_st for this server.  Do not specify the server
      // at this point as memcached() does not support IPv6 addresses.
      LOG_DEBUG("Setting up server %d for connection %p (%s)", ii, conn, _options.c_str());
      conn->st[ii] = memcached(_options.c_str(), _options.length());
      LOG_DEBUG("Set up connection %p to server %s", conn->st[ii], _servers[ii].c_str());

      // Switch to a longer connect timeout from here on.
      memcached_behavior_set(conn->st[ii], MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, 50);

      // Connect to the server.  The address is specified as either <IPv4 address>:<port>
      // or [<IPv6 address>]:<port>.  Look for square brackets to determine whether
      // this is an IPv6 address.
      std::vector<std::string> contact_details;
      size_t close_bracket = _servers[ii].find(']');

      if (close_bracket == _servers[ii].npos)
      {
        // IPv4 connection.  Split the string on the colon.
        Utils::split_string(_servers[ii], ':', contact_details);
        if (contact_details.size() != 2)
        {
          LOG_ERROR("Malformed contact details %s", _servers[ii].c_str());
          break;
        }
      }
      else
      {
        // IPv6 connection.  Split the string on ']', which removes any white
        // space from the start and the end, then remove the '[' from the
        // start of the IP addreess string and the start of the ';' from the start
        // of the port string.
        Utils::split_string(_servers[ii], ']', contact_details);
        if ((contact_details.size() != 2) ||
            (contact_details[0][0] != '[') ||
            (contact_details[1][0] != ':'))
        {
          LOG_ERROR("Malformed contact details %s", _servers[ii].c_str());
          break;
        }

        contact_details[0].erase(contact_details[0].begin());
        contact_details[1].erase(contact_details[1].begin());
      }

      LOG_DEBUG("Setting server to IP address %s port %s",
                contact_details[0].c_str(),
                contact_details[1].c_str());
      int port = atoi(contact_details[1].c_str());
      memcached_server_add(conn->st[ii], contact_details[0].c_str(), port);
    }

    conn->read_replicas.resize(_vbuckets);
    conn->write_replicas.resize(_vbuckets);

    // Now set up the read and write replica sets.
    for (int ii = 0; ii < _vbuckets; ++ii)
    {
      conn->read_replicas[ii].resize(_read_replicas[ii].size());
      for (size_t jj = 0; jj < _read_replicas[ii].size(); ++jj)
      {
        conn->read_replicas[ii][jj] = conn->st[_read_replicas[ii][jj]];
      }
      conn->write_replicas[ii].resize(_write_replicas[ii].size());
      for (size_t jj = 0; jj < _write_replicas[ii].size(); ++jj)
      {
        conn->write_replicas[ii][jj] = conn->st[_write_replicas[ii][jj]];
      }
    }

    // Flag that we are in sync with the latest view.
    conn->view_number = _view_number;

    pthread_rwlock_unlock(&_view_lock);
  }

  // Hash the key and convert the hash to a vbucket.
  int hash = memcached_generate_hash_value(key.data(), key.length(), MEMCACHED_HASH_MD5);
  int vbucket = hash & (_vbuckets - 1);
  LOG_DEBUG("Key %s hashes to vbucket %d via hash 0x%x", key.c_str(), vbucket, hash);

  return (operation == Op::READ) ? conn->read_replicas[vbucket] : conn->write_replicas[vbucket];
}
Example #12
0
/// Gets the set of replicas to use for a read or write operation for the
/// specified vbucket.
const std::vector<memcached_st*>& BaseMemcachedStore::get_replicas(int vbucket,
                                                                   Op operation)
{
  BaseMemcachedStore::connection* conn = (connection*)pthread_getspecific(_thread_local);
  if (conn == NULL)
  {
    // Create a new connection structure for this thread.
    conn = new BaseMemcachedStore::connection;
    pthread_setspecific(_thread_local, conn);
    conn->view_number = 0;
  }

  if (conn->view_number != _view_number)
  {
    // Either the view has changed or has not yet been set up, so set up the
    // connection and replica structures for this thread.
    for (std::map<std::string, memcached_st*>::iterator it = conn->st.begin();
         it != conn->st.end();
         it++)
    {
      memcached_free(it->second);
      it->second = NULL;
    }
    pthread_rwlock_rdlock(&_view_lock);

    TRC_DEBUG("Set up new view %d for thread", _view_number);

    // Create a set of memcached_st's one per server.
    for (size_t ii = 0; ii < _servers.size(); ++ii)
    {
      // Create a new memcached_st for this server.  Do not specify the server
      // at this point as memcached() does not support IPv6 addresses.
      TRC_DEBUG("Setting up server %d for connection %p (%s)", ii, conn, _options.c_str());
      conn->st[_servers[ii]] = memcached(_options.c_str(), _options.length());
      TRC_DEBUG("Set up connection %p to server %s", conn->st[_servers[ii]], _servers[ii].c_str());

      // Switch to a longer connect timeout from here on.
      memcached_behavior_set(conn->st[_servers[ii]], MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, _max_connect_latency_ms);

      std::string server;
      int port;
      if (Utils::split_host_port(_servers[ii], server, port))
      {
        TRC_DEBUG("Setting server to IP address %s port %d",
                  server.c_str(),
                  port);
        memcached_server_add(conn->st[_servers[ii]], server.c_str(), port);
      }
      else
      {
        TRC_ERROR("Malformed host/port %s, skipping server", _servers[ii].c_str());
      }
    }

    conn->read_replicas.resize(_vbuckets);
    conn->write_replicas.resize(_vbuckets);

    // Now set up the read and write replica sets.
    for (int ii = 0; ii < _vbuckets; ++ii)
    {
      conn->read_replicas[ii].resize(_read_replicas[ii].size());
      for (size_t jj = 0; jj < _read_replicas[ii].size(); ++jj)
      {
        conn->read_replicas[ii][jj] = conn->st[_read_replicas[ii][jj]];
      }
      conn->write_replicas[ii].resize(_write_replicas[ii].size());
      for (size_t jj = 0; jj < _write_replicas[ii].size(); ++jj)
      {
        conn->write_replicas[ii][jj] = conn->st[_write_replicas[ii][jj]];
      }
    }

    // Flag that we are in sync with the latest view.
    conn->view_number = _view_number;

    pthread_rwlock_unlock(&_view_lock);
  }

  return (operation == Op::READ) ? conn->read_replicas[vbucket] : conn->write_replicas[vbucket];
}