Пример #1
0
void
test_memcached_replace(void)
{
  uint32_t flags;
  memcached_return rc;

  grn_test_memcached_assert_equal_rc(
    MEMCACHED_NOTSTORED,
    memcached_replace(memc, "key", 3, "value", 5, 0, 0xdeadbeefU),
    cut_message("memcached replace succeeded."));

  sleep(1);

  grn_test_memcached_assert(
    memcached_add(memc, "key", 3, "value", 5, 0, 0xdeadbeefU),
    cut_message("memcached add failed."));

  grn_test_memcached_assert(
    memcached_replace(memc, "key", 3, "new-value", 9, 0, 0xdeadbeefU),
    cut_message("memcached replace failed."));

  val1 = memcached_get(memc, "key", 3, &val1_len, &flags, &rc);
  grn_test_memcached_assert(rc, cut_message("memcached get failed."));
  cut_assert_equal_string("new-value", val1);
  cut_assert_equal_uint(0xdeadbeefU, flags);
}
Пример #2
0
void
test_memcached_replace(void)
{
  uint32_t flags;
  memcached_return rc;

  rc = memcached_replace(memc, "key", 3, "value", 5, 0, 0xdeadbeefU);
  cut_set_message("memcached replace succeeded.");
  /* TODO: fix rc after libmemcached fix */
  cut_assert_equal_int(MEMCACHED_PROTOCOL_ERROR, rc);

  sleep(1);

  rc = memcached_add(memc, "key", 3, "value", 5, 0, 0xdeadbeefU);
  cut_set_message("memcached add failed.");
  cut_assert_equal_int(MEMCACHED_SUCCESS, rc);

  rc = memcached_replace(memc, "key", 3, "new-value", 9, 0, 0xdeadbeefU);
  cut_set_message("memcached replace failed.");
  cut_assert_equal_int(MEMCACHED_SUCCESS, rc);

  val1 = memcached_get(memc, "key", 3, &val1_len, &flags, &rc);
  cut_set_message("memcached get failed.");
  cut_assert_equal_int(MEMCACHED_SUCCESS, rc);
  cut_assert_equal_string("new-value", val1);
  cut_assert_equal_uint(0xdeadbeefU, flags);
}
Пример #3
0
int
sstack_memcache_replace(memcached_st *memc,
					const char *key,
					const char *value,
					size_t valuelen,
					time_t expiration,
					uint32_t flags,
					log_ctx_t *ctx)
{

	memcached_return_t ret;

	if (NULL == memc || NULL == key) {
		sfs_log(ctx, SFS_ERR, "%s: Invalid parameters specified \n",
						__FUNCTION__);

		return -1;
	}

	ret = memcached_replace(memc, key, strlen(key), value, valuelen,
			expiration, flags);
	if (ret != MEMCACHED_SUCCESS) {
		sfs_log(ctx, SFS_ERR, "%s: Replacing value of key %s failed. "
						"Attempted new value = %s. Error = %d\n",
						__FUNCTION__, key, value, ret);

		return -ret;
	} else {
		sfs_log(ctx, SFS_INFO, "%s: Successfully replaced value of key %s ."
						"New value = %s \n", __FUNCTION__, key, value);

		return 0;
	}
}
Пример #4
0
/*doc Memcached replace(key, value[, expiration])
Asks memcached to store the value identified by the key,
but only if the server *does* already hold data for this key.
Returns true on success, false if there is already data for this key.
Otherwise raises an exception.
*/
IoObject *IoMemcached_replace(IoMemcached *self, IoObject *locals, IoMessage *m)
{
	IoSeq    *key   = IoMessage_locals_seqArgAt_(m, locals, 0);
	IoObject *value = IoMessage_locals_quickValueArgAt_(m, locals, 1);

	time_t expiration = IoMessage_argCount(m) == 3 ? IoMessage_locals_intArgAt_(m, locals, 2) : 0;

	uint32_t flags;
	size_t size;
	char *cvalue = IoMemcached_serialize(self, locals, value, &size, &flags);

	memcached_return_t rc;
	rc = memcached_replace(DATA(self)->mc,
		CSTRING(key), IOSEQ_LENGTH(key),
		cvalue, size,
		expiration, flags
	);

	free(cvalue);

	if(rc != MEMCACHED_SUCCESS && rc != MEMCACHED_NOTSTORED)
		IoState_error_(IOSTATE, m, memcached_strerror(DATA(self)->mc, rc));

	// MEMCACHED_NOTSTORED is a legitmate error in the case of a collision.
	if(rc == MEMCACHED_NOTSTORED)
		return IOSTATE->ioFalse;

	return IOSTATE->ioTrue; // MEMCACHED_SUCCESS
}
Пример #5
0
static bool HHVM_METHOD(Memcache, replace, const String& key,
                                           const Variant& var, int flag /*= 0*/,
                                           int expire /*= 0*/) {
  if (key.empty()) {
    raise_warning("Key cannot be empty");
    return false;
  }

  auto data = Native::data<MemcacheData>(this_);

  if (!hasAvailableServers(data)) {
    return false;
  }

  String serializedKey = memcache_prepare_key(key);
  std::vector<char> serialized = memcache_prepare_for_storage(data, var, flag);

  memcached_return_t ret = memcached_replace(&data->m_memcache,
                                             serializedKey.c_str(),
                                             serializedKey.length(),
                                             serialized.data(),
                                             serialized.size(),
                                             expire, flag);
  return (ret == MEMCACHED_SUCCESS);
}
void rate_limiter::update(const std::string &ip, int bytes)
{
  if (ptr)
  {
    time_t now = time(NULL);
    std::string key;
    state *sp;
    size_t length;
    uint32_t flags;
    memcached_return error;

    key = "cgimap:" + ip;

    retry:

    if (ptr &&
        (sp = (state *)memcached_get(ptr, key.data(), key.size(), &length, &flags, &error)) != NULL)
    {
      assert(length == sizeof(state));

      int64_t elapsed = now - sp->last_update;

      sp->last_update = now;

      if (elapsed * bytes_per_sec < sp->bytes_served)
      {
         sp->bytes_served = sp->bytes_served - elapsed * bytes_per_sec + bytes;
      }
      else
      {
         sp->bytes_served = bytes;
      }

      // should use CAS but it's a right pain so we'll wing it for now...
      memcached_replace(ptr, key.data(), key.size(), (char *)sp, sizeof(state), 0, 0);

      free(sp);
    }
    else
    {
      state s;

      s.last_update = now;
      s.bytes_served = bytes;

      if (memcached_add(ptr, key.data(), key.size(), (char *)&s, sizeof(state), 0, 0) == MEMCACHED_NOTSTORED)
      {
        goto retry;
      }
    }
  }

  return;
}
Пример #7
0
 /**
  * Replaces an object on the server. This method only succeeds
  * if the object is already present on the server.
  *
  * @param[in] key key of object to replace
  * @param[in[ value value to replace object with
  * @return true on success; false otherwise
  */
 bool replace(const std::string &key, const std::vector<char> &value)
 {
   if (key.empty() ||
       value.empty())
   {
     return false;
   }
   memcached_return rc= memcached_replace(&memc, key.c_str(), key.length(),
                                          &value[0], value.size(),
                                          0, 0);
   return (rc == MEMCACHED_SUCCESS);
 }
