forked from hetao29/slightphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSRedis.php
More file actions
100 lines (98 loc) · 3.08 KB
/
SRedis.php
File metadata and controls
100 lines (98 loc) · 3.08 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
<?php
/*{{{LICENSE
+-----------------------------------------------------------------------+
| SlightPHP Framework |
+-----------------------------------------------------------------------+
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by |
| the Free Software Foundation. You should have received a copy of the |
| GNU General Public License along with this program. If not, see |
| http://www.gnu.org/licenses/. |
| Copyright (C) 2008-2009. All Rights Reserved. |
+-----------------------------------------------------------------------+
| Supports: http://www.slightphp.com |
+-----------------------------------------------------------------------+
}}}*/
/**
* @package SlightPHP
* SRedis is base on php5-redis(phpredis https://github.com/phpredis/phpredis)
* use RedisArray , docs is on https://github.com/phpredis/phpredis/blob/master/arrays.markdown#readme
*/
if(!defined("SLIGHTPHP_PLUGINS_DIR"))define("SLIGHTPHP_PLUGINS_DIR",dirname(__FILE__));
require_once(SLIGHTPHP_PLUGINS_DIR."/SConfig.php");
class SRedis{
public function __construct(){
}
private static $_config;
private static $_rc;
private static $_instances=array();
static function setConfigFile($file){
self::$_config = $file;
}
/**
* @param string $zone
* @return array
*/
static function getConfig($zone=null,$type="host"){
$config = SConfig::getConfig(self::$_config,$zone);
if(isset($config->$type)){
return $config->$type;
}elseif(isset($config->host)){
return $config->host;
}
return;
}
/**
* 切换配置文件
* @param string $zone
* @return array
*/
static function useConfig($zone,$type="host"){
$key = $zone.":".$type;
if(isset(self::$_instances[$key])){
self::$_rc = self::$_instances[$key];
return self::$_rc;
}
$hosts=array();
$options=array();
$config = self::getConfig($zone,$type);
if(empty($config)){
trigger_error("the redis hosts is not set in config file(".self::$_config.")");
return false;
}
if(is_array($config)){
$hosts=$config;
}else{
$hosts[]=$config;
}
$config = self::getConfig($zone,"options");
if(!empty($config)){
if(is_object($config)){
foreach ($config as $k=>$v){
$options[$k]=$v;
}
}
}
return self::$_rc = self::$_instances[$key] = new RedisArray($hosts,$options);
}
public function __call($name,$args){
try{
if(self::$_rc) return call_user_func_array(array(self::$_rc,$name),$args);
}catch(RedisException $e){
self::$_instances=null;
self::$_rc=null;
trigger_error($e);
}
return false;
}
public static function __callStatic($name,$args){
try{
if(self::$_rc) return call_user_func_array(array(self::$_rc,$name),$args);
}catch(RedisException $e){
self::$_instances=null;
self::$_rc=null;
trigger_error($e);
}
return false;
}
}