-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYoungRedis.php
168 lines (131 loc) · 4.68 KB
/
YoungRedis.php
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* Young Redis Class
*
* PHP redis load balance drived class
*
* By Young<[email protected]> @151204 v1.0.2
* 1.1 change the lb method type
* 1.2 add or remove server features
*
*/
class YoungRedis {
// redis obj pool
public static $handler=array();
// redis connect
public static $connected = NULL;
// real server node
public $node = '';
// virtual server shards
public $virtualShards = 24;
//---------------------------------------------------------------------------------------
public function __construct($options=array(), $key='') {
$this->_init($options,$key);
}
// init class
private function _init($options=array(), $key='') {
if ( !extension_loaded('redis') ) {
echo "No redis extension!";
exit(1);
}
$options = array (
'host' => isset($options['host']) ? $options['host'] : array('127.0.0.1:6379'),
'shards' => isset($options['shards']) ? $options['shards'] : array(),
'timeout' => isset($options['timeout']) ? $options['timeout'] : false,
'persistent' => isset($options['persistent']) ? $options['persistent'] : false,
'expire' => isset($options['expire']) ? $options['expire'] : 86400
);
$this->options = $options;
}
// connect redis
public function connect($key='') {
$this->getRedisObj($key);
$func = $this->options['persistent'] ? 'pconnect' : 'connect';
$hostArr = explode(':', $this->node);
$host = isset($hostArr[0]) ? $hostArr[0] : '127.0.0.1';
$port = isset($hostArr[1]) ? $hostArr[1] : '6379';
if (!isset(static::$connected[$this->node])) {
static::$connected[$this->node] = $this->options['timeout'] === false ?
static::$handler[$this->node]->$func($host, $port) :
static::$handler[$this->node]->$func($host, $port, $this->options['timeout']);
}
}
// get redis obj
public function getRedisObj($key='') {
$this->node = $this->getRedisNode($key);
if (isset(static::$handler[$this->node]) AND static::$handler[$this->node]!==NULL)
return static::$handler[$this->node];
else
return static::$handler[$this->node] = new Redis;
}
// get redis node
public function getRedisNode($key='') {
if(empty($key)) {
echo "Key is empty, Error!";
exit;
}
$shards = $this->options['shards'];
$host = $this->options['host'];
$s = md5($key);
$intKey = ord($s[0])*100+ord($s[15])*10+ord($s[31]); //need to change
$shardIndex = $intKey % $this->virtualShards;
return isset($host[$shards[$shardIndex]]) ? $host[$shards[$shardIndex]] : array();
}
// get value
public function get($name) {
$node = $this->getRedisNode($name);
return static::$handler[$node]->get($name);
}
// set key
public function set($name, $value, $expire = null) {
$node = $this->getRedisNode($name);
if(is_array($value)) $value = serialize($value);
if(is_null($expire)) {
$expire = $this->options['expire'];
}
if(is_int($expire)) {
$result = static::$handler[$node]->setex($name, $expire, $value);
}else{
$result = static::$handler[$node]->set($name, $value);
}
return $result;
}
// del key
public function rm($name) {
$node = $this->getRedisNode($name);
return static::$handler[$node]->delete($name);
}
// close resource
public function __destruct(){
foreach (static::$handler as $key => $value) {
echo "close : ",$key,PHP_EOL;
static::$handler[$key]->close();
}
}
}
// test
$option = array(
'host' => array('s1'=>'192.168.1.11:6379', 's2'=>'192.168.1.12:6379'),
'shards' => array(
0 => 's1', 1 => 's1', 2 => 's1', 3 => 's1', 4 => 's1', 5 => 's1', 6 => 's1', 7 => 's1',
8 => 's1', 9 => 's1', 10 => 's1', 11 => 's1', 12 => 's1', 13 => 's2', 14 => 's2', 15 => 's2',
16 => 's2', 17 => 's2', 18 => 's2', 19 => 's2', 20 => 's2', 21 => 's2', 22 => 's2', 23 => 's2'
)
);
$redis = new YoungRedis($option);
for($i=1;$i<=10000;$i++) {
$v = md5($i);
$key = $v;
$value = str_repeat($v,rand(5,10));
//var_dump($value);
$redis->connect($key);
$redis->set($key, $value, 600);
$r = $redis->get($key);
if(empty($r)) echo "Empty:",$key,PHP_EOL;
//$redis->rm($key);
}
/*register_shutdown_function(function () {
global $redis;
var_dump($redis)
unset($redis);
});*/