Пример #8
0
static int
exec_replace(memcached_st *mcd, const char *key, const char *value,
			 time_t expire, uint32_t flags)
{
	memcached_return_t  error = memcached_replace(mcd, key, strlen(key),
												  value, strlen(value),
												  expire, flags);
	if (error == MEMCACHED_SUCCESS)
		printf("replace: key=%s value=%s expire=%lu flags=%u\n",
			   key, value, expire, flags);
	else
		printf("replace: key=%s error=%s\n",
			   key, memcached_strerror(mcd, error));
	return 0;
}
Пример #9
0
/**
 * @function memcached.replace
 * 
 * ### Synopsis
 * 
 * var rc = memcached.replace(handle, key, value);
 * var rc = memcached.replace(handle, key, value, expiration);
 * var rc = memcached.replace(handle, key, value, expiration, flags);
 * 
 * Store information in memcached indexed by key.  
 * 
 * If an object identified by key already exists on the server, it is replaced.  Otherwise an error occurs.
 * 
 * @param {object} handle - handle to memcached connection.
 * @param {string} key - key of data to set in memcached.
 * @param {string} value - value of data to set in memcached.
 * @param {int} expiration - length of time value is valid (after this it will be removed from memcached automatically).
 * @param {int} flags - user defined integer value stored along with the value.
 * @return {int} rc - result code; 0 if no error, otherwise the error code.
 */
