优化 缓存机制改为扩展形式,添加一个 memory_driver_xx 文件即可自由扩展

This commit is contained in:
Discuz! 2017-02-08 12:41:58 +08:00
parent ffba5153b1
commit 64cfa85877
10 changed files with 139 additions and 133 deletions

View File

@ -2211,55 +2211,26 @@ EOT;
$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 : '--'
$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 : '--'
);
}
}
$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);
foreach($cachelist as $cache) {
showtablerow('', array('width="100"', 'width="120"', 'width="120"'), $cache);
}
showtablefooter();
if(!isset($setting['memory'])) {

View File

@ -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);
}
}
if(is_object($this->memory)) {
$this->memory->init($config);
if(!$this->memory->enable) {
$this->memory = null;
} else {
$this->type = $cache;
$this->enable = true;
$this->type = str_replace('memory_driver_', '', get_class($this->memory));
}
}
}
}
public function get($key, $prefix = '') {

View File

@ -6,16 +6,21 @@
*
* $Id: memory_driver_apc.php 27635 2012-02-08 06:38:31Z zhangguosheng $
*/
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) {

View File

@ -12,8 +12,15 @@ if (!defined('IN_DISCUZ')) {
class memory_driver_apcu {
public function init($config) {
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) {

View File

@ -6,16 +6,21 @@
*
* $Id: memory_driver_eaccelerator.php 30457 2012-05-30 01:48:49Z zhangguosheng $
*/
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) {

View File

@ -6,17 +6,24 @@
*
* $Id: memory_driver_memcache.php 27449 2012-02-01 05:32:35Z zhangguosheng $
*/
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 (!$this->env()) {
$this->enable = false;
}
if (!empty($config['server'])) {
$this->obj = new Memcache;
if ($config['pconnect']) {
@ -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);
}

View File

@ -6,13 +6,25 @@
*
* $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(!$this->env()) {
$this->enable = false;
}
if (!empty($config['server'])) {
try {
$this->obj = new Redis();
@ -22,6 +34,7 @@ class memory_driver_redis
$connect = @$this->obj->connect($config['server'], $config['port']);
}
} catch (RedisException $e) {
}
$this->enable = $connect ? true : false;
if ($this->enable) {
@ -164,6 +177,7 @@ class memory_driver_redis
function clear() {
return $this->obj->flushAll();
}
}
?>

View File

@ -6,16 +6,21 @@
*
* $Id: memory_driver_wincache.php 31432 2012-08-28 03:04:18Z zhangguosheng $
*/
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) {

View File

@ -6,16 +6,21 @@
*
* $Id: memory_driver_xcache.php 27449 2012-02-01 05:32:35Z zhangguosheng $
*/
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) {

View File

@ -12,11 +12,20 @@ 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->enable = $this->env();
if ($this->enable) {
$this->object = new yac();
}
}
public function get($key) {
return $this->object->get($key);