bool c_Memcache::t_addserver(const String& host, int port /* = 11211 */, bool persistent /* = false */, int weight /* = 0 */, int timeout /* = 0 */, int retry_interval /* = 0 */, bool status /* = true */, const Variant& failure_callback /* = null_variant */, int timeoutms /* = 0 */) { memcached_return_t ret; if (!host.empty() && !strncmp(host.c_str(), "unix://", sizeof("unix://") - 1)) { const char *socket_path = host.substr(sizeof("unix://") - 1).c_str(); ret = memcached_server_add_unix_socket_with_weight(&m_memcache, socket_path, weight); } else { ret = memcached_server_add_with_weight(&m_memcache, host.c_str(), port, weight); } if (ret == MEMCACHED_SUCCESS) { return true; } return false; }
bool c_Memcache::t_addserver(CStrRef host, int port /* = 11211 */, bool persistent /* = false */, int weight /* = 0 */, int timeout /* = 0 */, int retry_interval /* = 0 */, bool status /* = true */, CVarRef failure_callback /* = null_variant */, int timeoutms /* = 0 */) { memcached_return_t ret; if (!host.empty() && host[0] == '/') { ret = memcached_server_add_unix_socket_with_weight(&m_memcache, host.c_str(), weight); } else { ret = memcached_server_add_with_weight(&m_memcache, host.c_str(), port, weight); } if (ret == MEMCACHED_SUCCESS) { return true; } return false; }
memcached_return memcached_server_add_unix_socket(memcached_st *ptr, const char *filename) { return memcached_server_add_unix_socket_with_weight(ptr, filename, 0); }
int main(int argc, char * const argv[]) { memcached_st *mcd; in_port_t port; uint32_t weight; int num_conns = 0; int retval; int code; mcd = memcached_create(NULL); if (!mcd) { fprintf(stderr, "memcached_create(NULL) : %s\n", strerror(errno)); return 1; } while ((code = getopt(argc, argv, "c:vh")) > 0) { memcached_return_t rc; switch (code) { case 'c': if (strncmp(optarg, "tcp://", 6) == 0) { parse_connection(optarg+6, &port, &weight); rc = memcached_server_add_with_weight(mcd, optarg+6, port, weight); if (rc != MEMCACHED_SUCCESS) { fprintf(stderr, "failed to add tcp://%s:%d/%u\n", optarg+6, port, weight); return 1; } } else if (strncmp(optarg, "udp://", 6) == 0) { parse_connection(optarg+6, &port, &weight); rc = memcached_server_add_udp_with_weight(mcd, optarg+6, port, weight); if (rc != MEMCACHED_SUCCESS) { fprintf(stderr, "failed to add udp://%s:%d/%u\n", optarg+6, port, weight); return 1; } } else if (strncmp(optarg, "unix://", 7) == 0) { parse_connection(optarg+7, NULL, &weight); rc = memcached_server_add_unix_socket_with_weight(mcd, optarg+7, weight); if (rc != MEMCACHED_SUCCESS) { fprintf(stderr, "failed to add unix://%s/%u\n", optarg+7, weight); return 1; } } else { parse_connection(optarg, &port, &weight); rc = memcached_server_add_with_weight(mcd, optarg, port, weight); if (rc != MEMCACHED_SUCCESS) { fprintf(stderr, "failed to add tcp://%s:%d/%u\n", optarg, port, weight); return 1; } } num_conns++; break; case 'v': verbose = true; break; default: fprintf(stderr, "usage: %s [options] [<commands>]\n" "\n" "[options]\n" " -c <server> server to be connected\n" " [tcp://]<host>[:<port>][/<weight>]\n" " udp://<host>[:<port>][/<weight>]\n" " unix://<path>[/<weight>]\n" "\n" "[<command>]\n" " get <key>\n" " add <key> <value> [<expire> [<flags>]]\n" " set <key> <value> [<expire> [<flags>]]\n" " replace <key> <value> [<expire> [<flags>]]\n" " append <key> <value> [<expire> [<flags>]]\n" " prepend <key> <value> [<expire> [<flags>]]\n" " cas <key> <value> <cas> [<expire> [<flags>]]\n" " delete <key> [<expire>]\n" " incr <key>\n" " decr <key>\n" " flush [<when>]\n" " ----\n" " simple_bench [<scale>]\n", argv[0]); return 1; } } /* * If no server specified, a default is implicitly used */ if (num_conns == 0) { if (memcached_server_add(mcd, "localhost", 11211) != MEMCACHED_SUCCESS) { fprintf(stderr, "failed to add tcp://localhost:11211\n"); return 1; } } if (optind == argc) { char buffer[10240]; char **cmds = NULL; int n_cmds = 0; while(fgets(buffer, sizeof(buffer), stdin) != NULL) { char *tok = strtok(buffer, " \t\n"); int index = 0; while (tok != NULL) { if (index == n_cmds) { n_cmds = (n_cmds ? 2 * n_cmds : 10); cmds = realloc(cmds, sizeof(char *) * n_cmds); if (!cmds) { fprintf(stderr, "realloc() failed: %d(%s)\n", errno, strerror(errno)); return 1; } } cmds[index++] = tok; tok = strtok(NULL, " \t\n"); } if (index == 0) continue; retval = exec_command(mcd, index, cmds); if (retval != 0) return retval; } } else { retval = exec_command(mcd, argc - optind, argv + optind); } return retval; }