JSVAL _memcached_replace (JSARGS args) {
    HandleScope scope;
    M *handle = HANDLE(args[0]);
    String::Utf8Value key(args[1]);
    String::Utf8Value value(args[2]);
    time_t expiration = 0;
    if (args.Length() > 3) {
        expiration = args[3]->IntegerValue();
    }
    uint32_t flags = 0;
    if (args.Length() > 4) {
        flags = args[4]->IntegerValue();
    }
    return scope.Close(Integer::New(memcached_replace(handle, *key, strlen(*key), *value, strlen(*value), expiration, flags)));
}
Пример #10
0
bool c_Memcache::t_replace(CStrRef key, CVarRef var, int flag /*= 0*/,
                           int expire /*= 0*/) {
  if (key.empty()) {
    raise_warning("Key cannot be empty");
    return false;
  }

  String serialized = memcache_prepare_for_storage(var, flag);

  memcached_return_t ret = memcached_replace(&m_memcache,
                                             key.c_str(), key.length(),
                                             serialized.c_str(),
                                             serialized.length(),
                                             expire, flag);
  return (ret == MEMCACHED_SUCCESS);
}
Пример #11
0
static void rush_replace(int nr, char *buf, char seed, uint32_t key_len, uint32_t value_len, memcached_st *memc)
{
	int i;
	memcached_return_t rc;
	char *key, *value;

	for(i=0; i<nr; i++) {
		generate_key_value(i,buf,seed,&key,key_len,&value,value_len);

		rc=memcached_replace(memc,key,key_len,value,value_len,(time_t)0,(uint32_t)0);
		if(memcached_failed(rc)) {
			printf("memcached_replace() failed: %s\n",memcached_strerror(memc,rc));
		}
		PRINTF("i=%d, key_len=%ld, value_len=%ld\n",i,key_len,value_len);
	}
}
Пример #12
0
VALUE mc_replace(int argc, VALUE *argv, VALUE self) {
  memcached_st *mc;
  VALUE key, value, expiry, flags;
  static memcached_return_t result;

  Data_Get_Struct(self, memcached_st, mc);
  rb_scan_args(argc, argv, "22", &key, &value, &expiry, &flags);

  key = StringValue(key);
  if (!use_binary(mc)) key = escape_key(key, NULL);
  value = StringValue(value);

  result = memcached_replace(mc, RSTRING_PTR(key), RSTRING_LEN(key), RSTRING_PTR(value), RSTRING_LEN(value),
                         RTEST(expiry) ? NUM2UINT(expiry) : 0,
                         RTEST(flags)  ? NUM2UINT(flags)  : 0);

  if (result == MEMCACHED_SUCCESS) {
    return value;
  } else if(result == MEMCACHED_NOTSTORED) {
    return Qnil;
  } else {
    return throw_error(&result);
  }
}
Пример #13
0
int main(int argc, char *argv[])
{
  memcached_st *memc;
  memcached_return_t rc;
  memcached_server_st *servers;

  int return_code= 0;

  options_parse(argc, argv);
  initialize_sockets();

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

  if (!opt_servers)
  {
    char *temp;

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

  if (opt_servers)
    servers= memcached_servers_parse(opt_servers);
  else
    servers= memcached_servers_parse(argv[--argc]);

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

  while (optind < argc)
  {
    struct stat sbuf;
    int fd;
    char *ptr;
    ssize_t read_length;
    char *file_buffer_ptr;

    fd= open(argv[optind], O_RDONLY);
    if (fd < 0)
    {
      fprintf(stderr, "memcp: %s: %s\n", argv[optind], strerror(errno));
      optind++;
      continue;
    }

    (void)fstat(fd, &sbuf);

    ptr= rindex(argv[optind], '/');
    if (ptr)
      ptr++;
    else
      ptr= argv[optind];

    if (opt_verbose)
    {
      static const char *opstr[] = { "set", "add", "replace" };
      printf("op: %s\nsource file: %s\nlength: %zu\n"
	     "key: %s\nflags: %x\nexpires: %llu\n",
	     opstr[opt_method - OPT_SET], argv[optind], (size_t)sbuf.st_size,
	     ptr, opt_flags, (unsigned long long)opt_expires);
    }

    if ((file_buffer_ptr= (char *)malloc(sizeof(char) * (size_t)sbuf.st_size)) == NULL)
    {
      fprintf(stderr, "malloc: %s\n", strerror(errno));
      exit(1);
    }

    if ((read_length= read(fd, file_buffer_ptr, (size_t)sbuf.st_size)) == -1)
    {
      fprintf(stderr, "read: %s\n", strerror(errno));
      exit(1);
    }

    if (read_length != sbuf.st_size)
    {
      fprintf(stderr, "Failure reading from file\n");
      exit(1);
    }

    if (opt_method == OPT_ADD)
      rc= memcached_add(memc, ptr, strlen(ptr),
                        file_buffer_ptr, (size_t)sbuf.st_size,
			opt_expires, opt_flags);
    else if (opt_method == OPT_REPLACE)
      rc= memcached_replace(memc, ptr, strlen(ptr),
			    file_buffer_ptr, (size_t)sbuf.st_size,
			    opt_expires, opt_flags);
    else
      rc= memcached_set(memc, ptr, strlen(ptr),
                        file_buffer_ptr, (size_t)sbuf.st_size,
                        opt_expires, opt_flags);

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

      return_code= -1;
    }

    free(file_buffer_ptr);
    close(fd);
    optind++;
  }

  memcached_free(memc);

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

  return return_code;
}