bool https_client::connect_server(const acl::string& server_addr, acl::http_client& client) { // 先查本地映射表中有没有映射项 master_service& ms = acl::singleton2<master_service>::get_instance(); const char* addr = ms.get_addr(server_addr.c_str()); if (addr == NULL) addr = server_addr.c_str(); if (client.open(addr, 60, 60, true) == false) { out_.format("connect server %s error", addr); return false; } else logger_debug(DEBUG, 1, "connect server ok"); if (ssl_conf_) { logger_debug(DEBUG, 1, "begin open ssl"); acl::polarssl_io* ssl = new acl::polarssl_io(*ssl_conf_, false); if (client.get_stream().setup_hook(ssl) == ssl) { out_.puts("open ssl client error"); ssl->destroy(); return false; } else logger_debug(DEBUG, 1, "open ssl ok"); } return true; }
static bool test_bitop_xor(acl::redis_string& option, int n) { const char* keys[3]; acl::string key, key1, key2, key3; int ret; for (int i = 0; i < n; i++) { key.format("bit_%s_%d", __keypre.c_str(), i); key1.format("bit_%s_%d", __keypre.c_str(), i % 1); key2.format("bit_%s_%d", __keypre.c_str(), i % 2); key3.format("bit_%s_%d", __keypre.c_str(), i % 3); keys[0] = key1.c_str(); keys[1] = key2.c_str(); keys[2] = key3.c_str(); option.reset(); ret = option.bitop_xor(key.c_str(), keys, 3); if (ret < 0) { printf("bitop_xor error, key: %s\r\n", key.c_str()); return false; } else if (i < 10) printf("bitop_xor ok, key: %s, bits: %u\n", key.c_str(), ret); } return true; }
static bool test_smove(acl::redis_set& redis, int n) { acl::string src_key, dst_key; acl::string member; int ret; for (int i = 0; i < n; i++) { src_key.format("%s_%d", __keypre.c_str(), i); dst_key.format("dest_%s_%d", __keypre.c_str(), i); for (int j = 0; j < 1000; j++) { member.format("member_%d", j); redis.clear(); ret = redis.smove(src_key.c_str(), dst_key.c_str(), member.c_str()); if (ret < 0) { printf("smove error, src: %s, des: %s\r\n", src_key.c_str(), dst_key.c_str()); return false; } else if (j * i >= 100) continue; printf("smove ok, src: %s, dst: %s, member:%s, ret: %d\r\n", src_key.c_str(), dst_key.c_str(), member.c_str(), ret); } } return true; }
static bool test_qpeek(acl::disque& cmd, int i) { int count = 1; cmd.clear(); const std::vector<acl::disque_job*>* jobs = cmd.qpeek(__queue.c_str(), count); if (jobs == NULL) { printf("qpeek queue: %s error: %s\r\n", __queue.c_str(), cmd.result_error()); return false; } else if (i >= 10) return true; printf("qpeek queue: %s ok\r\n", __queue.c_str()); std::vector<acl::disque_job*>::const_iterator cit; for (cit = jobs->begin(); cit != jobs->end(); ++cit) { printf("\tid: %s\r\n", (*cit)->get_id()); printf("\tqueue: %s\r\n", (*cit)->get_queue()); printf("\tjob: %s\r\n", (*cit)->get_body().c_str()); } return true; }
qitem(const acl::string& addr, const acl::string& msg) { time_t now = time(NULL); char buf[128]; acl::rfc822 rfc; rfc.mkdate_cst(now, buf, sizeof(buf)); msg_.format("%s: %s--%s\r\n", buf, addr.c_str(), msg.c_str()); }
bool mem_cache::set(const acl::string& key, const void* dat, size_t dlen, time_t timeout, unsigned short flags) { bool has_tried = false; struct iovec v[4]; m_line.format("set %s %u %d %d\r\n", key.c_str(), flags, (int) timeout, (int) dlen); AGAIN: if (open() == false) return (false); v[0].iov_base = (void*) m_line.c_str(); v[0].iov_len = m_line.length(); v[1].iov_base = (void*) dat; v[1].iov_len = dlen; v[2].iov_base = (void*) "\r\n"; v[2].iov_len = 2; if (m_conn->writev(v, 3) < 0) { close(); if (m_retry && !has_tried) { has_tried = true; goto AGAIN; } m_ebuf.format("write set(%s) error", key.c_str()); return (false); } if (m_conn->gets(m_line) == false) { close(); if (m_retry && !has_tried) { has_tried = true; goto AGAIN; } m_ebuf.format("reply for set(%s) error", key.c_str()); return (false); } if (m_line != "STORED") { close(); if (m_retry && !has_tried) { has_tried = true; goto AGAIN; } m_ebuf.format("reply(%s) for set(%s) error", m_line.c_str(), key.c_str()); return (false); } return (true); }
static bool test_qlen(acl::disque& cmd, int i) { cmd.clear(); int ret = cmd.qlen(__queue.c_str()); if (ret < 0) { printf("qlen queue: %s error: %s\r\n", __queue.c_str(), cmd.result_error()); return false; } else if (i < 10) printf("qlen: %d, queue: %s\r\n", ret, __queue.c_str()); return true; }
void redis_commands::add_cmdline(acl::string& line, size_t i) { std::vector<acl::string>& tokens = line.split2(" \t|:,;"); if (tokens.size() < 3) { logger_warn("skip line(%d): %s", (int) i, line.c_str()); return; } acl::string cmd(tokens[0]); cmd.upper(); if (cmd == "ALL") { all_cmds_perm_ = tokens[2]; all_cmds_perm_.lower(); if (all_cmds_perm_ == "warn" || all_cmds_perm_ == "no") return; return; } REDIS_CMD redis_cmd; redis_cmd.cmd = cmd; redis_cmd.broadcast = tokens[1].equal("yes", false) ? true : false; redis_cmd.perm = tokens[2]; redis_cmd.perm.lower(); if (redis_cmd.perm != "yes" && redis_cmd.perm != "warn") redis_cmd.perm = "no"; redis_cmds_[cmd] = redis_cmd; }
static bool test_hkeys(acl::redis_hash& redis, int n) { acl::string key; std::vector<acl::string> attrs; for (int i = 0; i < n; i++) { key.format("%s_%d", __keypre.c_str(), i); attrs.clear(); redis.clear(); if (redis.hkeys(key.c_str(), attrs) == false) { printf("hkeys error, key: %s\r\n", key.c_str()); return false; } else if (i >= 10) continue; printf("hkeys ok, key: %s\r\n", key.c_str()); std::vector<acl::string>::const_iterator cit; for (cit = attrs.begin(); cit != attrs.end(); ++cit) { if (cit != attrs.begin()) printf(", "); printf("%s", (*cit).c_str()); } printf("\r\n"); } return true; }
static bool test_hincrbyfloat(acl::redis_hash& redis, int n) { acl::string key, attr; double result; for (int i = 0; i < n; i++) { key.format("hincrbyfloat_%s_%d", __keypre.c_str(), i); attr.format("attr1"); redis.clear(); if (redis.hincrbyfloat(key.c_str(), attr.c_str(), 8.8, &result) == false) { printf("hincrbyfloat error, key: %s\r\n", key.c_str()); return false; } else if (i >= 10) continue; printf("hincrbyfloat ok, key: %s, attr: %s, result: %.2f\r\n", key.c_str(), attr.c_str(), result); } return true; }
static bool test_sdiffstore(acl::redis_set& redis, int n) { acl::string key, dest_key("set_dest_key"); std::vector<acl::string> keys; for (int i = 0; i < 10; i++) { key.format("%s_%d", __keypre.c_str(), i); keys.push_back(key); } for (int i = 0; i < n; i++) { redis.clear(); int ret = redis.sdiffstore(dest_key.c_str(), keys); if (ret < 0) { printf("sdiffstore error, dest: %s\r\n", dest_key.c_str()); return false; } else if (i >= 10) continue; printf("sdiffstore ok, dest: %s, count: %d\r\n", dest_key.c_str(), ret); } return true; }
static bool test_sadd(acl::redis_set& redis, int n) { acl::string key; std::vector<acl::string> members; acl::string member; members.reserve(1000); for (int j = 0; j < 1000; j++) { member.format("member_%d", j); members.push_back(member); } for (int i = 0; i < n; i++) { key.format("%s_%d", __keypre.c_str(), i); redis.clear(); int ret = redis.sadd(key, members); if (ret < 0) { printf("sadd key: %s error\r\n", key.c_str()); return false; } else if (i >= 10) continue; printf("sadd ok, key: %s, ret: %d\r\n", key.c_str(), ret); } return true; }
static bool test_zscore(acl::redis_zset& redis, int n) { acl::string key; acl::string member; bool ret; double result; for (int i = 0; i < n; i++) { key.format("%s_%d", __keypre.c_str(), i); for (int j = 0; j < 1000; j++) { member.format("member_%d", j); redis.clear(); ret = redis.zscore(key.c_str(), member.c_str(), result); if (ret == false) { printf("zscore error, key: %s\r\n", key.c_str()); return false; } else if (j > 0 && j * i < 100) printf("zscore ok, key: %s, member:%s, " "score: %.2f\r\n", key.c_str(), member.c_str(), result); } } return true; }
static bool test_get(acl::redis_string& option, int n) { acl::string key; acl::string value; for (int i = 0; i < n; i++) { key.format("%s_%d", __keypre.c_str(), i); //key.format("key1_%s_%d", __keypre.c_str(), i); value.clear(); option.reset(); if (option.get(key.c_str(), value) == false) { printf("get key: %s\r\n", key.c_str()); return false; } else if (i < 10) printf("key: %s, value: %s, len: %d\r\n", key.c_str(), value.c_str(), (int) value.length()); } return true; }
static bool test_zrangebylex(acl::redis_zset& redis, int n) { acl::string key; const char* min = "[aaa", *max = "(g"; std::vector<acl::string> result; for (int i = 0; i < n; i++) { key.format("%s_%d", __keypre.c_str(), i); redis.clear(); int ret = redis.zrangebylex(key.c_str(), min, max, &result); if (ret < 0) { printf("zrangebylex error(%s), key: %s\r\n", redis.result_error(), key.c_str()); return false; } if (i >= 10) { result.clear(); continue; } std::vector<acl::string>::const_iterator cit; for (cit = result.begin(); cit != result.end(); ++cit) { if (cit != result.begin()) printf(", "); printf("%s", (*cit).c_str()); } printf("\r\n"); } return true; }
static bool test_getbit(acl::redis_string& option, int n) { acl::string key; unsigned off = 5; int bit; for (int i = 0; i < n; i++) { key.format("bit_%s_%d", __keypre.c_str(), i); option.reset(); if (option.getbit(key.c_str(), off, bit) == false) { printf("getbit error, key: %s, off: %u\r\n", key.c_str(), off); return false; } else if (i >= 10) continue; printf("getbit ok, key: %s, off: %d, bit: %d\r\n", key.c_str(), off, bit); } return true; }
static bool test_getrange(acl::redis_string& option, int n) { acl::string key, value; int start = 5, end = 10; for (int i = 0; i < n; i++) { key.format("%s_%d", __keypre.c_str(), i); value.clear(); option.reset(); if (option.getrange(key, start, end, value) == false) { printf("getrange error, key: %s, start: %d, end: %d\r\n", key.c_str(), start, end); return false; } else if (i >= 10) continue; printf("getrange ok, key: %s, start: %d, end: %d, value: %s\r\n", key.c_str(), start, end, value.c_str()); } return true; }
static bool test_setrange(acl::redis_string& option, int n) { acl::string key, value; unsigned int off = 5; int ret; for (int i = 0; i < n; i++) { key.format("%s_%d", __keypre.c_str(), i); value.format("range_value_%s", key.c_str()); option.reset(); ret = option.setrange(key.c_str(), off, value.c_str()); if (ret < 0) { printf("setrange error, key: %s, off: %u, value: %s\r\n", key.c_str(), off, value.c_str()); return false; } else if (i < 10) printf("setrange ok, key: %s, off: %u, value: %s\r\n", key.c_str(), off, value.c_str()); } return true; }
static bool test_getset(acl::redis_string& option, int n) { acl::string key; acl::string value; acl::string result; for (int i = 0; i < n; i++) { key.format("%s_%d", __keypre.c_str(), i); value.format("getset_%s", key.c_str()); result.clear(); option.reset(); if (option.getset(key.c_str(), value.c_str(), result) == false) { printf("getset error, key: %s\r\n", key.c_str()); return false; } else if (i < 10) printf("getset: key: %s, old value: %s\r\n", key.c_str(), result.c_str()); } return true; }
static bool test_zadd(acl::redis_zset& redis, int n) { acl::string key; std::map<acl::string, double> members; acl::string member; for (int i = 0; i < n; i++) { key.format("%s_%d", __keypre.c_str(), i); for (int j = 0; j < 1000; j++) { member.format("member_%d", j); members[member] = j; } redis.clear(); int ret = redis.zadd(key, members); if (ret < 0) { printf("add key: %s error\r\n", key.c_str()); return false; } else if (i < 10) printf("add ok, key: %s, ret: %d\r\n", key.c_str(), ret); members.clear(); } return true; }
static bool test_sismember(acl::redis_set& redis, int n) { bool ret; acl::string key, member; for (int i = 0; i < n; i++) { key.format("%s_%d", __keypre.c_str(), i); for (int j = 0; j < 1000; j++) { member.format("member_%d", j); redis.clear(); ret = redis.sismember(key.c_str(), member.c_str()); if (redis.eof()) { printf("sismmeber eof, key: %s, member: %s\r\n", key.c_str(), member.c_str()); return false; } if (i >= 10) continue; printf("sismember: %s, key: %s, member: %s\r\n", ret ? "true" : "false", key.c_str(), member.c_str()); } } return true; }
static bool test_hsetnx(acl::redis_hash& redis, int n) { acl::string key; acl::string attr, value; int ret; for (int i = 0; i < n; i++) { key.format("%s_%d", __keypre.c_str(), i); attr.format("attr4"); value.format("value_%s", key.c_str()); redis.clear(); if ((ret = redis.hsetnx(key.c_str(), attr.c_str(), value.c_str())) <0) { printf("hsetnx key: %s error\r\n", key.c_str()); return false; } else if (i < 10) printf("hsetnx key: %s ok, ret: %d\r\n", key.c_str(), ret); } return true; }
static bool test_spop(acl::redis_set& redis, int n) { acl::string key; acl::string member; for (int i = 0; i < n; i++) { key.format("%s_%d", __keypre.c_str(), i); redis.clear(); bool ret = redis.spop(key.c_str(), member); if (redis.eof()) { printf("spop eof, key: %s\r\n", key.c_str()); return false; } if (i >= 10) continue; printf("spop %s, key: %s, member: %s\r\n", ret ? "ok" : "empty", key.c_str(), ret ? member.c_str() : ""); } return true; }
static bool test_zrem(acl::redis_zset& redis, int n) { acl::string key; acl::string member; std::vector<acl::string> members; for (int i = 0; i < n; i++) { key.format("%s_%d", __keypre.c_str(), i); for (int j = 900; j < 1000; j++) { member.format("member_%d", j); members.push_back(member); } redis.clear(); int ret = redis.zrem(key.c_str(), members); if (ret < 0) { printf("zrem error, key: %s\r\n", key.c_str()); return false; } else if (i < 10) printf("zrem ok, key: %s, ret: %d\r\n", key.c_str(), ret); } return true; }
static bool test_sdiff(acl::redis_set& redis, int n) { acl::string key; std::vector<acl::string> keys; std::vector<acl::string> result; for (int i = 0; i < 10; i++) { key.format("%s_%d", __keypre.c_str(), i); keys.push_back(key); } for (int i = 0; i < n; i++) { redis.clear(); int ret = redis.sdiff(keys, &result); if (ret < 0) { printf("sdiff error\r\n"); return false; } else if (i < 10) printf("sdiff ok, count: %d\r\n", ret); } return true; }
static bool test_hincrby(acl::redis_hash& redis, int n) { acl::string key, attr; long long int result; for (int i = 0; i < n; i++) { key.format("hincr_%s_%d", __keypre.c_str(), i); attr.format("attr1"); redis.clear(); if (redis.hincrby(key.c_str(), attr.c_str(), 10, &result) == false) { printf("hincrby error, key: %s, attr: %s\r\n", key.c_str(), attr.c_str()); return false; } else if (i < 10) printf("hincrby, key: %s, attr: %s, result: %lld\r\n", key.c_str(), attr.c_str(), result); } return true; }
static bool test_hget(acl::redis_hash& redis, int n) { acl::string key; acl::string attr, value; for (int i = 0; i < n; i++) { key.format("%s_%d", __keypre.c_str(), i); attr.format("attr1"); value.clear(); redis.clear(); if (redis.hget(key.c_str(), attr.c_str(), value) == false) { printf("hget key: %s, attr: %s\r\n", key.c_str(), attr.c_str()); return false; } else if (i >= 10) continue; printf("key: %s, attr: %s, value: %s, len: %d\r\n", key.c_str(), attr.c_str(), value.c_str(), (int) value.length()); } return true; }
static bool test_zincrby(acl::redis_zset& redis, int n) { acl::string key; double inc = 2.5, result; acl::string member; for (int i = 0; i < n; i++) { key.format("%s_%d", __keypre.c_str(), i); for (int j = 0; j < 1000; j++) { member.format("member_%d", j); redis.clear(); if (redis.zincrby(key.c_str(), inc, member.c_str(), &result) == false) { printf("zincrby error, key: %s\r\n", key.c_str()); return false; } else if (j < 10 && i * j < 100) printf("zincrby ok key: %s, result: %.2f\r\n", key.c_str(), result); } } return true; }
static bool test_hgetall(acl::redis_hash& redis, int n) { acl::string key; std::map<acl::string, acl::string> result; for (int i = 0; i < n; i++) { key.format("%s_%d", __keypre.c_str(), i); result.clear(); redis.clear(); if (redis.hgetall(key.c_str(), result) == false) { printf("hgetall key: %s\r\n", key.c_str()); return false; } else if (i >= 10) continue; std::map<acl::string, acl::string>::const_iterator cit; printf("key: %s\r\n", key.c_str()); for (cit = result.begin(); cit != result.end(); ++cit) { printf("attr: %s=%s\r\n", cit->first.c_str(), cit->second.c_str()); } } return true; }
static bool test_zrank(acl::redis_zset& redis, int n) { acl::string key; acl::string member; for (int i = 0; i < n; i++) { key.format("%s_%d", __keypre.c_str(), i); for (int j = 0; j < 1000; j++) { member.format("member_%d", j); redis.clear(); int ret = redis.zrank(key.c_str(), member.c_str()); if (ret < 0) { printf("zrank error, key: %s\r\n", key.c_str()); return false; } else if (j > 0 && j * i < 100) printf("zrank ok, key: %s, member:%s, " "rank: %d\r\n", key.c_str(), member.c_str(), ret); } } return true; }