From 64cfa8587757fdba1e30296f5b7d952c57ff5624 Mon Sep 17 00:00:00 2001 From: Discuz! Date: Wed, 8 Feb 2017 12:41:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E6=9C=BA=E5=88=B6=E6=94=B9=E4=B8=BA=E6=89=A9=E5=B1=95=E5=BD=A2?= =?UTF-8?q?=E5=BC=8F=EF=BC=8C=E6=B7=BB=E5=8A=A0=E4=B8=80=E4=B8=AA=20memory?= =?UTF-8?q?=5Fdriver=5Fxx=20=E6=96=87=E4=BB=B6=E5=8D=B3=E5=8F=AF=E8=87=AA?= =?UTF-8?q?=E7=94=B1=E6=89=A9=E5=B1=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- upload/source/admincp/admincp_setting.php | 71 ++++++------------- upload/source/class/discuz/discuz_memory.php | 45 +++--------- .../source/class/memory/memory_driver_apc.php | 17 +++-- .../class/memory/memory_driver_apcu.php | 9 ++- .../memory/memory_driver_eaccelerator.php | 15 ++-- .../class/memory/memory_driver_memcache.php | 20 ++++-- .../class/memory/memory_driver_redis.php | 48 ++++++++----- .../class/memory/memory_driver_wincache.php | 17 +++-- .../class/memory/memory_driver_xcache.php | 17 +++-- .../source/class/memory/memory_driver_yac.php | 13 +++- 10 files changed, 139 insertions(+), 133 deletions(-) diff --git a/upload/source/admincp/admincp_setting.php b/upload/source/admincp/admincp_setting.php index 10a6de5..a0eaf28 100644 --- a/upload/source/admincp/admincp_setting.php +++ b/upload/source/admincp/admincp_setting.php @@ -2210,56 +2210,27 @@ EOT; $cache_extension = C::memory()->extension; $cache_config = C::memory()->config; $cache_type = C::memory()->type; - - $redis = array('Redis', - $cache_extension['redis'] ? cplang('setting_memory_php_enable') : cplang('setting_memory_php_disable'), - $cache_config['redis']['server'] ? cplang('open') : cplang('closed'), - $cache_type == 'redis' ? $do_clear_link : '--' - ); - - $memcache = array('memcache', - $cache_extension['memcache'] ? cplang('setting_memory_php_enable') : cplang('setting_memory_php_disable'), - $cache_config['memcache']['server'] ? cplang('open') : cplang('closed'), - $cache_type == 'memcache' ? $do_clear_link : '--' - ); - $apc = array('APC', - $cache_extension['apc'] ? cplang('setting_memory_php_enable') : cplang('setting_memory_php_disable'), - $cache_config['apc'] ? cplang('open') : cplang('closed'), - $cache_type == 'apc' ? $do_clear_link : '--' - ); - $xcache = array('Xcache', - $cache_extension['xcache'] ? cplang('setting_memory_php_enable') : cplang('setting_memory_php_disable'), - $cache_config['xcache'] ? cplang('open') : cplang('closed'), - $cache_type == 'xcache' ? $do_clear_link : '--' - ); - $ea = array('eAccelerator', - $cache_extension['eaccelerator'] ? cplang('setting_memory_php_enable') : cplang('setting_memory_php_disable'), - $cache_config['eaccelerator'] ? cplang('open') : cplang('closed'), - $cache_type == 'eaccelerator' ? $do_clear_link : '--' - ); - $wincache = array('wincache', - $cache_extension['wincache'] ? cplang('setting_memory_php_enable') : cplang('setting_memory_php_disable'), - $cache_config['wincache'] ? cplang('open') : cplang('closed'), - $cache_type == 'wincache' ? $do_clear_link : '--' - ); - $yac = array('Yac', - $cache_extension['yac'] ? cplang('setting_memory_php_enable') : cplang('setting_memory_php_disable'), - $cache_config['yac'] ? cplang('open') : cplang('closed'), - $cache_type == 'yac' ? $do_clear_link : '--' - ); - $apcu = array('APCu', - $cache_extension['apcu'] ? cplang('setting_memory_php_enable') : cplang('setting_memory_php_disable'), - $cache_config['apcu'] ? cplang('open') : cplang('closed'), - $cache_type == 'apcu' ? $do_clear_link : '--' - ); - showtablerow('', array('width="100"', 'width="120"', 'width="120"'), $redis); - showtablerow('', '', $memcache); - showtablerow('', '', $apc); - showtablerow('', '', $xcache); - showtablerow('', '', $ea); - showtablerow('', '', $wincache); - showtablerow('', '', $yac); - showtablerow('', '', $apcu); + + $dir = DISCUZ_ROOT.'./source/class/memory'; + $qaadir = dir($dir); + $cachelist = array(); + while($entry = $qaadir->read()) { + if(!in_array($entry, array('.', '..')) && preg_match("/^memory\_driver\_[\w\.]+$/", $entry) && substr($entry, -4) == '.php' && strlen($entry) < 30 && is_file($dir.'/'.$entry)) { + $cache = str_replace(array('.php', 'memory_driver_'), '', $entry); + $class_name = 'memory_driver_'.$cache; + $memory = new $class_name(); + $available = is_array($cache_config[$cache]) ? !empty($cache_config[$cache]['server']) : !empty($cache_config[$cache]); + $cachelist[] = array($memory->cacheName, + $memory->env($config) ? cplang('setting_memory_php_enable') : cplang('setting_memory_php_disable'), + $available ? cplang('open') : cplang('closed'), + $cache_type == $cache ? $do_clear_link : '--' + ); + } + } + + foreach($cachelist as $cache) { + showtablerow('', array('width="100"', 'width="120"', 'width="120"'), $cache); + } showtablefooter(); if(!isset($setting['memory'])) { diff --git a/upload/source/class/discuz/discuz_memory.php b/upload/source/class/discuz/discuz_memory.php index fd20885..7fc0a91 100644 --- a/upload/source/class/discuz/discuz_memory.php +++ b/upload/source/class/discuz/discuz_memory.php @@ -23,50 +23,27 @@ class discuz_memory extends discuz_base public $debug = array(); public function __construct() { - $this->extension['redis'] = extension_loaded('redis'); - $this->extension['memcache'] = extension_loaded('memcache'); - $this->extension['apc'] = function_exists('apc_cache_info') && @apc_cache_info(); - $this->extension['xcache'] = function_exists('xcache_get'); - $this->extension['eaccelerator'] = function_exists('eaccelerator_get'); - $this->extension['wincache'] = function_exists('wincache_ucache_meminfo') && wincache_ucache_meminfo(); - $this->extension['yac'] = extension_loaded('Yac'); - $this->extension['apcu'] = function_exists('apcu_cache_info') && @apcu_cache_info(); } public function init($config) { $this->config = $config; $this->prefix = empty($config['prefix']) ? substr(md5($_SERVER['HTTP_HOST']), 0, 6).'_' : $config['prefix']; + unset($this->config['prefix']); - - if($this->extension['redis'] && !empty($config['redis']['server'])) { - $this->memory = new memory_driver_redis(); - $this->memory->init($this->config['redis']); - if(!$this->memory->enable) { - $this->memory = null; - } - } - - if($this->extension['memcache'] && !empty($config['memcache']['server'])) { - $this->memory = new memory_driver_memcache(); - $this->memory->init($this->config['memcache']); - if(!$this->memory->enable) { - $this->memory = null; - } - } - - foreach(array('apc', 'eaccelerator', 'xcache', 'wincache', 'yac', 'apcu') as $cache) { - if(!is_object($this->memory) && $this->extension[$cache] && $this->config[$cache]) { + foreach($this->config as $cache => $config) { + $available = is_array($config) ? !empty($config['server']) : !empty($config); + if($available && !is_object($this->memory)) { $class_name = 'memory_driver_'.$cache; $this->memory = new $class_name(); - $this->memory->init(null); + $this->memory->init($config); + if(!$this->memory->enable) { + $this->memory = null; + } else { + $this->type = $cache; + $this->enable = true; + } } } - - if(is_object($this->memory)) { - $this->enable = true; - $this->type = str_replace('memory_driver_', '', get_class($this->memory)); - } - } public function get($key, $prefix = '') { diff --git a/upload/source/class/memory/memory_driver_apc.php b/upload/source/class/memory/memory_driver_apc.php index 6613bf4..3415d25 100644 --- a/upload/source/class/memory/memory_driver_apc.php +++ b/upload/source/class/memory/memory_driver_apc.php @@ -6,16 +6,21 @@ * * $Id: memory_driver_apc.php 27635 2012-02-08 06:38:31Z zhangguosheng $ */ - -if(!defined('IN_DISCUZ')) { +if (!defined('IN_DISCUZ')) { exit('Access Denied'); } -class memory_driver_apc -{ +class memory_driver_apc { + + public $cacheName = 'APC'; + public $enable; + + public function env() { + return function_exists('apc_cache_info') && @apc_cache_info(); + } public function init($config) { - + $this->enable = $this->env(); } public function get($key) { @@ -42,4 +47,4 @@ class memory_driver_apc return apc_dec($key, $step) !== false ? apc_fetch($key) : false; } -} \ No newline at end of file +} diff --git a/upload/source/class/memory/memory_driver_apcu.php b/upload/source/class/memory/memory_driver_apcu.php index ea2bed9..7d3c9e9 100644 --- a/upload/source/class/memory/memory_driver_apcu.php +++ b/upload/source/class/memory/memory_driver_apcu.php @@ -12,8 +12,15 @@ if (!defined('IN_DISCUZ')) { class memory_driver_apcu { + public $cacheName = 'APCu'; + public $enable; + + public function env() { + return function_exists('apcu_cache_info') && @apcu_cache_info(); + } + public function init($config) { - + $this->enable = $this->env(); } public function get($key) { diff --git a/upload/source/class/memory/memory_driver_eaccelerator.php b/upload/source/class/memory/memory_driver_eaccelerator.php index 9056dd4..58da4f1 100644 --- a/upload/source/class/memory/memory_driver_eaccelerator.php +++ b/upload/source/class/memory/memory_driver_eaccelerator.php @@ -6,16 +6,21 @@ * * $Id: memory_driver_eaccelerator.php 30457 2012-05-30 01:48:49Z zhangguosheng $ */ - -if(!defined('IN_DISCUZ')) { +if (!defined('IN_DISCUZ')) { exit('Access Denied'); } -class memory_driver_eaccelerator -{ +class memory_driver_eaccelerator { + + public $cacheName = 'eAccelerator'; + public $enable; + + public function env() { + return function_exists('eaccelerator_get'); + } public function init($config) { - + $this->enable = $this->env(); } public function get($key) { diff --git a/upload/source/class/memory/memory_driver_memcache.php b/upload/source/class/memory/memory_driver_memcache.php index f89fdbc..8ec59cb 100644 --- a/upload/source/class/memory/memory_driver_memcache.php +++ b/upload/source/class/memory/memory_driver_memcache.php @@ -6,20 +6,27 @@ * * $Id: memory_driver_memcache.php 27449 2012-02-01 05:32:35Z zhangguosheng $ */ - -if(!defined('IN_DISCUZ')) { +if (!defined('IN_DISCUZ')) { exit('Access Denied'); } -class memory_driver_memcache -{ +class memory_driver_memcache { + + public $cacheName = 'MemCache'; public $enable; public $obj; + public function env() { + return extension_loaded('memcache'); + } + public function init($config) { - if(!empty($config['server'])) { + if (!$this->env()) { + $this->enable = false; + } + if (!empty($config['server'])) { $this->obj = new Memcache; - if($config['pconnect']) { + if ($config['pconnect']) { $connect = @$this->obj->pconnect($config['server'], $config['port']); } else { $connect = @$this->obj->connect($config['server'], $config['port']); @@ -35,6 +42,7 @@ class memory_driver_memcache public function getMulti($keys) { return $this->obj->get($keys); } + public function set($key, $value, $ttl = 0) { return $this->obj->set($key, $value, MEMCACHE_COMPRESSED, $ttl); } diff --git a/upload/source/class/memory/memory_driver_redis.php b/upload/source/class/memory/memory_driver_redis.php index c2df5c7..11c4078 100644 --- a/upload/source/class/memory/memory_driver_redis.php +++ b/upload/source/class/memory/memory_driver_redis.php @@ -6,26 +6,39 @@ * * $Id: memory_driver_redis.php 33336 2013-05-29 02:05:10Z andyzheng $ */ +if (!defined('IN_DISCUZ')) { + exit('Access Denied'); +} -class memory_driver_redis -{ +class memory_driver_redis { + + public $cacheName = 'Redis'; var $enable; var $obj; + + public function env() { + return extension_loaded('redis'); + } function init($config) { - if(!empty($config['server'])) { + if(!$this->env()) { + $this->enable = false; + } + + if (!empty($config['server'])) { try { $this->obj = new Redis(); - if($config['pconnect']) { + if ($config['pconnect']) { $connect = @$this->obj->pconnect($config['server'], $config['port']); } else { $connect = @$this->obj->connect($config['server'], $config['port']); } } catch (RedisException $e) { + } $this->enable = $connect ? true : false; - if($this->enable) { - if($config['requirepass']) { + if ($this->enable) { + if ($config['requirepass']) { $this->obj->auth($config['requirepass']); } @$this->obj->setOption(Redis::OPT_SERIALIZER, $config['serializer']); @@ -35,7 +48,7 @@ class memory_driver_redis function &instance() { static $object; - if(empty($object)) { + if (empty($object)) { $object = new memory_driver_redis(); $object->init(getglobal('config/memory/redis')); } @@ -43,7 +56,7 @@ class memory_driver_redis } function get($key) { - if(is_array($key)) { + if (is_array($key)) { return $this->getMulti($key); } return $this->obj->get($key); @@ -53,8 +66,8 @@ class memory_driver_redis $result = $this->obj->getMultiple($keys); $newresult = array(); $index = 0; - foreach($keys as $key) { - if($result[$index] !== false) { + foreach ($keys as $key) { + if ($result[$index] !== false) { $newresult[$key] = $result[$index]; } $index++; @@ -63,12 +76,12 @@ class memory_driver_redis return $newresult; } - function select($db=0) { + function select($db = 0) { return $this->obj->select($db); } function set($key, $value, $ttl = 0) { - if($ttl) { + if ($ttl) { return $this->obj->setex($key, $ttl, $value); } else { return $this->obj->set($key, $value); @@ -79,11 +92,11 @@ class memory_driver_redis return $this->obj->delete($key); } - function setMulti($arr, $ttl=0) { - if(!is_array($arr)) { + function setMulti($arr, $ttl = 0) { + if (!is_array($arr)) { return FALSE; } - foreach($arr as $key => $v) { + foreach ($arr as $key => $v) { $this->set($key, $v, $ttl); } return TRUE; @@ -121,7 +134,7 @@ class memory_driver_redis return $this->obj->keys($key); } - function expire($key, $second){ + function expire($key, $second) { return $this->obj->expire($key, $second); } @@ -145,7 +158,7 @@ class memory_driver_redis return $this->obj->hVals($key); } - function hIncrBy($key, $field, $incr){ + function hIncrBy($key, $field, $incr) { return $this->obj->hIncrBy($key, $field, $incr); } @@ -164,6 +177,7 @@ class memory_driver_redis function clear() { return $this->obj->flushAll(); } + } ?> \ No newline at end of file diff --git a/upload/source/class/memory/memory_driver_wincache.php b/upload/source/class/memory/memory_driver_wincache.php index fdffad8..84873af 100644 --- a/upload/source/class/memory/memory_driver_wincache.php +++ b/upload/source/class/memory/memory_driver_wincache.php @@ -6,16 +6,21 @@ * * $Id: memory_driver_wincache.php 31432 2012-08-28 03:04:18Z zhangguosheng $ */ - -if(!defined('IN_DISCUZ')) { +if (!defined('IN_DISCUZ')) { exit('Access Denied'); } -class memory_driver_wincache -{ +class memory_driver_wincache { + + public $cacheName = 'WinCache'; + public $enable; + + public function env() { + return function_exists('wincache_ucache_meminfo') && wincache_ucache_meminfo(); + } public function init($config) { - + $this->enable = $this->env(); } public function get($key) { @@ -46,4 +51,4 @@ class memory_driver_wincache return wincache_ucache_dec($key, $step); } -} \ No newline at end of file +} diff --git a/upload/source/class/memory/memory_driver_xcache.php b/upload/source/class/memory/memory_driver_xcache.php index 08820a7..569fb03 100644 --- a/upload/source/class/memory/memory_driver_xcache.php +++ b/upload/source/class/memory/memory_driver_xcache.php @@ -6,16 +6,21 @@ * * $Id: memory_driver_xcache.php 27449 2012-02-01 05:32:35Z zhangguosheng $ */ - -if(!defined('IN_DISCUZ')) { +if (!defined('IN_DISCUZ')) { exit('Access Denied'); } -class memory_driver_xcache -{ +class memory_driver_xcache { + public $cacheName = 'XCache'; + public $enable; + + public function env() { + return function_exists('xcache_get'); + } + public function init($config) { - + $this->enable = $this->env(); } public function get($key) { @@ -42,4 +47,4 @@ class memory_driver_xcache return xcache_dec($key, $step); } -} \ No newline at end of file +} diff --git a/upload/source/class/memory/memory_driver_yac.php b/upload/source/class/memory/memory_driver_yac.php index 7bff810..c677024 100644 --- a/upload/source/class/memory/memory_driver_yac.php +++ b/upload/source/class/memory/memory_driver_yac.php @@ -12,10 +12,19 @@ if (!defined('IN_DISCUZ')) { class memory_driver_yac { + public $cacheName = 'Yac'; private $object = null; + public $enable; + + public function env() { + return extension_loaded('Yac'); + } public function init($config) { - $this->object = new yac(); + $this->enable = $this->env(); + if ($this->enable) { + $this->object = new yac(); + } } public function get($key) { @@ -60,4 +69,4 @@ class memory_driver_yac { return $this->set($key, $old - $step); } -} \ No newline at end of file +}