Пример #1
0
int f_memcache_exists(lua_State *L) {
	char *r;
	int n = lua_gettop(L);
	struct memcache *mc;

	if (!lua_islightuserdata(L, lua_upvalueindex(1))) {
		lua_pushstring(L, "where is my userdata ?");
		lua_error(L);
	}

	mc = lua_touserdata(L, lua_upvalueindex(1));

	if (n != 1) {
		lua_pushstring(L, "expected one argument");
		lua_error(L);
	}

	if (!lua_isstring(L, 1)) {
		lua_pushstring(L, "argument has to be a string");
		lua_error(L);
	}

	if (NULL == (r = mc_aget(mc,
				 (char*) lua_tostring(L, 1), lua_strlen(L, 1)))) {

		lua_pushboolean(L, 0);
		return 1;
	}

	free(r);

	lua_pushboolean(L, 1);
	return 1;
}
Пример #2
0
int
main(int argc, char *argv[]) {
  struct memcache *mc = NULL;
  int i, ret;
  u_int32_t hash_pre, hash_post;
  void *v;

  mc = mc_new();
  if (mc == NULL)
    err(EX_OSERR, "Unable to allocate a new memcache object");

  mc_server_add4(mc, "localhost:11211");
  warnx("mc_add");

  mc_err_filter_del(MCM_ERR_LVL_INFO);
  mc_err_filter_del(MCM_ERR_LVL_NOTICE);

  ret = mc_add(mc, key, strlen(key), val, strlen(val), 0, 0);
  printf("add %d\n", ret);

  ret = mc_set(mc, key, strlen(key), val, strlen(val), 0, 0);
  hash_pre = mc_hash_key(val, strlen(val));
  printf("set %d\n", ret);

  for (i = 0; i < 10000; i++) {
    v = mc_aget(mc, key, strlen(key));
    if (v != NULL) {
      hash_post = mc_hash_key(v, strlen(v));
      if (hash_post != hash_pre) {
	printf("Hash pre (%u) != post (%u)\n", hash_pre, hash_post);
      }
      free(v);
    } else {
      printf("Return value null on iteration %d\n", i);
    }
  }

  mc_free(mc);

  return EX_OK;
}
Пример #3
0
int f_memcache_get_long(lua_State * L)
{
	char *r;
	int n = lua_gettop(L);

	struct memcache *mc;

	if (!lua_islightuserdata(L, lua_upvalueindex(1)))
	{
		lua_pushstring(L, "where is my userdata ?");
		lua_error(L);
	}

	mc = lua_touserdata(L, lua_upvalueindex(1));


	if (n != 1)
	{
		lua_pushstring(L, "expected one argument");
		lua_error(L);
	}

	if (!lua_isstring(L, 1))
	{
		lua_pushstring(L, "argument has to be a string");
		lua_error(L);
	}

	if (NULL == (r = mc_aget(mc, lua_tostring(L, 1), lua_strlen(L, 1))))
	{
		lua_pushnil(L);
		return 1;
	}

	lua_pushnumber(L, strtol(r, NULL, 10));

	free(r);

	return 1;
}