forked from xinqiyang/buddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MRedis.php
executable file
·60 lines (52 loc) · 1.57 KB
/
MRedis.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
<?php
// +----------------------------------------------------------------------
// | Buddy Framework
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://buddy.woshimaijia.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: xinqiyang <xinqiyang@gmail.com>
// +----------------------------------------------------------------------
/**
* Redis Model
* redis Model use phpredis extension
* master and slave action,more read / write is use master db
* USE:
* $redis = MRedis::instance('redis');
* $key = 'key';
* $redis->get($key);
*/
class MRedis {
private static $objRedis;
public $redis;
private function __construct($node='redis')
{
$this->redis = new Redis();
$config = C('redis.'.$node);
if(empty($config))
{
logFatal(__CLASS__.'/'.__FUNCTION__.":get redis.$node config error");
}
try {
$this->redis->connect($config['host'], $config['port']);
}catch (Exception $e){
logFatal(__CLASS__.'/'.__FUNCTION__.":connet redis server error");
}
}
/**
* redis instance
* @param string $dbname dbname
* @param bool $pconnect pconnect
* @return object redis objects
*/
public static function instance($node='redis') {
$node = empty($node) ? 'redis' : $node;
if(!self::$objRedis[$node])
{
$redis = new self($node);
self::$objRedis[$node] = $redis->redis;
}
return self::$objRedis[$node];
}
}