This repository was archived by the owner on Mar 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdatabase_redis.cpp
More file actions
103 lines (99 loc) · 3.35 KB
/
database_redis.cpp
File metadata and controls
103 lines (99 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* File: database_redis.cpp
* Author: wuhao
*
* Created on 2015年11月27日, 下午2:12
*/
#include "boost/algorithm/string.hpp"
#include "core.h"
#include "database_redis.h"
std::string database_redis::class_name;
zend_class_entry* database_redis::class_entry;
php::value database_redis::init(const php::parameter& param) {
if(param.size < 1 || !param[0].is_type(IS_ARRAY)) {
zend_throw_error(NULL, "redis config of type array is required");
return false;
}
php::value conf = param[0];
if(!conf.__isset("master") && !conf.__isset("slave")) {
zend_throw_error(NULL, "illegal redis config #1");
return false;
}
php::property prop(database_redis::class_entry);
prop.sset("_conf", conf);
return true;
}
php::value database_redis::get_master(const php::parameter& param) {
zend_long idx = 0;
std::string key = "master_";
if(param.size == 1) {
idx = param[0];
}
key.append(std::to_string(idx));
php::property prop(database_redis::class_entry);
php::value cache = prop.sget("_cache");
php::value db = cache[key];
if(!db.is_type(IS_OBJECT)) {
php::value conf = prop.sget("_conf");
conf = conf["master"][idx];
if(!conf.is_type(IS_ARRAY)) {
zend_throw_error(NULL, "illegal redis config #2");
return php::null;
}
db = database_redis::create(conf);
cache[key] = db;
}
return db;
}
php::value database_redis::get_slave(const php::parameter& param) {
zend_long idx = 0;
std::string key = "slave_";
if(param.size == 1) {
idx = param[0];
}
key.append(std::to_string(idx));
php::property prop(database_redis::class_entry);
php::value cache = prop.sget("_cache");
php::value db = cache[key];
if(!db.is_type(IS_OBJECT)) {
php::value conf = prop.sget("_conf");
conf = conf["slave"][idx];
if(!conf.is_type(IS_ARRAY)) {
zend_throw_error(NULL, "illegal redis config #2");
return php::null;
}
db = database_redis::create(conf);
cache[key] = db;
}
return db;
}
php::value database_redis::create(php::value& config) {
std::string create_redis = "new Redis()";
php::value redis;
zend_eval_stringl(const_cast<char*>(create_redis.c_str()), create_redis.length(), redis.intern(), "database_redis::create");
if(!redis.is_type(IS_OBJECT)) {
zend_throw_error(nullptr, "failed to create Redis client");
return php::null;
}
// connect
std::vector<zval> connect_param;
connect_param.push_back(*config["host"].intern());
connect_param.push_back(*config["port"].intern());
if(config.__isset("timeout")) {
connect_param.push_back(*config["timeout"].intern());
}else{
php::value timeout = 2.0;
connect_param.push_back(*timeout.intern());
}
php::call_method(&redis, "connect", connect_param);
// setOption
if(config.__isset("prefix")) {
php::value redis_opt_prefix = 2;
php::call_method_2(&redis, "setoption", redis_opt_prefix, config["prefix"]);
}
// Auth
if(config.__isset("auth")) {
php::call_method_1(&redis, "auth", config["auth"]);
}
return std::move(redis);
}