移除sae驱动 改为扩展包

This commit is contained in:
thinkphp
2016-07-07 11:55:57 +08:00
parent d5353d1e32
commit e35a0a88ac
5 changed files with 1 additions and 304 deletions

View File

@@ -144,7 +144,7 @@ return [
// +----------------------------------------------------------------------
'log' => [
// 日志记录方式,内置 file sae socket 支持扩展
// 日志记录方式,内置 file socket 支持扩展
'type' => 'File',
// 日志保存目录
'path' => LOG_PATH,

View File

@@ -1,122 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\cache\driver;
use think\Cache;
use think\Exception;
/**
* SAE Memcache缓存驱动
* @author liu21st <liu21st@gmail.com>
*/
class Sae
{
protected $handler = null;
protected $options = [
'host' => '127.0.0.1',
'port' => 11211,
'expire' => 0,
'timeout' => false,
'persistent' => false,
'prefix' => '',
];
/**
* 架构函数
* @param array $options 缓存参数
* @access public
*/
public function __construct($options = [])
{
if (!function_exists('memcache_init')) {
throw new \BadFunctionCallException('must run at sae');
}
$this->handler = memcache_init();
if (!$this->handler) {
throw new Exception('memcache init error');
}
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
}
/**
* 读取缓存
* @access public
* @param string $name 缓存变量名
* @return mixed
*/
public function get($name)
{
return $this->handler->get($_SERVER['HTTP_APPVERSION'] . '/' . $this->options['prefix'] . $name);
}
/**
* 写入缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $value 存储数据
* @param integer $expire 有效时间(秒)
* @return bool
*/
public function set($name, $value, $expire = null)
{
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$name = $this->options['prefix'] . $name;
if ($this->handler->set($_SERVER['HTTP_APPVERSION'] . '/' . $name, $value, 0, $expire)) {
return true;
}
return false;
}
/**
* 删除缓存
* @param string $name 缓存变量名
* @param bool|false $ttl
* @return bool
*/
public function rm($name, $ttl = false)
{
$name = $_SERVER['HTTP_APPVERSION'] . '/' . $this->options['prefix'] . $name;
return false === $ttl ?
$this->handler->delete($name) :
$this->handler->delete($name, $ttl);
}
/**
* 清除缓存
* @access public
* @return bool
*/
public function clear()
{
return $this->handler->flush();
}
/**
* 获得SaeKv对象
*/
private function getKv()
{
static $kv;
if (!$kv) {
$kv = new \SaeKV();
if (!$kv->init()) {
throw new Exception('KVDB init error');
}
}
return $kv;
}
}

View File

@@ -1,71 +0,0 @@
<?php
namespace think\log\driver;
/**
* 调试输出到SAE
*/
class Sae
{
protected $config = [
'log_time_format' => ' c ',
];
// 实例化并传入参数
public function __construct(array $config = [])
{
$this->config = array_merge($this->config, $config);
}
/**
* 日志写入接口
* @access public
* @param array $log 日志信息
* @return bool
*/
public function save(array $log = [])
{
static $is_debug = null;
$now = date($this->config['log_time_format']);
// 获取基本信息
if (isset($_SERVER['HTTP_HOST'])) {
$current_uri = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
} else {
$current_uri = "cmd:" . implode(' ', $_SERVER['argv']);
}
$runtime = number_format(microtime(true), 8, '.', '') - THINK_START_TIME;
$reqs = number_format(1 / $runtime, 2);
$time_str = " [运行时间:{$runtime}s] [吞吐率:{$reqs}req/s]";
$memory_use = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2);
$memory_str = " [内存消耗:{$memory_use}kb]";
$file_load = " [文件加载:" . count(get_included_files()) . "]";
$info = '[ log ] ' . $current_uri . $time_str . $memory_str . $file_load . "\r\n";
foreach ($log as $type => $val) {
foreach ($val as $msg) {
if (!is_string($msg)) {
$msg = var_export($msg, true);
}
$info .= '[ ' . $type . ' ] ' . $msg . "\r\n";
}
}
$logstr = "[{$now}] {$_SERVER['SERVER_ADDR']} {$_SERVER['REMOTE_ADDR']} {$_SERVER['REQUEST_URI']}\r\n{$info}\r\n";
if (is_null($is_debug)) {
$appSettings = [];
preg_replace_callback('@(\w+)\=([^;]*)@', function ($match) use (&$appSettings) {
$appSettings[$match['1']] = $match['2'];
}, $_SERVER['HTTP_APPCOOKIE']);
$is_debug = in_array($_SERVER['HTTP_APPVERSION'], explode(',', $appSettings['debug'])) ? true : false;
}
if ($is_debug) {
sae_set_display_errors(false); //记录日志不将日志打印出来
}
sae_debug($logstr);
if ($is_debug) {
sae_set_display_errors(true);
}
return true;
}
}

View File

@@ -1,109 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\template\driver;
use think\Exception;
class Sae
{
// mc 对象
private $mc;
// 编译缓存内容
private $contents = [];
/**
* 架构函数
* @access public
*/
public function __construct()
{
if (!function_exists('memcache_init')) {
throw new Exception('请在SAE平台上运行代码。');
}
$this->mc = @memcache_init();
if (!$this->mc) {
throw new Exception('您未开通Memcache服务请在SAE管理平台初始化Memcache服务');
}
}
/**
* 写入编译缓存
* @param string $cacheFile 缓存的文件名
* @param string $content 缓存的内容
* @return void|array
*/
public function write($cacheFile, $content)
{
// 添加写入时间
$content = $_SERVER['REQUEST_TIME'] . $content;
if (!$this->mc->set($cacheFile, $content, MEMCACHE_COMPRESSED, 0)) {
throw new Exception('sae mc write error:' . $cacheFile);
} else {
$this->contents[$cacheFile] = $content;
return true;
}
}
/**
* 读取编译编译
* @param string $cacheFile 缓存的文件名
* @param array $vars 变量数组
* @return void
*/
public function read($cacheFile, $vars = [])
{
if (!empty($vars) && is_array($vars)) {
extract($vars, EXTR_OVERWRITE);
}
eval('?>' . $this->get($cacheFile, 'content'));
}
/**
* 检查编译缓存是否有效
* @param string $cacheFile 缓存的文件名
* @param int $cacheTime 缓存时间
* @return boolean
*/
public function check($cacheFile, $cacheTime)
{
$mtime = $this->get($cacheFile, 'mtime');
if (0 != $cacheTime && $_SERVER['REQUEST_TIME'] > $mtime + $cacheTime) {
// 缓存是否在有效期
return false;
}
return true;
}
/**
* 读取文件信息
* @access private
* @param string $filename 文件名
* @param string $name 信息名 mtime或者content
* @return boolean
*/
private function get($filename, $name)
{
if (!isset($this->contents[$filename])) {
$this->contents[$filename] = $this->mc->get($filename);
}
$content = $this->contents[$filename];
if (false === $content) {
return false;
}
$info = array(
'mtime' => substr($content, 0, 10),
'content' => substr($content, 10),
);
return $info[$name];
}
}

View File

@@ -53,7 +53,6 @@ class loaderTest extends \PHPUnit_Framework_TestCase
public function testImport()
{
$this->assertEquals(true, Loader::import('think.log.driver.Sae'));
$this->assertEquals(false, Loader::import('think.log.driver.MyTest'));
}