增加sae相关驱动

This commit is contained in:
thinkphp
2015-12-10 15:17:32 +08:00
parent 9f0ee6acbb
commit 7b0fe7286d
5 changed files with 291 additions and 34 deletions

155
library/think/cache/driver/sae.php vendored Normal file
View File

@@ -0,0 +1,155 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2015 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\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,
'length' => 0,
];
/**
* 架构函数
* @param array $options 缓存参数
* @access public
*/
public function __construct($options = [])
{
if (!function_exists('memcache_init')) {
throw new Exception('请在SAE平台上运行代码。');
}
$this->handler = memcache_init();
if (!$this->handler) {
throw new Exception('您未开通Memcache服务请在SAE管理平台初始化Memcache服务');
}
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)) {
if ($this->options['length'] > 0) {
// 记录缓存队列
$this->queue($name);
}
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请在SAE管理平台初始化KVDB服务');
}
}
return $kv;
}
/**
* 队列缓存
* @access protected
* @param string $key 队列名
* @return mixed
*/
//[sae] 下重写queque队列缓存方法
protected function queue($key)
{
$queue_name = isset($this->options['queue_name']) ? $this->options['queue_name'] : 'think_queue';
$kv = $this->getKv();
$value = $kv->get($queue_name);
if (!$value) {
$value = [];
}
// 进列
if (false === array_search($key, $value)) {
array_push($value, $key);
}
if (count($value) > $this->options['length']) {
// 出列
$key = array_shift($value);
// 删除缓存
$this->rm($key);
}
return $kv->set($queue_name, $value);
}
}

View File

