兼容memcached缓存

This commit is contained in:
zyx 2019-07-08 11:56:52 +08:00
parent 5d6d0992fc
commit de7bc08397
4 changed files with 79 additions and 6 deletions

View File

@ -9,7 +9,7 @@ $_config = array();
* @example
* $_config['db']['1']['dbhost'] = 'localhost'; // 服务器地址
* $_config['db']['1']['dbuser'] = 'root'; // 用户
* $_config['db']['1']['dbpw'] = 'root';// 密码
* $_config['db']['1']['dbpw'] = '';// 密码
* $_config['db']['1']['dbcharset'] = 'gbk';// 字符集
* $_config['db']['1']['pconnect'] = '0';// 是否持续连接
* $_config['db']['1']['dbname'] = 'x1';// 数据库
@ -90,6 +90,7 @@ $_config['memory']['redis']['server'] = '';
$_config['memory']['redis']['port'] = 6379;
$_config['memory']['redis']['pconnect'] = 1;
$_config['memory']['redis']['timeout'] = 0;
$_config['memory']['redis']['requirepass'] = '';//如果redis需要密码请填写redis密码
/**
* 是否使用 Redis::SERIALIZER_IGBINARY选项,需要igbinary支持,windows下测试时请关闭否则会出>现错误Reading from client: Connection reset by peer
* 支持以下选项默认使用PHP的serializer
@ -104,6 +105,11 @@ $_config['memory']['memcache']['port'] = 11211; // memcache 服务器端口
$_config['memory']['memcache']['pconnect'] = 1; // memcache 是否长久连接
$_config['memory']['memcache']['timeout'] = 1; // memcache 服务器连接超时
$_config['memory']['memcached']['server'] = '127.0.0.1'; // memcached 服务器地址
$_config['memory']['memcached']['port'] = 11211; // memcached 服务器端口
$_config['memory']['memcached']['pconnect'] = 1; // memcached 是否长久连接
$_config['memory']['memcached']['timeout'] = 1; // memcached 服务器连接超时
$_config['memory']['apc'] = 1; // 启动对 apc 的支持
$_config['memory']['xcache'] = 1; // 启动对 xcache 的支持
$_config['memory']['eaccelerator'] = 0; // 启动对 eaccelerator 的支持
@ -124,8 +130,8 @@ $_config['output']['gzip'] = 0; // 是否采用 Gzip 压缩输出
$_config['output']['tplrefresh'] = 1; // 模板自动刷新开关 0=关闭, 1=打开
$_config['output']['language'] = 'zh-cn'; // 页面语言 zh-cn/zh-tw
$_config['output']['language_list']['zh-cn']='简体中文'; // 页面语言 zh-cn/en-us
$_config['output']['language'] = 'zh-CN'; // 页面语言 zh-cn/zh-tw
$_config['output']['language_list']['zh-CN']='简体中文'; // 页面语言 zh-CN/en-US
$_config['output']['staticurl'] = 'static/'; // 站点静态文件路径,“/”结尾
$_config['output']['ajaxvalidate'] = 0; // 是否严格验证 Ajax 页面的真实性 0=关闭1=打开

View File

@ -18,6 +18,7 @@ class dzz_memory extends dzz_base
public function __construct() {
$this->extension['redis'] = extension_loaded('redis');
$this->extension['memcache'] = extension_loaded('memcache');
$this->extension['memcached'] = extension_loaded('memcached');
$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');
@ -36,8 +37,14 @@ class dzz_memory extends dzz_base
$this->memory = null;
}
}
if($this->extension['memcache'] && !empty($config['memcache']['server'])) {
if(!$this->memory->enable && $this->extension['memcached'] && !empty($config['memcached']['server'])) {
$this->memory = new memory_driver_memcached();
$this->memory->init($this->config['memcached']);
if(!$this->memory->enable) {
$this->memory = null;
}
}
if(!$this->memory->enable && $this->extension['memcache'] && !empty($config['memcache']['server'])) {
$this->memory = new memory_driver_memcache();
$this->memory->init($this->config['memcache']);
if(!$this->memory->enable) {

View File

@ -8,19 +8,22 @@ class memory_driver_memcache
{
public $enable;
public $obj;
public
public function init($config) {
if(!empty($config['server'])) {
$this->obj = new Memcache;
if($config['pconnect']) {
$connect = @$this->obj->pconnect($config['server'], $config['port']);
} else {
$connect = @$this->obj->connect($config['server'], $config['port']);
}
$this->enable = $connect ? true : false;
}
}
public function get($key) {
return $this->obj->get($key);
}

View File

@ -0,0 +1,57 @@
<?php
if(!defined('IN_DZZ')) {
exit('Access Denied');
}
class memory_driver_memcached
{
public $enable;
public $obj;
public function init($config) {
if(!empty($config['server'])) {
$this->obj = new Memcached();
$connect = $this->connectd($config['server'], $config['port']);
$this->enable = $connect ? true : false;
}
}
public function connectd($host , $port){
$servers = $this->obj->getServerList();
if(is_array($servers)) {
foreach ($servers as $server) {
if($server['host'] == $host and $server['port'] == $port) return true;
}
}
return $this->obj->addServer($host , $port);
}
public function get($key) {
return $this->obj->get($key);
}
public function getMulti($keys) {
return $this->obj->get($keys);
}
public function set($key, $value, $ttl = 0) {
return $this->obj->set($key, $value, $ttl);
}
public function rm($key) {
return $this->obj->delete($key);
}
public function clear() {
return $this->obj->flush();
}
public function inc($key, $step = 1) {
return $this->obj->increment($key, $step);
}
public function dec($key, $step = 1) {
return $this->obj->decrement($key, $step);
}
}
?>