优化 缓存机制改为扩展形式,添加一个 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

@ -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'])) {

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);
$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 = '') {

View File

@ -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;
}
}
}

View File

@ -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) {

View File

@ -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) {

View File

@ -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);
}

View File

@ -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();
}
}
?>

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}