@@ -186,7 +186,7 @@ class Template
private function checkCache($template, $cacheFile)
{
if (!$this->config['tpl_cache']) {
// 优先对配置设定检测
// 优先对配置设定检测
return false;
}
// 检查编译存储是否有效

View File

@@ -0,0 +1,98 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2015 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服务');
}
}
// 写入编译缓存
public function write($cacheFile, $content)
{
// 添加写入时间
$content = 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;
}
}
// 读取编译编译
public function read($cacheFile, $vars)
{
if (!is_null($vars)) {
extract($vars, EXTR_OVERWRITE);
}
eval('?>' . $this->get($cacheFile, 'content'));
}
// 检查编译缓存是否有效
public function check($template, $cacheFile, $cacheTime)
{
$mtime = $this->get($cacheFile, 'mtime');
if (!$this->get($cacheFile, 'content') || (is_file($template) && filemtime($template) > $mtime)) {
// 模板文件如果有更新则缓存需要更新
return false;
}if (0 != $cacheTime && time() > $mtime + $cacheTime) {
// 缓存是否在有效期
return false;
} else {
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

@@ -20,31 +20,32 @@ return [
// 别名定义
'alias' => [
'think\App' => CORE_PATH . 'app' . EXT,
'think\Log' => CORE_PATH . 'log' . EXT,
'think\log\driver\File' => CORE_PATH . 'log' . DS . 'driver' . DS . 'file' . EXT,
'think\Config' => CORE_PATH . 'config' . EXT,
'think\Create' => CORE_PATH . 'create' . EXT,
'think\Route' => CORE_PATH . 'route' . EXT,
'think\Exception' => CORE_PATH . 'exception' . EXT,
'think\Model' => CORE_PATH . 'model' . EXT,
'think\Db' => CORE_PATH . 'db' . EXT,
'think\Db\Driver' => CORE_PATH . 'db' . DS . 'driver' . EXT,
'think\Template' => CORE_PATH . 'template' . EXT,
'think\view\driver\Think' => CORE_PATH . 'view' . DS . 'driver' . DS . 'think' . EXT,
'think\template\driver\File' => CORE_PATH . 'template' . DS . 'driver' . DS . 'file' . EXT,
'think\Error' => CORE_PATH . 'error' . EXT,
'think\Cache' => CORE_PATH . 'cache' . EXT,
'think\cache\driver\File' => CORE_PATH . 'cache' . DS . 'driver' . DS . 'file' . EXT,
'think\Hook' => CORE_PATH . 'hook' . EXT,
'think\Session' => CORE_PATH . 'session' . EXT,
'think\Cookie' => CORE_PATH . 'cookie' . EXT,
'think\Config' => CORE_PATH . 'config' . EXT,
'think\Controller' => CORE_PATH . 'controller' . EXT,
'think\View' => CORE_PATH . 'view' . EXT,
'think\Url' => CORE_PATH . 'url' . EXT,
'think\Cookie' => CORE_PATH . 'cookie' . EXT,
'think\Create' => CORE_PATH . 'create' . EXT,
'think\Db' => CORE_PATH . 'db' . EXT,
'think\Debug' => CORE_PATH . 'debug' . EXT,
'think\Error' => CORE_PATH . 'error' . EXT,
'think\Exception' => CORE_PATH . 'exception' . EXT,
'think\Hook' => CORE_PATH . 'hook' . EXT,
'think\Input' => CORE_PATH . 'input' . EXT,
'think\Lang' => CORE_PATH . 'lang' . EXT,
'think\Log' => CORE_PATH . 'log' . EXT,
'think\Model' => CORE_PATH . 'model' . EXT,
'think\Response' => CORE_PATH . 'response' . EXT,
'think\Route' => CORE_PATH . 'route' . EXT,
'think\Session' => CORE_PATH . 'session' . EXT,
'think\Template' => CORE_PATH . 'template' . EXT,
'think\Url' => CORE_PATH . 'url' . EXT,
'think\View' => CORE_PATH . 'view' . EXT,
'think\db\Driver' => CORE_PATH . 'db' . DS . 'driver' . EXT,
'think\view\driver\Think' => CORE_PATH . 'view' . DS . 'driver' . DS . 'think' . EXT,
'think\template\driver\File' => CORE_PATH . 'template' . DS . 'driver' . DS . 'file' . EXT,
'think\log\driver\File' => CORE_PATH . 'log' . DS . 'driver' . DS . 'file' . EXT,
'think\cache\driver\File' => CORE_PATH . 'cache' . DS . 'driver' . DS . 'file' . EXT,
'org\Slog' => ORG_PATH . 'slog'.EXT,
],
'init' => [],
];

View File

@@ -48,24 +48,27 @@ return [
// 指定从服务器序号
'slave_no' => '',
],
'log_type' => 'Sae',
'data_cache_type' => 'Memcachesae',
'check_app_dir' => false,
'log' => [
'type' => 'Sae',
],
'cache' => [
'type' => 'Sae',
],
'file_upload_type' => 'Sae',
'compile_type' => 'Sae',
],
// 别名定义
'alias' => [
'think\Log' => CORE_PATH . 'log' . EXT,
'think\log\driver\File' => CORE_PATH . 'log/driver/file' . EXT,
'think\log\driver\Sae' => CORE_PATH . 'log/driver/sae' . EXT,
'think\Exception' => CORE_PATH . 'exception' . EXT,
'think\Model' => CORE_PATH . 'model' . EXT,
'think\Db' => CORE_PATH . 'db' . EXT,
'think\Template' => CORE_PATH . 'template' . EXT,
'think\Cache' => CORE_PATH . 'cache' . EXT,
'think\cache\driver\File' => CORE_PATH . 'cache/driver/file' . EXT,
'think\cache\driver\Memcache' => CORE_PATH . 'cache/driver/memcache' . EXT,
'think\Log' => CORE_PATH . 'log' . EXT,
'think\log\driver\Sae' => CORE_PATH . 'log' . DS . 'driver' . DS . 'sae' . EXT,
'think\Exception' => CORE_PATH . 'exception' . EXT,
'think\Model' => CORE_PATH . 'model' . EXT,
'think\Db' => CORE_PATH . 'db' . EXT,
'think\Template' => CORE_PATH . 'template' . EXT,
'think\Cache' => CORE_PATH . 'cache' . EXT,
'think\cache\driver\Sae' => CORE_PATH . 'cache' . DS . 'driver' . DS . 'sae' . EXT,
'think\template\driver\Sae' => CORE_PATH . 'template' . DS . 'driver' . DS . 'sae' . EXT,
],
];