memcached不存在时检测memcache,解决windows没有memcached.dll的问题

This commit is contained in:
huangdijia
2016-01-28 17:51:42 +08:00
parent dea723e1ee
commit 635182dda2
2 changed files with 304 additions and 222 deletions

View File

@@ -9,13 +9,57 @@
// | Author: liu21st <liu21st@gmail.com> // | Author: liu21st <liu21st@gmail.com>
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace think\cache\driver; namespace {
// 检测php环境
if (!extension_loaded('memcached')) {
if (!extension_loaded('memcache')) {
throw new Exception('_NOT_SUPPERT_:memcache or memcachd');
}
class Memcached extends Memcache
{
const OPT_CONNECT_TIMEOUT = 14;
private $timeout = 1000;
use think\Cache; public function addServers(array $servers = [])
use think\Exception; {
if(empty($servers)) return;
foreach ($servers as $key => $server) {
if (empty($server[0])) continue;
$this->addServer(
$server[0],
!empty($server[1]) ? $server[1] : 11211,
true,
!empty($server[2]) ? $server[1] : 1,
$this->timeout > 0 ? ($this->timeout/1000) : 1
);
}
}
class Memcached public function setOption(int $option, mixed $value)
{ {
switch ($option) {
case self::OPT_CONNECT_TIMEOUT:
$this->timeout = $value;
break;
default:
break;
}
}
public function set(string $key, mixed $value, $expiration = 0)
{
return $this->set($key, $value, MEMCACHE_COMPRESSED, $expiration);
}
}
}
}
namespace think\cache\driver {
use think\Cache;
use think\Exception;
class Memcached
{
protected $handler = null; protected $handler = null;
protected $options = [ protected $options = [
'host' => '127.0.0.1', 'host' => '127.0.0.1',
@@ -32,9 +76,6 @@ class Memcached
*/ */
public function __construct($options = []) public function __construct($options = [])
{ {
if (!extension_loaded('memcached')) {
throw new Exception('_NOT_SUPPERT_:memcached');
}
if (!empty($options)) { if (!empty($options)) {
$this->options = array_merge($this->options, $options); $this->options = array_merge($this->options, $options);
} }
@@ -133,4 +174,5 @@ class Memcached
{ {
return $this->handler->flush(); return $this->handler->flush();
} }
}
} }

View File

@@ -8,14 +8,57 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com> // | Author: liu21st <liu21st@gmail.com>
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace {
// 检测php环境
if (!extension_loaded('memcached')) {
if (!extension_loaded('memcache')) {
throw new Exception('_NOT_SUPPERT_:memcache or memcached');
}
class Memcached extends Memcache
{
const OPT_CONNECT_TIMEOUT = 14;
private $timeout = 1000;
namespace think\session\driver; public function addServers(array $servers = [])
{
if(empty($servers)) return;
foreach ($servers as $key => $server) {
if (empty($server[0])) continue;
$this->addServer(
$server[0],
!empty($server[1]) ? $server[1] : 11211,
true,
!empty($server[2]) ? $server[1] : 1,
$this->timeout > 0 ? ($this->timeout/1000) : 1
);
}
}
use SessionHandler; public function setOption(int $option, mixed $value)
use think\Exception; {
switch ($option) {
case self::OPT_CONNECT_TIMEOUT:
$this->timeout = $value;
break;
default:
break;
}
}
class Memcached extends SessionHandler public function set(string $key, mixed $value, $expiration = 0)
{ {
return $this->set($key, $value, MEMCACHE_COMPRESSED, $expiration);
}
}
}
}
namespace think\session\driver {
use SessionHandler;
use think\Exception;
class Memcached extends SessionHandler
{
protected $handler = null; protected $handler = null;
protected $config = [ protected $config = [
'host' => '127.0.0.1', // memcache主机 'host' => '127.0.0.1', // memcache主机
@@ -38,10 +81,6 @@ class Memcached extends SessionHandler
*/ */
public function open($savePath, $sessName) public function open($savePath, $sessName)
{ {
// 检测php环境
if (!extension_loaded('memcached')) {
throw new Exception('_NOT_SUPPERT_:memcached');
}
$this->handler = new \Memcached; $this->handler = new \Memcached;
// 设置连接超时时间(单位:毫秒) // 设置连接超时时间(单位:毫秒)
if ($this->config['timeout'] > 0) { if ($this->config['timeout'] > 0) {
@@ -114,4 +153,5 @@ class Memcached extends SessionHandler
{ {
return true; return true;
} }
}
} }