From 2863b3ada360d384cc0210c30fb4f3a261178521 Mon Sep 17 00:00:00 2001 From: vus520 Date: Thu, 27 Sep 2012 10:36:55 +0800 Subject: [PATCH 01/20] =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E7=8B=AC=E7=AB=8B?= =?UTF-8?q?=E5=88=86=E7=BB=84=E5=87=BD=E6=95=B0=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 加载独立分组函数库 --- ThinkPHP/Lib/Core/App.class.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ThinkPHP/Lib/Core/App.class.php b/ThinkPHP/Lib/Core/App.class.php index dde3121..dba2314 100644 --- a/ThinkPHP/Lib/Core/App.class.php +++ b/ThinkPHP/Lib/Core/App.class.php @@ -54,7 +54,10 @@ class App { C(include $config_path.'config.php'); // 加载分组函数文件 if(is_file($common_path.'function.php')) - include $common_path.'function.php'; + include $common_path.'function.php'; + // 加载独立分组函数库 + if(is_file($common_path.'common.php')) + include $common_path.'common.php'; } // 页面压缩输出支持 if(C('OUTPUT_ENCODE')){ -- Gitee From fc0321f7b5be6968f2b0f81a203c8f46eab3f075 Mon Sep 17 00:00:00 2001 From: vus520 Date: Wed, 27 Feb 2013 17:01:45 +0800 Subject: [PATCH 02/20] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=94=AF=E6=8C=81memca?= =?UTF-8?q?che=20redis=E7=9A=84=E7=BC=93=E5=AD=98=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ThinkPHP/Common/functions.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ThinkPHP/Common/functions.php b/ThinkPHP/Common/functions.php index 8fc982d..faf0917 100644 --- a/ThinkPHP/Common/functions.php +++ b/ThinkPHP/Common/functions.php @@ -530,6 +530,8 @@ function session($name,$value='') { ini_set('session.auto_start', 0); if(isset($name['name'])) session_name($name['name']); if(isset($name['path'])) session_save_path($name['path']); + if(isset($name['save_handler'])) ini_set('session.save_handler', $name['save_handler']); + if(isset($name['save_path'])) ini_set('session.save_path', $name['save_path']); if(isset($name['domain'])) ini_set('session.cookie_domain', $name['domain']); if(isset($name['expire'])) ini_set('session.gc_maxlifetime', $name['expire']); if(isset($name['use_trans_sid'])) ini_set('session.use_trans_sid', $name['use_trans_sid']?1:0); @@ -750,4 +752,4 @@ function filter_exp(&$value){ if (in_array(strtolower($value),array('exp','or'))){ $value .= ' '; } -} \ No newline at end of file +} -- Gitee From deb169915e270ec0db483299be296ff8f1482211 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=98=E7=BC=98?= Date: Mon, 11 Aug 2014 18:44:36 +0800 Subject: [PATCH 03/20] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=B8=AARedis?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=89=A9=E5=B1=95=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=8C=E5=BD=93=E6=B2=A1=E6=9C=89=E9=85=8D=E7=BD=AE=E8=BF=87?= =?UTF-8?q?=E6=9C=9F=E6=97=B6=E9=97=B4=E6=97=B6=EF=BC=8C=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E7=9A=84=E8=BF=87=E6=9C=9F=E6=97=B6=E9=97=B4=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E4=B8=BA0=E3=80=82redis=E8=BF=87=E6=9C=9F=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E4=B8=BA0=E6=97=B6=EF=BC=8C=E4=B8=BA=E7=AB=8B=E5=8D=B3?= =?UTF-8?q?=E5=A4=B1=E6=95=88=EF=BC=8Cmemcache=E9=87=8C=E4=B8=BA=E6=B0=B8?= =?UTF-8?q?=E4=B8=8D=E8=BF=87=E6=9C=9F=E3=80=82=E4=B8=A4=E8=80=85=E6=AD=A3?= =?UTF-8?q?=E5=A5=BD=E7=9B=B8=E5=8F=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ThinkPHP/Library/Think/Cache/Driver/Redis.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ThinkPHP/Library/Think/Cache/Driver/Redis.class.php b/ThinkPHP/Library/Think/Cache/Driver/Redis.class.php index 0d29f5e..4d2ee61 100644 --- a/ThinkPHP/Library/Think/Cache/Driver/Redis.class.php +++ b/ThinkPHP/Library/Think/Cache/Driver/Redis.class.php @@ -74,7 +74,7 @@ class Redis extends Cache { $name = $this->options['prefix'].$name; //对数组/对象数据进行缓存处理,保证数据完整性 $value = (is_object($value) || is_array($value)) ? json_encode($value) : $value; - if(is_int($expire)) { + if(is_int($expire) && $expire) { $result = $this->handler->setex($name, $expire, $value); }else{ $result = $this->handler->set($name, $value); -- Gitee From 87a27f928c4a8f9d2f12cfe1dc423eec060827e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=98=E7=BC=98?= Date: Thu, 6 Nov 2014 19:12:14 +0800 Subject: [PATCH 04/20] =?UTF-8?q?MongoModel=E4=B8=AD=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E8=B6=85=E6=97=B6=E6=97=B6=E9=97=B4=E6=8E=A7?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Library/Think/Model/MongoModel.class.php | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/ThinkPHP/Library/Think/Model/MongoModel.class.php b/ThinkPHP/Library/Think/Model/MongoModel.class.php index 2d0c225..5fb86cd 100644 --- a/ThinkPHP/Library/Think/Model/MongoModel.class.php +++ b/ThinkPHP/Library/Think/Model/MongoModel.class.php @@ -385,7 +385,7 @@ class MongoModel extends Model{ * @access public * @return json */ - public function getLastError(){ + public function getLastError() { return $this->db->command(array('getLastError'=>1)); } @@ -394,7 +394,7 @@ class MongoModel extends Model{ * @access public * @return json */ - public function status(){ + public function status() { $option = $this->_parseOptions(); return $this->db->command(array('collStats'=>$option['table'])); } @@ -404,7 +404,7 @@ class MongoModel extends Model{ * @access public * @return object */ - public function getDB(){ + public function getDB() { return $this->db->getDB(); } @@ -413,7 +413,19 @@ class MongoModel extends Model{ * @access public * @return object */ - public function getCollection(){ + public function getCollection() { return $this->db->getCollection(); } + + /** + * 设置查询超时时间,-1为不限时间 + * @param int $timeout 毫秒 + * @access public + * @return object + */ + public function timeout($timeout=-1) { + if(class_exists('MongoCursor')) + MongoCursor::$timeout = $timeout; + return $this; + } } \ No newline at end of file -- Gitee From 6bfd296b61fbea2246f050cea0180828275e1d75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=98=E7=BC=98?= Date: Wed, 19 Nov 2014 14:46:04 +0800 Subject: [PATCH 05/20] =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E6=89=A9=E5=B1=95=E6=96=87=E4=BB=B6=E6=94=AF=E6=8C=81=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E9=85=8D=E7=BD=AE=E7=9B=AE=E5=BD=95=E4=B8=8B=E7=9A=84?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ThinkPHP/Common/functions.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ThinkPHP/Common/functions.php b/ThinkPHP/Common/functions.php index 1ab5567..856332b 100644 --- a/ThinkPHP/Common/functions.php +++ b/ThinkPHP/Common/functions.php @@ -1372,6 +1372,8 @@ function load_ext_file($path) { $file = $path.'Conf/'.$config.CONF_EXT; if(is_file($file)) { is_numeric($key)?C(load_config($file)):C($key,load_config($file)); + }elseif(is_file($file = CONF_PATH.$config.CONF_EXT)) { + is_numeric($key)?C(load_config($file)):C($key,load_config($file)); } } } -- Gitee From ea11ac79d7b75dd3f7464ebd6481e52f43fd17ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=98=E7=BC=98?= Date: Sat, 27 Dec 2014 21:32:40 +0800 Subject: [PATCH 06/20] =?UTF-8?q?=E4=BC=98=E5=8C=96MongoDB=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E7=9A=84=E6=9F=A5=E8=AF=A2=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Library/Think/Db/Driver/Mongo.class.php | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/ThinkPHP/Library/Think/Db/Driver/Mongo.class.php b/ThinkPHP/Library/Think/Db/Driver/Mongo.class.php index f8234f7..e412b93 100644 --- a/ThinkPHP/Library/Think/Db/Driver/Mongo.class.php +++ b/ThinkPHP/Library/Think/Db/Driver/Mongo.class.php @@ -363,6 +363,16 @@ class Mongo extends Driver { if(isset($options['table'])) { $this->switchCollection($options['table'],'',false); } + + $cache = isset($options['cache'])?$options['cache']:false; + if($cache) { + $key = is_string($cache['key'])?$cache['key']:md5(serialize($options)); + $value = S($key,'','',$cache['type']); + if(false !== $value) { + return $value; + } + } + $this->model = $options['model']; $this->queryTimes++; N('db_query',1); // 兼容代码 @@ -412,6 +422,10 @@ class Mongo extends Driver { $this->debug(false); $this->_cursor = $_cursor; $resultSet = iterator_to_array($_cursor); + + if($cache) + S($key,$resultSet,$cache['expire'],$cache['type']); + return $resultSet; } catch (\MongoCursorException $e) { E($e->getMessage()); @@ -440,6 +454,15 @@ class Mongo extends Driver { if(isset($options['table'])) { $this->switchCollection($options['table'],'',false); } + + $cache = isset($options['cache'])?$options['cache']:false; + if($cache) { + $key = is_string($cache['key'])?$cache['key']:md5(serialize($options)); + $value = S($key,'','',$cache['type']); + if(false !== $value) { + return $value; + } + } $this->model = $options['model']; $this->queryTimes++; N('db_query',1); // 兼容代码 @@ -453,6 +476,10 @@ class Mongo extends Driver { $this->debug(true); $count = $this->_collection->count($query); $this->debug(false); + + if($cache) + S($key,$count,$cache['expire'],$cache['type']); + return $count; } catch (\MongoCursorException $e) { E($e->getMessage()); -- Gitee From 7030af64b9d13bdf7cedc9e38399285da77f6614 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=98=E7=BC=98?= Date: Sat, 27 Dec 2014 21:55:37 +0800 Subject: [PATCH 07/20] =?UTF-8?q?cli=E6=A8=A1=E5=BC=8F=E4=BB=85=E5=9C=A8Wi?= =?UTF-8?q?ndows=E7=8E=AF=E5=A2=83=E4=B8=AD=E5=AF=B9=E4=B8=AD=E6=96=87?= =?UTF-8?q?=E8=BF=9B=E8=A1=8CGBK=E7=BC=96=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ThinkPHP/Library/Think/Think.class.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ThinkPHP/Library/Think/Think.class.php b/ThinkPHP/Library/Think/Think.class.php index 3bd1864..4d038b4 100644 --- a/ThinkPHP/Library/Think/Think.class.php +++ b/ThinkPHP/Library/Think/Think.class.php @@ -297,7 +297,8 @@ class Think { $e = $error; } if(IS_CLI){ - exit(iconv('UTF-8','gbk',$e['message']).PHP_EOL.'FILE: '.$e['file'].'('.$e['line'].')'.PHP_EOL.$e['trace']); + $e['message'] = IS_WIN ? iconv('UTF-8','gbk',$e['message']) : $e['message']; + exit($e['message'].PHP_EOL.'FILE: '.$e['file'].'('.$e['line'].')'.PHP_EOL.$e['trace']); } } else { //否则定向到错误页面 -- Gitee From 19fc70f0445a2600e7367282dd5862827b727b31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=98=E7=BC=98?= Date: Sat, 27 Dec 2014 23:20:37 +0800 Subject: [PATCH 08/20] =?UTF-8?q?=E4=BF=AE=E5=A4=8DMongoDB=E5=9C=A8?= =?UTF-8?q?=E5=8D=87=E7=BA=A7=E5=88=B03.2.3=E6=97=B6=EF=BC=8C=E6=89=A7?= =?UTF-8?q?=E8=A1=8Ccomand=E5=91=BD=E4=BB=A4=E6=97=B6=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E8=AE=B0=E5=BD=95last=20sql=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ThinkPHP/Library/Think/Db/Driver/Mongo.class.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ThinkPHP/Library/Think/Db/Driver/Mongo.class.php b/ThinkPHP/Library/Think/Db/Driver/Mongo.class.php index e412b93..ce7592b 100644 --- a/ThinkPHP/Library/Think/Db/Driver/Mongo.class.php +++ b/ThinkPHP/Library/Think/Db/Driver/Mongo.class.php @@ -116,6 +116,7 @@ class Mongo extends Driver { } } N('db_write',1); // 兼容代码 + $this->model = $options['model']; $this->executeTimes++; try{ if($this->config['debug']) { @@ -275,9 +276,10 @@ class Mongo extends Driver { if(isset($options['table'])) { $this->switchCollection($options['table']); } + + $this->model = $options['model']; $this->executeTimes++; N('db_write',1); // 兼容代码 - $this->model = $options['model']; $query = $this->parseWhere(isset($options['where'])?$options['where']:array()); $set = $this->parseSet($data); if($this->config['debug']) { -- Gitee From 61a97515d83fc40b895ffffd1eca1c8a361db723 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=98=E7=BC=98?= Date: Sat, 4 Apr 2015 19:12:33 +0800 Subject: [PATCH 09/20] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E5=A4=B4=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ThinkPHP/Library/Think/View.class.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ThinkPHP/Library/Think/View.class.php b/ThinkPHP/Library/Think/View.class.php index cbd6aaf..87a3b4b 100644 --- a/ThinkPHP/Library/Think/View.class.php +++ b/ThinkPHP/Library/Think/View.class.php @@ -90,7 +90,6 @@ class View { // 网页字符编码 header('Content-Type:'.$contentType.'; charset='.$charset); header('Cache-control: '.C('HTTP_CACHE_CONTROL')); // 页面缓存控制 - header('X-Powered-By:ThinkPHP'); // 输出模板文件 echo $content; } @@ -226,4 +225,4 @@ class View { return $theme?$theme . '/':''; } -} \ No newline at end of file +} -- Gitee From 467564e9e887a32bf7aa8ffef501437119f46728 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=98=E7=BC=98?= Date: Thu, 7 May 2015 18:22:17 +0800 Subject: [PATCH 10/20] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E5=87=BA=E7=8E=B0=E9=94=99=E8=AF=AF=E6=97=B6=EF=BC=8C=E5=8F=91?= =?UTF-8?q?=E9=80=81=E7=9A=84404=E9=94=99=E8=AF=AF=E4=B8=BA500=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ThinkPHP/Library/Think/Think.class.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ThinkPHP/Library/Think/Think.class.php b/ThinkPHP/Library/Think/Think.class.php index 4d038b4..0d37c75 100644 --- a/ThinkPHP/Library/Think/Think.class.php +++ b/ThinkPHP/Library/Think/Think.class.php @@ -225,9 +225,11 @@ class Think { } $error['trace'] = $e->getTraceAsString(); Log::record($error['message'],Log::ERR); - // 发送404信息 - header('HTTP/1.1 404 Not Found'); - header('Status:404 Not Found'); + + // 发送500信息 + header('HTTP/1.1 500 Not Found'); + header('Status:500 Not Found'); + self::halt($error); } -- Gitee From 27cc7a464eb689d550ae0f68e91ecedf60f8800b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=98=E7=BC=98?= Date: Thu, 12 Nov 2015 09:42:05 +0800 Subject: [PATCH 11/20] =?UTF-8?q?Redis=E6=B7=BB=E5=8A=A0=E9=AD=94=E6=9C=AF?= =?UTF-8?q?=E6=96=B9=E6=B3=95=EF=BC=8C=E6=94=AF=E6=8C=81=E5=85=B6=E5=AE=83?= =?UTF-8?q?redis=E9=A9=B1=E5=8A=A8=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ThinkPHP/Library/Think/Cache/Driver/Redis.class.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ThinkPHP/Library/Think/Cache/Driver/Redis.class.php b/ThinkPHP/Library/Think/Cache/Driver/Redis.class.php index eaca5ee..aaaaa6f 100644 --- a/ThinkPHP/Library/Think/Cache/Driver/Redis.class.php +++ b/ThinkPHP/Library/Think/Cache/Driver/Redis.class.php @@ -108,5 +108,15 @@ class Redis extends Cache { return $this->handler->flushDB(); } + + /** + * 魔术方法,phpRedis中所包含的所有操作均可以通过魔术方法直接调用 + * @access public + * @return mixed + */ + function __call($method, $args) + { + return call_user_func_array(array($this->handler, $method) , $args); + } } -- Gitee From ceac8ec3c1424b8b5e0cda46c45e541f905d32aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=98=E7=BC=98?= Date: Thu, 12 Nov 2015 09:48:37 +0800 Subject: [PATCH 12/20] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=87=A0=E9=A1=B9gitig?= =?UTF-8?q?nore=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .buildpath | 5 ---- .gitignore | 13 ++++++++- .project | 27 ------------------- .settings/org.eclipse.php.core.prefs | 3 --- ...debug.core.Debug_Process_Preferences.prefs | 2 -- 5 files changed, 12 insertions(+), 38 deletions(-) delete mode 100644 .buildpath delete mode 100644 .project delete mode 100644 .settings/org.eclipse.php.core.prefs delete mode 100644 .settings/org.eclipse.php.debug.core.Debug_Process_Preferences.prefs diff --git a/.buildpath b/.buildpath deleted file mode 100644 index 219add1..0000000 --- a/.buildpath +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/.gitignore b/.gitignore index 723ef36..11f3e51 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,12 @@ -.idea \ No newline at end of file +.idea +.settings/ +.DS_Store +.project +.metadata +*.properties +.classpath +.settings/ +.loadpath +.buildpath +*.swp +composer.lock diff --git a/.project b/.project deleted file mode 100644 index 90d7c0b..0000000 --- a/.project +++ /dev/null @@ -1,27 +0,0 @@ - - - thinkphp - - - - - - org.eclipse.wst.validation.validationbuilder - - - - - org.eclipse.dltk.core.scriptbuilder - - - - - com.zend.php.composer.core.ComposerJsonBuilder - - - - - - org.eclipse.php.core.PHPNature - - diff --git a/.settings/org.eclipse.php.core.prefs b/.settings/org.eclipse.php.core.prefs deleted file mode 100644 index 7487825..0000000 --- a/.settings/org.eclipse.php.core.prefs +++ /dev/null @@ -1,3 +0,0 @@ -eclipse.preferences.version=1 -include_path=0;/thinkphp -use_asp_tags_as_php=false diff --git a/.settings/org.eclipse.php.debug.core.Debug_Process_Preferences.prefs b/.settings/org.eclipse.php.debug.core.Debug_Process_Preferences.prefs deleted file mode 100644 index cfb0f47..0000000 --- a/.settings/org.eclipse.php.debug.core.Debug_Process_Preferences.prefs +++ /dev/null @@ -1,2 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.php.debug.core.use-project-settings=true -- Gitee From e797c963a06664ef1beff0272bb148e64bad16af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=98=E7=BC=98?= Date: Thu, 12 Nov 2015 09:55:03 +0800 Subject: [PATCH 13/20] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ThinkPHP/Library/Think/Cache/Driver/Redis.class.php | 2 +- ThinkPHP/Library/Think/Db/Driver/Mongo.class.php | 3 ++- ThinkPHP/Library/Think/Model/MongoModel.class.php | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/ThinkPHP/Library/Think/Cache/Driver/Redis.class.php b/ThinkPHP/Library/Think/Cache/Driver/Redis.class.php index aaaaa6f..3999147 100644 --- a/ThinkPHP/Library/Think/Cache/Driver/Redis.class.php +++ b/ThinkPHP/Library/Think/Cache/Driver/Redis.class.php @@ -116,7 +116,7 @@ class Redis extends Cache */ function __call($method, $args) { - return call_user_func_array(array($this->handler, $method) , $args); + return call_user_func_array(array($this->handler, $method) , $args); } } diff --git a/ThinkPHP/Library/Think/Db/Driver/Mongo.class.php b/ThinkPHP/Library/Think/Db/Driver/Mongo.class.php index 280f7e4..1972ed5 100644 --- a/ThinkPHP/Library/Think/Db/Driver/Mongo.class.php +++ b/ThinkPHP/Library/Think/Db/Driver/Mongo.class.php @@ -511,8 +511,9 @@ class Mongo extends Driver $count = $this->_collection->count($query); $this->debug(false); - if($cache) + if($cache) { S($key,$count,$cache['expire'],$cache['type']); + } return $count; } catch (\MongoCursorException $e) { diff --git a/ThinkPHP/Library/Think/Model/MongoModel.class.php b/ThinkPHP/Library/Think/Model/MongoModel.class.php index f8b4dcd..eb48cb3 100644 --- a/ThinkPHP/Library/Think/Model/MongoModel.class.php +++ b/ThinkPHP/Library/Think/Model/MongoModel.class.php @@ -489,7 +489,7 @@ class MongoModel extends Model * @access public * @return object */ - public function getCollection() + public function getCollection() { return $this->db->getCollection(); } -- Gitee From dad5aa8e5af799228d4065ce6b25a699e611d671 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=98=E7=BC=98?= Date: Thu, 12 Nov 2015 10:00:11 +0800 Subject: [PATCH 14/20] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Library/Think/Db/Driver/Mongo.class.php | 19 +++++++++---------- .../Library/Think/Model/MongoModel.class.php | 5 +++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/ThinkPHP/Library/Think/Db/Driver/Mongo.class.php b/ThinkPHP/Library/Think/Db/Driver/Mongo.class.php index 1972ed5..79b2cdf 100644 --- a/ThinkPHP/Library/Think/Db/Driver/Mongo.class.php +++ b/ThinkPHP/Library/Think/Db/Driver/Mongo.class.php @@ -130,7 +130,7 @@ class Mongo extends Driver return $value; } } - N('db_write',1); // 兼容代码 + N('db_write', 1); // 兼容代码 $this->model = $options['model']; $this->executeTimes++; try { @@ -300,7 +300,6 @@ class Mongo extends Driver $this->switchCollection($options['table']); } - $this->model = $options['model']; $this->executeTimes++; N('db_write', 1); // 兼容代码 $this->model = $options['model']; @@ -395,13 +394,13 @@ class Mongo extends Driver $cache = isset($options['cache'])?$options['cache']:false; if($cache) { - $key = is_string($cache['key'])?$cache['key']:md5(serialize($options)); - $value = S($key,'','',$cache['type']); - if(false !== $value) { - return $value; - } + $key = is_string($cache['key'])?$cache['key']:md5(serialize($options)); + $value = S($key,'','',$cache['type']); + if(false !== $value) { + return $value; + } } - + $this->model = $options['model']; $this->queryTimes++; N('db_query', 1); // 兼容代码 @@ -455,9 +454,9 @@ class Mongo extends Driver $resultSet = iterator_to_array($_cursor); if($cache) { - S($key,$resultSet,$cache['expire'],$cache['type']); + S($key,$resultSet,$cache['expire'],$cache['type']); } - + return $resultSet; } catch (\MongoCursorException $e) { E($e->getMessage()); diff --git a/ThinkPHP/Library/Think/Model/MongoModel.class.php b/ThinkPHP/Library/Think/Model/MongoModel.class.php index eb48cb3..8d107af 100644 --- a/ThinkPHP/Library/Think/Model/MongoModel.class.php +++ b/ThinkPHP/Library/Think/Model/MongoModel.class.php @@ -501,8 +501,9 @@ class MongoModel extends Model * @return object */ public function timeout($timeout=-1) { - if(class_exists('MongoCursor')) - MongoCursor::$timeout = $timeout; + if(class_exists('MongoCursor')) { + MongoCursor::$timeout = $timeout; + } return $this; } } -- Gitee From 573d247ca185a5e4033061c2ff58309c9a6422d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=98=E7=BC=98?= Date: Thu, 12 Nov 2015 10:02:38 +0800 Subject: [PATCH 15/20] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ThinkPHP/Library/Think/Db/Driver/Mongo.class.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ThinkPHP/Library/Think/Db/Driver/Mongo.class.php b/ThinkPHP/Library/Think/Db/Driver/Mongo.class.php index 79b2cdf..d69d7a6 100644 --- a/ThinkPHP/Library/Think/Db/Driver/Mongo.class.php +++ b/ThinkPHP/Library/Think/Db/Driver/Mongo.class.php @@ -299,7 +299,7 @@ class Mongo extends Driver if (isset($options['table'])) { $this->switchCollection($options['table']); } - + $this->executeTimes++; N('db_write', 1); // 兼容代码 $this->model = $options['model']; @@ -487,14 +487,14 @@ class Mongo extends Driver if (isset($options['table'])) { $this->switchCollection($options['table'], '', false); } - + $cache = isset($options['cache'])?$options['cache']:false; if($cache) { - $key = is_string($cache['key'])?$cache['key']:md5(serialize($options)); - $value = S($key,'','',$cache['type']); - if(false !== $value) { - return $value; - } + $key = is_string($cache['key'])?$cache['key']:md5(serialize($options)); + $value = S($key,'','',$cache['type']); + if(false !== $value) { + return $value; + } } $this->model = $options['model']; $this->queryTimes++; -- Gitee From 81a4fae1d66d80e975709c5ec184c7bee79dc1a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=98=E7=BC=98?= Date: Tue, 5 Apr 2016 16:35:14 +0800 Subject: [PATCH 16/20] =?UTF-8?q?ThinkPHP=20Redis=E7=AE=80=E5=8D=95?= =?UTF-8?q?=E4=B8=BB=E4=BB=8E=E5=AE=9E=E7=8E=B0=E7=9A=84=E9=AB=98=E5=8F=AF?= =?UTF-8?q?=E7=94=A8=E6=96=B9=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Think/Cache/Driver/Redisd.class.php | 290 ++++++++++++++++++ 1 file changed, 290 insertions(+) create mode 100644 ThinkPHP/Library/Think/Cache/Driver/Redisd.class.php diff --git a/ThinkPHP/Library/Think/Cache/Driver/Redisd.class.php b/ThinkPHP/Library/Think/Cache/Driver/Redisd.class.php new file mode 100644 index 0000000..3be55aa --- /dev/null +++ b/ThinkPHP/Library/Think/Cache/Driver/Redisd.class.php @@ -0,0 +1,290 @@ +'Redisd', //默认动态缓存为Redis + 'DATA_CACHE_PREFIX' =>'Redis_', //缓存前缀 + 'DATA_CACHE_TIME' =>0, //默认过期时间,默认0为永不过期 + 'REDISD_HOST' =>'192.168.1.11,192.168.1.12', //redis服务器ip,多台用逗号隔开;读写分离开启时,第一台负责写,其它[随机]负责读 + 'REDISD_MASTER_FAILOVER' =>'192.168.1.12', //当主发生故障时,从12断从升级为主,读写均在12 + 'REDISD_PORT' =>6379, //端口号 + 'REDISD_TIMEOUT' =>30, //连接超时时间 + 'REDISD_PERSISTENT' =>false, //是否长连接 false=短连接,推荐长链接 + 'REDISD_AUTH' =>'', //AUTH认证密码 + */ + +namespace Think\Cache\Driver; + +use Think\Cache; + +class Redisd extends Cache +{ + static $redis_rw_handler; + static $redis_err_pool; + + /** + * 为了在单次php请求中复用redis连接,第一次获取的options会被缓存,第二次使用不同的$options,将会无效 + * + * @param array $options 缓存参数,来自于S函数和\Think\Cache::getInstance("redisd", $options); + * @access public + */ + public function __construct($options = array()) + { + if (! extension_loaded('redis' )) { + throw_exception(L('_NOT_SUPPERT_') . ':redis'); + } + + $default = array ( + 'host' => C('REDISD_HOST') ? C('REDISD_HOST') : '127.0.0.1', + 'port' => C('REDISD_PORT') ? C('REDISD_PORT') : 6379, + 'timeout' => C('REDISD_TIMEOUT') ? C('REDISD_TIMEOUT') : 0, + 'persistent' => C('REDISD_PERSISTENT'), + 'auth' => C('REDISD_AUTH'), + 'server_master_failover' => C('REDISD_MASTER_FAILOVER'), + ); + + $options = array_merge($default, $options); + + $this->options = $options; + $this->options ['expire'] = isset($options ['expire']) ? $options ['expire'] : C('DATA_CACHE_TIME'); + $this->options ['prefix'] = isset($options ['prefix']) ? $options ['prefix'] : C('DATA_CACHE_PREFIX'); + $this->options ['length'] = isset($options ['length']) ? $options ['length'] : 0; + $this->options ['func'] = $options ['persistent'] ? 'pconnect' : 'connect'; + + $host = explode(",", trim($this->options ['host'], ",")); + $host = array_map("trim", $host); + + $this->options ["servers"] = count($host); + $this->options ["server_master"] = array_shift($host); + $this->options ["server_master_failover"] = explode(",", trim($this->options ['server_master_failover'], ",")); + $this->options ["server_slave"] = (array)$host; + } + + /** + * 主从选择器,配置多个Host则自动启用读写分离,默认主写,随机从读 + * 随机从读的场景适合读频繁,且php与redis从位于单机的架构,这样可以减少网络IO + * 一致Hash适合超高可用,跨网络读取,且从节点较多的情况,本业务不考虑该需求 + * + * @access public + * @param bool $master true 默认主写 + */ + public function master($master = false) + { + if (isset(self::$redis_rw_handler[$master])) { + return $this->handler = self::$redis_rw_handler[$master]; + } + + //如果不为主,则从配置的host剔除主,并随机读从,失败以后再随机选择从 + //另外一种方案是根据key的一致性hash选择不同的node,但读写频繁的业务中可能打开大量的文件句柄 + if(!$master && $this->options["servers"] > 1) { + shuffle($this->options["server_slave"]); + $host = array_shift($this->options["server_slave"]); + }else{ + $host = $this->options["server_master"]; + } + + $this->handler = new \Redis(); + $func = $this->options ['func']; + + $parse = parse_url($host); + $host = isset($parse['host']) ? $parse['host'] : $parse['path']; + $port = $parse['port'] ? $parse['port'] : $this->options ['port']; + + $this->handler->$func($host, $port, $this->options ['timeout']); + + if ($this->options ['auth'] != null) { + $this->handler->auth($this->options ['auth']); + } + + //发生错误则摘掉当前节点 + try { + $error = $this->handler->getLastError(); + } catch (\RedisException $e) { + //phpredis throws a RedisException object if it can't reach the Redis server. + //That can happen in case of connectivity issues, if the Redis service is down, or if the redis host is overloaded. + //In any other problematic case that does not involve an unreachable server + //(such as a key not existing, an invalid command, etc), phpredis will return FALSE. + + \Think\Log::write(sprintf("redisd->%s:%s:%s", $master ? "master" : "salve", $host, $e->getMessage()), \Think\Log::WARN); + + //主节点挂了以后,尝试连接主备,断开主备的主从连接进行升主 + if($master) { + if(! count($this->options["server_master_failover"])) { + E("redisd master: no more server_master_failover. {$host} : ".$e->getMessage()); + return false; + } + + $this->options["server_master"] = array_shift($this->options["server_master_failover"]); + $this->master(); + + \Think\Log::write(sprintf("master is down, try server_master_failover : %s", $this->options["server_master"]), \Think\Log::WARN); + + //如果是slave,断开主从升主,需要手工同步新主的数据到旧主上 + //目前这块的逻辑未经过严格测试 + $this->handler->slaveof(); + } else { + //尝试failover,如果有其它节点则进行其它节点的尝试 + array_shift($this->options["server_slave"]); + + //如果无可用节点,则抛出异常 + if(! count($this->options["server_slave"])) { + \Think\Log::write("已无可用Redis读节点", \Think\Log::EMERG); + E("redisd slave: no more server_slave. {$host} : ".$e->getMessage()); + return false; + } else { + \Think\Log::write("salve {$host} is down, try another one.", \Think\Log::EMERG); + return $this->master(false); + } + } + } catch(\Exception $e) { + E($e->getMessage(), $e->getCode()); + } + + self::$redis_rw_handler[$master] = $this->handler; + } + + /** + * 读取缓存 + * + * @access public + * @param string $name 缓存key + * @return mixed + */ + public function get($name) + { + N('cache_read', 1); + $this->master(false); + + try { + $value = $this->handler->get($this->options ['prefix'] . $name); + } catch (\RedisException $e) { + $this->master(); + $this->get($name); + } catch (\Exception $e) { + \Think\Log::write($e->getMessage(), \Think\Log::WARN); + } + + $jsonData = null; + //如果是对象则进行反转 + if (!empty($value) && false !== strpos("[{", $value[0])) { + $jsonData = $value ? json_decode($value, true) : $value; + } + + return ($jsonData === NULL) ? $value : $jsonData; + } + + /** + * 写入缓存 + * + * @access public + * @param string $name 缓存key + * @param mixed $value 缓存value + * @param integer $expire 过期时间,单位秒 + * @return boolen + */ + public function set($name, $value, $expire = null) + { + N('cache_write', 1); + $this->master(true); + + if (is_null($expire )) { + $expire = $this->options ['expire']; + } + $name = $this->options ['prefix'] . $name; + + /** + * 兼容历史版本 + * Redis不支持存储对象,存入对象会转换成字符串 + * 但在这里,对所有数据做json_decode会有性能开销 + */ + $value = (is_object($value) || is_array($value )) ? json_encode($value) : $value; + + if ($value === null) { + return $this->handler->delete($this->options ['prefix'] . $name); + } + + // $expire < 0 则等于ttl操作,列为todo吧 + try { + if (is_int($expire) && $expire) { + $result = $this->handler->setex($name, $expire, $value); + } else { + $result = $this->handler->set($name, $value); + } + } catch (\RedisException $e) { + $this->master(true); + $this->set($name, $value, $expire); + } catch (\Exception $e) { + \Think\Log::write($e->getMessage(), \Think\Log::WARN); + } + + return $result; + } + + /** + * 返回句柄对象 + * 需要先执行 $redis->master() 连接到 DB + * + * @access public + * @return object + */ + function handler() + { + return $this->handler; + } + + /** + * 删除缓存 + * + * @access public + * @param string $name 缓存变量名 + * @return boolen + */ + public function rm($name) + { + N('cache_write', 1); + $this->master(true); + return $this->handler->delete($this->options ['prefix'] . $name); + } + + /** + * 清除缓存 + * + * @access public + * @return boolen + */ + public function clear() + { + N('cache_write', 1); + $this->master(true); + return $this->handler->flushDB (); + } + + /** + * 析构释放连接 + * + * @access public + */ + public function __destruct() + { + //该方法仅在connect连接时有效 + //当使用pconnect时,连接会被重用,连接的生命周期是fpm进程的生命周期,而非一次php的执行。 + //如果代码中使用pconnect, close的作用仅是使当前php不能再进行redis请求,但无法真正关闭redis长连接,连接在后续请求中仍然会被重用,直至fpm进程生命周期结束。 + + try { + if(method_exists($this->handler, "close")) + $this->handler->close (); + } catch (\Exception $e) { + } + } +} \ No newline at end of file -- Gitee From 805c645b9fb777c14a13c65c9c06a3afcf3e0392 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=98=E7=BC=98?= Date: Thu, 7 Apr 2016 16:00:41 +0800 Subject: [PATCH 17/20] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=91=98=E9=99=A4?= =?UTF-8?q?=E4=BB=8E=E8=8A=82=E7=82=B9=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ThinkPHP/Library/Think/Cache/Driver/Redisd.class.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ThinkPHP/Library/Think/Cache/Driver/Redisd.class.php b/ThinkPHP/Library/Think/Cache/Driver/Redisd.class.php index 3be55aa..1147b67 100644 --- a/ThinkPHP/Library/Think/Cache/Driver/Redisd.class.php +++ b/ThinkPHP/Library/Think/Cache/Driver/Redisd.class.php @@ -135,7 +135,11 @@ class Redisd extends Cache $this->handler->slaveof(); } else { //尝试failover,如果有其它节点则进行其它节点的尝试 - array_shift($this->options["server_slave"]); + foreach ($this->options["server_slave"] as $k=>$v) + { + if (trim($v) == trim($host)) + unset($this->options["server_slave"][$k]); + } //如果无可用节点,则抛出异常 if(! count($this->options["server_slave"])) { -- Gitee From cbfcd8eb00ce89fa99f572d4311ef4a497b25283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=98=E7=BC=98?= Date: Thu, 7 Apr 2016 18:41:35 +0800 Subject: [PATCH 18/20] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=AF=BB=E5=86=99?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E6=97=B6=E9=87=8D=E8=AF=95=E7=9A=84=E7=AD=96?= =?UTF-8?q?=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ThinkPHP/Library/Think/Cache/Driver/Redisd.class.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ThinkPHP/Library/Think/Cache/Driver/Redisd.class.php b/ThinkPHP/Library/Think/Cache/Driver/Redisd.class.php index 1147b67..30ee64d 100644 --- a/ThinkPHP/Library/Think/Cache/Driver/Redisd.class.php +++ b/ThinkPHP/Library/Think/Cache/Driver/Redisd.class.php @@ -173,6 +173,7 @@ class Redisd extends Cache try { $value = $this->handler->get($this->options ['prefix'] . $name); } catch (\RedisException $e) { + unset(self::$redis_rw_handler[0]); $this->master(); $this->get($name); } catch (\Exception $e) { @@ -226,6 +227,7 @@ class Redisd extends Cache $result = $this->handler->set($name, $value); } } catch (\RedisException $e) { + unset(self::$redis_rw_handler[1]); $this->master(true); $this->set($name, $value, $expire); } catch (\Exception $e) { -- Gitee From 9aa90a0fc2c84a4a053fa6bed27db748fbb623dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=98=E7=BC=98?= Date: Tue, 26 Apr 2016 13:47:07 +0800 Subject: [PATCH 19/20] =?UTF-8?q?=E5=8E=BB=E6=8E=89slaveof=20no=20one?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ThinkPHP/Library/Think/Cache/Driver/Redisd.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ThinkPHP/Library/Think/Cache/Driver/Redisd.class.php b/ThinkPHP/Library/Think/Cache/Driver/Redisd.class.php index 30ee64d..b5062d9 100644 --- a/ThinkPHP/Library/Think/Cache/Driver/Redisd.class.php +++ b/ThinkPHP/Library/Think/Cache/Driver/Redisd.class.php @@ -132,7 +132,7 @@ class Redisd extends Cache //如果是slave,断开主从升主,需要手工同步新主的数据到旧主上 //目前这块的逻辑未经过严格测试 - $this->handler->slaveof(); + //$this->handler->slaveof(); } else { //尝试failover,如果有其它节点则进行其它节点的尝试 foreach ($this->options["server_slave"] as $k=>$v) -- Gitee From 3661fd85473a93128d0287233e108ce36919ac3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=98=E7=BC=98?= Date: Wed, 4 May 2016 10:46:50 +0800 Subject: [PATCH 20/20] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ThinkPHP/Library/Think/Cache/Driver/Redisd.class.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/ThinkPHP/Library/Think/Cache/Driver/Redisd.class.php b/ThinkPHP/Library/Think/Cache/Driver/Redisd.class.php index b5062d9..7c9e08e 100644 --- a/ThinkPHP/Library/Think/Cache/Driver/Redisd.class.php +++ b/ThinkPHP/Library/Think/Cache/Driver/Redisd.class.php @@ -19,9 +19,15 @@ 'REDISD_HOST' =>'192.168.1.11,192.168.1.12', //redis服务器ip,多台用逗号隔开;读写分离开启时,第一台负责写,其它[随机]负责读 'REDISD_MASTER_FAILOVER' =>'192.168.1.12', //当主发生故障时,从12断从升级为主,读写均在12 'REDISD_PORT' =>6379, //端口号 - 'REDISD_TIMEOUT' =>30, //连接超时时间 - 'REDISD_PERSISTENT' =>false, //是否长连接 false=短连接,推荐长链接 - 'REDISD_AUTH' =>'', //AUTH认证密码 + 'REDISD_TIMEOUT' =>10, //连接超时时间 + 'REDISD_PERSISTENT' =>true, //是否长连接 false=短连接,推荐长连接 + 'REDISD_AUTH' =>'', //AUTH认证密码,当redis服务直接暴露在外网时需要 + */ + +/** + $redis = \Think\Cache::getInstance(); + $redis->master(true); + $redis->get($redis->getOptions('prefix').'key'); */ namespace Think\Cache\Driver; -- Gitee