PSR规范调整

This commit is contained in:
thinkphp
2015-10-04 13:05:15 +08:00
parent 1cfb3704c6
commit 27e724bb3c
135 changed files with 9426 additions and 11556 deletions

View File

@@ -10,18 +10,20 @@
// +----------------------------------------------------------------------
namespace think\cache\driver;
use think\Exception;
/**
* Apc缓存驱动
* @author liu21st <liu21st@gmail.com>
*/
class Apc {
class Apc
{
protected $options = [
'expire' => 0,
'prefix' => '',
'length' => 0,
protected $options = [
'expire' => 0,
'prefix' => '',
'length' => 0,
];
/**
@@ -29,12 +31,13 @@ class Apc {
* @param array $options 缓存参数
* @access public
*/
public function __construct($options=[]) {
if(!function_exists('apc_cache_info')) {
public function __construct($options = [])
{
if (!function_exists('apc_cache_info')) {
throw new Exception('_NOT_SUPPERT_:Apc');
}
if(!empty($options)) {
$this->options = array_merge($this->options,$options);
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
}
@@ -44,9 +47,10 @@ class Apc {
* @param string $name 缓存变量名
* @return mixed
*/
public function get($name) {
return apc_fetch($this->options['prefix'].$name);
}
public function get($name)
{
return apc_fetch($this->options['prefix'] . $name);
}
/**
* 写入缓存
@@ -56,22 +60,26 @@ class Apc {
* @param integer $expire 有效时间(秒)
* @return boolen
*/
public function set($name, $value, $expire = null) {
if(is_null($expire)) {
$expire = $this->options['expire'];
public function set($name, $value, $expire = null)
{
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$name = $this->options['prefix'].$name;
if($result = apc_store($name, $value, $expire)) {
if($this->options['length']>0) {
$name = $this->options['prefix'] . $name;
if ($result = apc_store($name, $value, $expire)) {
if ($this->options['length'] > 0) {
// 记录缓存队列
$queue = apc_fetch('__info__');
if(!$queue) {
$queue = [];
$queue = apc_fetch('__info__');
if (!$queue) {
$queue = [];
}
if(false===array_search($name, $queue)) array_push($queue,$name);
if(count($queue) > $this->options['length']) {
if (false === array_search($name, $queue)) {
array_push($queue, $name);
}
if (count($queue) > $this->options['length']) {
// 出列
$key = array_shift($queue);
$key = array_shift($queue);
// 删除缓存
apc_delete($key);
}
@@ -79,7 +87,7 @@ class Apc {
}
}
return $result;
}
}
/**
* 删除缓存
@@ -87,16 +95,18 @@ class Apc {
* @param string $name 缓存变量名
* @return boolen
*/
public function rm($name) {
return apc_delete($this->options['prefix'].$name);
}
public function rm($name)
{
return apc_delete($this->options['prefix'] . $name);
}
/**
* 清除缓存
* @access public
* @return boolen
*/
public function clear() {
public function clear()
{
return apc_clear_cache();
}
}

View File

@@ -22,15 +22,16 @@ namespace think\cache\driver;
* );
* @author liu21st <liu21st@gmail.com>
*/
class Db {
class Db
{
protected $handler = null;
protected $options = [
'db' => '',
'table' => '',
'prefix' => '',
'expire' => 0,
'length' => 0,
protected $handler = null;
protected $options = [
'db' => '',
'table' => '',
'prefix' => '',
'expire' => 0,
'length' => 0,
];
/**
@@ -38,11 +39,12 @@ class Db {
* @param array $options 缓存参数
* @access public
*/
public function __construct($options=[]) {
if(!empty($options)) {
$this->options = array_merge($this->options,$options);
public function __construct($options = [])
{
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
$this->handler = \Think\Db::instance();
$this->handler = \Think\Db::instance();
}
/**
@@ -51,20 +53,20 @@ class Db {
* @param string $name 缓存变量名
* @return mixed
*/
public function get($name) {
$name = $this->options['prefix'].addslashes($name);
$result = $this->handler->query('SELECT `data`,`datacrc` FROM `'.$this->options['table'].'` WHERE `cachekey`=\''.$name.'\' AND (`expire` =0 OR `expire`>'.time().') LIMIT 0,1');
if(false !== $result ) {
$result = $result[0];
$content = $result['data'];
if(function_exists('gzcompress')) {
public function get($name)
{
$name = $this->options['prefix'] . addslashes($name);
$result = $this->handler->query('SELECT `data`,`datacrc` FROM `' . $this->options['table'] . '` WHERE `cachekey`=\'' . $name . '\' AND (`expire` =0 OR `expire`>' . time() . ') LIMIT 0,1');
if (false !== $result) {
$result = $result[0];
$content = $result['data'];
if (function_exists('gzcompress')) {
//启用数据压缩
$content = gzuncompress($content);
$content = gzuncompress($content);
}
$content = unserialize($content);
$content = unserialize($content);
return $content;
}
else {
} else {
return false;
}
}
@@ -77,48 +79,52 @@ class Db {
* @param integer $expire 有效时间(秒)
* @return boolen
*/
public function set($name, $value,$expire=null) {
$data = serialize($value);
$name = $this->options['prefix'].addslashes($name);
if(function_exists('gzcompress')) {
public function set($name, $value, $expire = null)
{
$data = serialize($value);
$name = $this->options['prefix'] . addslashes($name);
if (function_exists('gzcompress')) {
//数据压缩
$data = gzcompress($data,3);
$data = gzcompress($data, 3);
}
if(is_null($expire)) {
$expire = $this->options['expire'];
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$expire = ($expire==0)?0: (time()+$expire) ;//缓存有效期为0表示永久缓存
$result = $this->handler->query('select `cachekey` from `'.$this->options['table'].'` where `cachekey`=\''.$name.'\' limit 0,1');
if(!empty($result) ) {
//更新记录
$result = $this->handler->execute('UPDATE '.$this->options['table'].' SET data=\''.$data.'\' ,expire='.$expire.' WHERE `cachekey`=\''.$name.'\'');
}else {
//新增记录
$result = $this->handler->execute('INSERT INTO '.$this->options['table'].' (`cachekey`,`data`,`expire`) VALUES (\''.$name.'\',\''.$data.'\','.$expire.')');
$expire = (0 == $expire) ? 0 : (time() + $expire); //缓存有效期为0表示永久缓存
$result = $this->handler->query('select `cachekey` from `' . $this->options['table'] . '` where `cachekey`=\'' . $name . '\' limit 0,1');
if (!empty($result)) {
//更新记录
$result = $this->handler->execute('UPDATE ' . $this->options['table'] . ' SET data=\'' . $data . '\' ,expire=' . $expire . ' WHERE `cachekey`=\'' . $name . '\'');
} else {
//新增记录
$result = $this->handler->execute('INSERT INTO ' . $this->options['table'] . ' (`cachekey`,`data`,`expire`) VALUES (\'' . $name . '\',\'' . $data . '\',' . $expire . ')');
}
if($result) {
if($this->options['length']>0) {
if ($result) {
if ($this->options['length'] > 0) {
// 记录缓存队列
$result = $this->handler->query('SELECT `data`,`datacrc` FROM `'.$this->options['table'].'` WHERE `cachekey`=\'__info__\' AND `expire` =0 LIMIT 0,1');
$queue = xcache_get('__info__');
if(!$result) {
$this->handler->execute('INSERT INTO '.$this->options['table'].' (`cachekey`,`data`,`expire`) VALUES (\'__info__\',\'\',0)');
$queue = [];
}else{
$queue = unserialize($result[0]['data']);
$result = $this->handler->query('SELECT `data`,`datacrc` FROM `' . $this->options['table'] . '` WHERE `cachekey`=\'__info__\' AND `expire` =0 LIMIT 0,1');
$queue = xcache_get('__info__');
if (!$result) {
$this->handler->execute('INSERT INTO ' . $this->options['table'] . ' (`cachekey`,`data`,`expire`) VALUES (\'__info__\',\'\',0)');
$queue = [];
} else {
$queue = unserialize($result[0]['data']);
}
if(false===array_search($name, $queue)) array_push($queue,$name);
if(count($queue) > $this->options['length']) {
if (false === array_search($name, $queue)) {
array_push($queue, $name);
}
if (count($queue) > $this->options['length']) {
// 出列
$key = array_shift($queue);
$key = array_shift($queue);
// 删除缓存
$this->handler->execute('DELETE FROM `'.$this->options['table'].'` WHERE `cachekey`=\''.$key.'\'');
$this->handler->execute('DELETE FROM `' . $this->options['table'] . '` WHERE `cachekey`=\'' . $key . '\'');
}
$this->handler->execute('UPDATE '.$this->options['table'].' SET data=\''.serialize($queue).'\' ,expire=0 WHERE `cachekey`=\'__info__\'');
$this->handler->execute('UPDATE ' . $this->options['table'] . ' SET data=\'' . serialize($queue) . '\' ,expire=0 WHERE `cachekey`=\'__info__\'');
xcache_set('__info__', $queue);
}
return true;
}else {
} else {
return false;
}
}
@@ -129,9 +135,10 @@ class Db {
* @param string $name 缓存变量名
* @return boolen
*/
public function rm($name) {
$name = $this->options['prefix'].addslashes($name);
return $this->handler->execute('DELETE FROM `'.$this->options['table'].'` WHERE `cachekey`=\''.$name.'\'');
public function rm($name)
{
$name = $this->options['prefix'] . addslashes($name);
return $this->handler->execute('DELETE FROM `' . $this->options['table'] . '` WHERE `cachekey`=\'' . $name . '\'');
}
/**
@@ -139,8 +146,9 @@ class Db {
* @access public
* @return boolen
*/
public function clear() {
return $this->handler->execute('TRUNCATE TABLE `'.$this->options['table'].'`');
public function clear()
{
return $this->handler->execute('TRUNCATE TABLE `' . $this->options['table'] . '`');
}
}

View File

@@ -15,12 +15,13 @@ namespace think\cache\driver;
* Eaccelerator缓存驱动
* @author liu21st <liu21st@gmail.com>
*/
class Eaccelerator {
class Eaccelerator
{
protected $options = [
'prefix' => '',
'expire' => 0,
'length' => 0,
protected $options = [
'prefix' => '',
'expire' => 0,
'length' => 0,
];
/**
@@ -28,9 +29,10 @@ class Eaccelerator {
* @param array $options 缓存参数
* @access public
*/
public function __construct($options=[]) {
if(!empty($options)) {
$this->options = array_merge($this->options,$options);
public function __construct($options = [])
{
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
}
@@ -40,9 +42,10 @@ class Eaccelerator {
* @param string $name 缓存变量名
* @return mixed
*/
public function get($name) {
return eaccelerator_get($this->options['prefix'].$name);
}
public function get($name)
{
return eaccelerator_get($this->options['prefix'] . $name);
}
/**
* 写入缓存
@@ -52,23 +55,27 @@ class Eaccelerator {
* @param integer $expire 有效时间(秒)
* @return boolen
*/
public function set($name, $value, $expire = null) {
if(is_null($expire)) {
$expire = $this->options['expire'];
public function set($name, $value, $expire = null)
{
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$name = $this->options['prefix'].$name;
$name = $this->options['prefix'] . $name;
eaccelerator_lock($name);
if(eaccelerator_put($name, $value, $expire)) {
if($this->options['length']>0) {
if (eaccelerator_put($name, $value, $expire)) {
if ($this->options['length'] > 0) {
// 记录缓存队列
$queue = eaccelerator_get('__info__');
if(!$queue) {
$queue = [];
$queue = eaccelerator_get('__info__');
if (!$queue) {
$queue = [];
}
if(false===array_search($name, $queue)) array_push($queue,$name);
if(count($queue) > $this->options['length']) {
if (false === array_search($name, $queue)) {
array_push($queue, $name);
}
if (count($queue) > $this->options['length']) {
// 出列
$key = array_shift($queue);
$key = array_shift($queue);
// 删除缓存
eaccelerator_rm($key);
}
@@ -77,8 +84,7 @@ class Eaccelerator {
return true;
}
return false;
}
}
/**
* 删除缓存
@@ -86,16 +92,18 @@ class Eaccelerator {
* @param string $name 缓存变量名
* @return boolen
*/
public function rm($name) {
return eaccelerator_rm($this->options['prefix'].$name);
}
public function rm($name)
{
return eaccelerator_rm($this->options['prefix'] . $name);
}
/**
* 清除缓存
* @access public
* @return boolen
*/
public function clear() {
return ;
public function clear()
{
return;
}
}

View File

@@ -15,27 +15,32 @@ namespace think\cache\driver;
* 文件类型缓存类
* @author liu21st <liu21st@gmail.com>
*/
class File {
class File
{
protected $options = [
'expire' => 0,
'cache_subdir' => false,
'path_level' => 1,
'prefix' => '',
'length' => 0,
'path' => '',
'data_compress' => false,
protected $options = [
'expire' => 0,
'cache_subdir' => false,
'path_level' => 1,
'prefix' => '',
'length' => 0,
'path' => '',
'data_compress' => false,
];
/**
* 架构函数
* @access public
*/
public function __construct($options=[]) {
if(!empty($options)) {
$this->options = array_merge($this->options,$options);
public function __construct($options = [])
{
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
if(substr($this->options['path'], -1) != '/') $this->options['path'] .= '/';
if (substr($this->options['path'], -1) != '/') {
$this->options['path'] .= '/';
}
$this->init();
}
@@ -44,11 +49,14 @@ class File {
* @access private
* @return boolen
*/
private function init() {
private function init()
{
// 创建项目缓存目录
if (!is_dir($this->options['path'])) {
if (! mkdir($this->options['path'],0755))
if (!mkdir($this->options['path'], 0755)) {
return false;
}
}
}
@@ -58,23 +66,24 @@ class File {
* @param string $name 缓存变量名
* @return string
*/
private function filename($name) {
$name = md5($name);
if($this->options['cache_subdir']) {
private function filename($name)
{
$name = md5($name);
if ($this->options['cache_subdir']) {
// 使用子目录
$dir = '';
$len = $this->options['path_level'];
for($i=0;$i<$len;$i++) {
$dir .= $name{$i}.'/';
$dir = '';
$len = $this->options['path_level'];
for ($i = 0; $i < $len; $i++) {
$dir .= $name{$i} . '/';
}
if(!is_dir($this->options['path'].$dir)) {
mkdir($this->options['path'].$dir,0755,true);
if (!is_dir($this->options['path'] . $dir)) {
mkdir($this->options['path'] . $dir, 0755, true);
}
$filename = $dir.$this->options['prefix'].$name.'.php';
}else{
$filename = $this->options['prefix'].$name.'.php';
$filename = $dir . $this->options['prefix'] . $name . '.php';
} else {
$filename = $this->options['prefix'] . $name . '.php';
}
return $this->options['path'].$filename;
return $this->options['path'] . $filename;
}
/**
@@ -83,28 +92,28 @@ class File {
* @param string $name 缓存变量名
* @return mixed
*/
public function get($name) {
$filename = $this->filename($name);
public function get($name)
{
$filename = $this->filename($name);
if (!is_file($filename)) {
return false;
return false;
}
$content = file_get_contents($filename);
if( false !== $content) {
$expire = (int)substr($content,8, 12);
if($expire != 0 && time() > filemtime($filename) + $expire) {
$content = file_get_contents($filename);
if (false !== $content) {
$expire = (int) substr($content, 8, 12);
if (0 != $expire && time() > filemtime($filename) + $expire) {
//缓存过期删除缓存文件
unlink($filename);
return false;
}
$content = substr($content,20, -3);
if($this->options['data_compress'] && function_exists('gzcompress')) {
$content = substr($content, 20, -3);
if ($this->options['data_compress'] && function_exists('gzcompress')) {
//启用数据压缩
$content = gzuncompress($content);
$content = gzuncompress($content);
}
$content = unserialize($content);
$content = unserialize($content);
return $content;
}
else {
} else {
return false;
}
}
@@ -117,30 +126,34 @@ class File {
* @param int $expire 有效时间 0为永久
* @return boolen
*/
public function set($name,$value,$expire=null) {
if(is_null($expire)) {
$expire = $this->options['expire'];
public function set($name, $value, $expire = null)
{
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$filename = $this->filename($name);
$data = serialize($value);
if($this->options['data_compress'] && function_exists('gzcompress')) {
$filename = $this->filename($name);
$data = serialize($value);
if ($this->options['data_compress'] && function_exists('gzcompress')) {
//数据压缩
$data = gzcompress($data,3);
$data = gzcompress($data, 3);
}
$data = "<?php\n//".sprintf('%012d',$expire).$data."\n?>";
$result = file_put_contents($filename,$data);
if($result) {
if($this->options['length']>0) {
$data = "<?php\n//" . sprintf('%012d', $expire) . $data . "\n?>";
$result = file_put_contents($filename, $data);
if ($result) {
if ($this->options['length'] > 0) {
// 记录缓存队列
$queue_file = dirname($filename).'/__info__.php';
$queue = unserialize(file_get_contents($queue_file));
if(!$queue) {
$queue = [];
$queue_file = dirname($filename) . '/__info__.php';
$queue = unserialize(file_get_contents($queue_file));
if (!$queue) {
$queue = [];
}
if(false===array_search($name, $queue)) array_push($queue,$name);
if(count($queue) > $this->options['length']) {
if (false === array_search($name, $queue)) {
array_push($queue, $name);
}
if (count($queue) > $this->options['length']) {
// 出列
$key = array_shift($queue);
$key = array_shift($queue);
// 删除缓存
unlink($this->filename($key));
}
@@ -148,7 +161,7 @@ class File {
}
clearstatcache();
return true;
}else {
} else {
return false;
}
}
@@ -159,7 +172,8 @@ class File {
* @param string $name 缓存变量名
* @return boolen
*/
public function rm($name) {
public function rm($name)
{
return unlink($this->filename($name));
}
@@ -169,15 +183,18 @@ class File {
* @param string $name 缓存变量名
* @return boolen
*/
public function clear() {
$path = $this->options['temp'];
if ( $dir = opendir( $path ) ) {
while ( $file = readdir( $dir ) ) {
$check = is_dir( $file );
if ( !$check )
unlink( $path . $file );
public function clear()
{
$path = $this->options['temp'];
if ($dir = opendir($path)) {
while ($file = readdir($dir)) {
$check = is_dir($file);
if (!$check) {
unlink($path . $file);
}
}
closedir( $dir );
closedir($dir);
return true;
}
}

View File

@@ -10,21 +10,23 @@
// +----------------------------------------------------------------------
namespace think\cache\driver;
use think\Exception;
/**
* Memcache缓存驱动
* @author liu21st <liu21st@gmail.com>
*/
class Memcache {
protected $handler = null;
protected $options = [
'host' => '127.0.0.1',
'port' => 11211,
'expire' => 0,
'timeout' => false,
'persistent' => false,
'length' => 0,
class Memcache
{
protected $handler = null;
protected $options = [
'host' => '127.0.0.1',
'port' => 11211,
'expire' => 0,
'timeout' => false,
'persistent' => false,
'length' => 0,
];
/**
@@ -32,23 +34,24 @@ class Memcache {
* @param array $options 缓存参数
* @access public
*/
public function __construct($options=[]) {
if ( !extension_loaded('memcache') ) {
public function __construct($options = [])
{
if (!extension_loaded('memcache')) {
throw new Exception('_NOT_SUPPERT_:memcache');
}
if(!empty($options)) {
$this->options = array_merge($this->options,$options);
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
$this->handler = new \Memcache;
$this->handler = new \Memcache;
// 支持集群
$hosts = explode(',', $this->options['host']);
$ports = explode(',', $this->options['port']);
$hosts = explode(',', $this->options['host']);
$ports = explode(',', $this->options['port']);
foreach((array) $hosts as $i=>$host){
$port = isset($ports[$i]) ? $ports[$i] : $ports[0];
$options['timeout'] === false ?
$this->handler->addServer($host, $port, $this->options['persistent'], 1) :
$this->handler->addServer($host, $port, $this->options['persistent'], 1, $this->options['timeout']);
foreach ((array) $hosts as $i => $host) {
$port = isset($ports[$i]) ? $ports[$i] : $ports[0];
false === $options['timeout'] ?
$this->handler->addServer($host, $port, $this->options['persistent'], 1) :
$this->handler->addServer($host, $port, $this->options['persistent'], 1, $this->options['timeout']);
}
}
@@ -58,8 +61,9 @@ class Memcache {
* @param string $name 缓存变量名
* @return mixed
*/
public function get($name) {
return $this->handler->get($this->options['prefix'].$name);
public function get($name)
{
return $this->handler->get($this->options['prefix'] . $name);
}
/**
@@ -70,22 +74,26 @@ class Memcache {
* @param integer $expire 有效时间(秒)
* @return boolen
*/
public function set($name, $value, $expire = null) {
if(is_null($expire)) {
$expire = $this->options['expire'];
public function set($name, $value, $expire = null)
{
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$name = $this->options['prefix'].$name;
if($this->handler->set($name, $value, 0, $expire)) {
if($this->options['length']>0) {
$name = $this->options['prefix'] . $name;
if ($this->handler->set($name, $value, 0, $expire)) {
if ($this->options['length'] > 0) {
// 记录缓存队列
$queue = $this->handler->get('__info__');
if(!$queue) {
$queue = [];
$queue = $this->handler->get('__info__');
if (!$queue) {
$queue = [];
}
if(false===array_search($name, $queue)) array_push($queue,$name);
if(count($queue) > $this->options['length']) {
if (false === array_search($name, $queue)) {
array_push($queue, $name);
}
if (count($queue) > $this->options['length']) {
// 出列
$key = array_shift($queue);
$key = array_shift($queue);
// 删除缓存
$this->handler->delete($key);
}
@@ -102,11 +110,12 @@ class Memcache {
* @param string $name 缓存变量名
* @return boolen
*/
public function rm($name, $ttl = false) {
$name = $this->options['prefix'].$name;
return $ttl === false ?
$this->handler->delete($name) :
$this->handler->delete($name, $ttl);
public function rm($name, $ttl = false)
{
$name = $this->options['prefix'] . $name;
return false === $ttl ?
$this->handler->delete($name) :
$this->handler->delete($name, $ttl);
}
/**
@@ -114,7 +123,8 @@ class Memcache {
* @access public
* @return boolen
*/
public function clear() {
public function clear()
{
return $this->handler->flush();
}
}

View File

@@ -10,41 +10,44 @@
// +----------------------------------------------------------------------
namespace think\cache\driver;
use think\Exception;
/**
* Redis缓存驱动
* Redis缓存驱动
* 要求安装phpredis扩展https://github.com/nicolasff/phpredis
* @author 尘缘 <130775@qq.com>
*/
class Redis {
protected $handler = null;
protected $options = [
'host' => '127.0.0.1',
'port' => 6379,
'timeout' => false,
'expire' => 0,
'persistent' => false,
'length' => 0,
class Redis
{
protected $handler = null;
protected $options = [
'host' => '127.0.0.1',
'port' => 6379,
'timeout' => false,
'expire' => 0,
'persistent' => false,
'length' => 0,
];
/**
* 架构函数
/**
* 架构函数
* @param array $options 缓存参数
* @access public
*/
public function __construct($options=[]) {
if ( !extension_loaded('redis') ) {
public function __construct($options = [])
{
if (!extension_loaded('redis')) {
throw new Exception('_NOT_SUPPERT_:redis');
}
if(!empty($options)) {
$this->options = array_merge($this->options,$options);
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
$func = $options['persistent'] ? 'pconnect' : 'connect';
$this->handler = new \Redis;
$options['timeout'] === false ?
$this->handler->$func($options['host'], $options['port']) :
$this->handler->$func($options['host'], $options['port'], $options['timeout']);
$func = $options['persistent'] ? 'pconnect' : 'connect';
$this->handler = new \Redis;
false === $options['timeout'] ?
$this->handler->$func($options['host'], $options['port']) :
$this->handler->$func($options['host'], $options['port'], $options['timeout']);
}
/**
@@ -53,8 +56,9 @@ class Redis {
* @param string $name 缓存变量名
* @return mixed
*/
public function get($name) {
return $this->handler->get($this->options['prefix'].$name);
public function get($name)
{
return $this->handler->get($this->options['prefix'] . $name);
}
/**
@@ -65,27 +69,31 @@ class Redis {
* @param integer $expire 有效时间(秒)
* @return boolen
*/
public function set($name, $value, $expire = null) {
if(is_null($expire)) {
$expire = $this->options['expire'];
public function set($name, $value, $expire = null)
{
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$name = $this->options['prefix'].$name;
if(is_int($expire)) {
$name = $this->options['prefix'] . $name;
if (is_int($expire)) {
$result = $this->handler->setex($name, $expire, $value);
}else{
} else {
$result = $this->handler->set($name, $value);
}
if($result && $this->options['length']>0) {
if($this->options['length']>0) {
if ($result && $this->options['length'] > 0) {
if ($this->options['length'] > 0) {
// 记录缓存队列
$queue = $this->handler->get('__info__');
if(!$queue) {
$queue = [];
$queue = $this->handler->get('__info__');
if (!$queue) {
$queue = [];
}
if(false===array_search($name, $queue)) array_push($queue,$name);
if(count($queue) > $this->options['length']) {
if (false === array_search($name, $queue)) {
array_push($queue, $name);
}
if (count($queue) > $this->options['length']) {
// 出列
$key = array_shift($queue);
$key = array_shift($queue);
// 删除缓存
$this->handler->delete($key);
}
@@ -101,8 +109,9 @@ class Redis {
* @param string $name 缓存变量名
* @return boolen
*/
public function rm($name) {
return $this->handler->delete($this->options['prefix'].$name);
public function rm($name)
{
return $this->handler->delete($this->options['prefix'] . $name);
}
/**
@@ -110,7 +119,8 @@ class Redis {
* @access public
* @return boolen
*/
public function clear() {
public function clear()
{
return $this->handler->flushDB();
}

File diff suppressed because it is too large Load Diff

View File

@@ -15,22 +15,27 @@ namespace think\cache\driver;
* 文件类型缓存类
* @author liu21st <liu21st@gmail.com>
*/
class Simple {
class Simple
{
protected $options = [
'prefix' => '',
'path' => '',
protected $options = [
'prefix' => '',
'path' => '',
];
/**
* 架构函数
* @access public
*/
public function __construct($options=[]) {
if(!empty($options)) {
$this->options = array_merge($this->options,$options);
public function __construct($options = [])
{
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
if(substr($this->options['path'], -1) != '/') $this->options['path'] .= '/';
if (substr($this->options['path'], -1) != '/') {
$this->options['path'] .= '/';
}
}
/**
@@ -39,8 +44,9 @@ class Simple {
* @param string $name 缓存变量名
* @return string
*/
private function filename($name) {
return $this->options['path'].$this->options['prefix'].md5($name).'.php';
private function filename($name)
{
return $this->options['path'] . $this->options['prefix'] . md5($name) . '.php';
}
/**
@@ -49,11 +55,12 @@ class Simple {
* @param string $name 缓存变量名
* @return mixed
*/
public function get($name) {
$filename = $this->filename($name);
public function get($name)
{
$filename = $this->filename($name);
if (is_file($filename)) {
return include $filename;
}else{
} else {
return false;
}
}
@@ -66,13 +73,14 @@ class Simple {
* @param int $expire 有效时间 0为永久
* @return boolen
*/
public function set($name,$value,$expire=null) {
$filename = $this->filename($name);
public function set($name, $value, $expire = null)
{
$filename = $this->filename($name);
// 缓存数据
$dir = dirname($filename);
$dir = dirname($filename);
// 目录不存在则创建
//if (!is_dir($dir))
// mkdir($dir,0755,true);
// mkdir($dir,0755,true);
return file_put_contents($filename, ("<?php\treturn " . var_export($value, true) . ";?>"));
}
@@ -82,7 +90,8 @@ class Simple {
* @param string $name 缓存变量名
* @return boolen
*/
public function rm($name) {
public function rm($name)
{
return unlink($this->filename($name));
}
@@ -92,8 +101,9 @@ class Simple {
* @param string $name 缓存变量名
* @return boolen
*/
public function clear() {
$filename = $this->filename('*');
public function clear()
{
$filename = $this->filename('*');
array_map("unlink", glob($filename));
}
}

View File

@@ -10,21 +10,23 @@
// +----------------------------------------------------------------------
namespace think\cache\driver;
use think\Exception;
/**
* Sqlite缓存驱动
* @author liu21st <liu21st@gmail.com>
*/
class Sqlite {
class Sqlite
{
protected $options = [
'db' => ':memory:',
'table' => 'sharedmemory',
'prefix' => '',
'expire' => 0,
'length' => 0,
'persistent' => false,
protected $options = [
'db' => ':memory:',
'table' => 'sharedmemory',
'prefix' => '',
'expire' => 0,
'length' => 0,
'persistent' => false,
];
/**
@@ -32,15 +34,16 @@ class Sqlite {
* @param array $options 缓存参数
* @access public
*/
public function __construct($options=[]) {
if ( !extension_loaded('sqlite') ) {
public function __construct($options = [])
{
if (!extension_loaded('sqlite')) {
throw new Exception('_NOT_SUPPERT_:sqlite');
}
if(!empty($options)) {
$this->options = array_merge($this->options,$options);
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
$func = $this->options['persistent'] ? 'sqlite_popen' : 'sqlite_open';
$this->handler = $func($this->options['db']);
$func = $this->options['persistent'] ? 'sqlite_popen' : 'sqlite_open';
$this->handler = $func($this->options['db']);
}
/**
@@ -49,15 +52,16 @@ class Sqlite {
* @param string $name 缓存变量名
* @return mixed
*/
public function get($name) {
$name = $this->options['prefix'].sqlite_escape_string($name);
$sql = 'SELECT value FROM '.$this->options['table'].' WHERE var=\''.$name.'\' AND (expire=0 OR expire >'.time().') LIMIT 1';
public function get($name)
{
$name = $this->options['prefix'] . sqlite_escape_string($name);
$sql = 'SELECT value FROM ' . $this->options['table'] . ' WHERE var=\'' . $name . '\' AND (expire=0 OR expire >' . time() . ') LIMIT 1';
$result = sqlite_query($this->handler, $sql);
if (sqlite_num_rows($result)) {
$content = sqlite_fetch_single($result);
if(function_exists('gzcompress')) {
$content = sqlite_fetch_single($result);
if (function_exists('gzcompress')) {
//启用数据压缩
$content = gzuncompress($content);
$content = gzuncompress($content);
}
return unserialize($content);
}
@@ -72,20 +76,21 @@ class Sqlite {
* @param integer $expire 有效时间(秒)
* @return boolen
*/
public function set($name, $value,$expire=null) {
$name = $this->options['prefix'].sqlite_escape_string($name);
public function set($name, $value, $expire = null)
{
$name = $this->options['prefix'] . sqlite_escape_string($name);
$value = sqlite_escape_string(serialize($value));
if(is_null($expire)) {
$expire = $this->options['expire'];
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$expire = ($expire==0)?0: (time()+$expire) ;//缓存有效期为0表示永久缓存
if(function_exists('gzcompress')) {
$expire = (0 == $expire) ? 0 : (time() + $expire); //缓存有效期为0表示永久缓存
if (function_exists('gzcompress')) {
//数据压缩
$value = gzcompress($value,3);
$value = gzcompress($value, 3);
}
$sql = 'REPLACE INTO '.$this->options['table'].' (var, value,expire) VALUES (\''.$name.'\', \''.$value.'\', \''.$expire.'\')';
if(sqlite_query($this->handler, $sql)){
if($this->options['length']>0) {
$sql = 'REPLACE INTO ' . $this->options['table'] . ' (var, value,expire) VALUES (\'' . $name . '\', \'' . $value . '\', \'' . $expire . '\')';
if (sqlite_query($this->handler, $sql)) {
if ($this->options['length'] > 0) {
// 记录缓存队列
$this->queue($name);
}
@@ -100,9 +105,10 @@ class Sqlite {
* @param string $name 缓存变量名
* @return boolen
*/
public function rm($name) {
$name = $this->options['prefix'].sqlite_escape_string($name);
$sql = 'DELETE FROM '.$this->options['table'].' WHERE var=\''.$name.'\'';
public function rm($name)
{
$name = $this->options['prefix'] . sqlite_escape_string($name);
$sql = 'DELETE FROM ' . $this->options['table'] . ' WHERE var=\'' . $name . '\'';
sqlite_query($this->handler, $sql);
return true;
}
@@ -112,9 +118,10 @@ class Sqlite {
* @access public
* @return boolen
*/
public function clear() {
$sql = 'DELETE FROM '.$this->options['table'];
public function clear()
{
$sql = 'DELETE FROM ' . $this->options['table'];
sqlite_query($this->handler, $sql);
return ;
return;
}
}

View File

@@ -10,18 +10,20 @@
// +----------------------------------------------------------------------
namespace think\cache\driver;
use think\Exception;
/**
* Wincache缓存驱动
* @author liu21st <liu21st@gmail.com>
*/
class Wincache {
class Wincache
{
protected $options = [
'prefix' => '',
'expire' => 0,
'length' => 0,
protected $options = [
'prefix' => '',
'expire' => 0,
'length' => 0,
];
/**
@@ -29,12 +31,13 @@ class Wincache {
* @param array $options 缓存参数
* @access public
*/
public function __construct($options=[]) {
if ( !function_exists('wincache_ucache_info') ) {
public function __construct($options = [])
{
if (!function_exists('wincache_ucache_info')) {
throw new Exception('_NOT_SUPPERT_:WinCache');
}
if(!empty($options)) {
$this->options = array_merge($this->options,$options);
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
}
@@ -44,9 +47,10 @@ class Wincache {
* @param string $name 缓存变量名
* @return mixed
*/
public function get($name) {
$name = $this->options['prefix'].$name;
return wincache_ucache_exists($name)? wincache_ucache_get($name) : false;
public function get($name)
{
$name = $this->options['prefix'] . $name;
return wincache_ucache_exists($name) ? wincache_ucache_get($name) : false;
}
/**
@@ -57,22 +61,26 @@ class Wincache {
* @param integer $expire 有效时间(秒)
* @return boolen
*/
public function set($name, $value,$expire=null) {
if(is_null($expire)) {
$expire = $this->options['expire'];
public function set($name, $value, $expire = null)
{
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$name = $this->options['prefix'].$name;
if(wincache_ucache_set($name, $value, $expire)) {
if($this->options['length']>0) {
$name = $this->options['prefix'] . $name;
if (wincache_ucache_set($name, $value, $expire)) {
if ($this->options['length'] > 0) {
// 记录缓存队列
$queue = wincache_ucache_get('__info__');
if(!$queue) {
$queue = [];
$queue = wincache_ucache_get('__info__');
if (!$queue) {
$queue = [];
}
if(false===array_search($name, $queue)) array_push($queue,$name);
if(count($queue) > $this->options['length']) {
if (false === array_search($name, $queue)) {
array_push($queue, $name);
}
if (count($queue) > $this->options['length']) {
// 出列
$key = array_shift($queue);
$key = array_shift($queue);
// 删除缓存
wincache_ucache_delete($key);
}
@@ -89,8 +97,9 @@ class Wincache {
* @param string $name 缓存变量名
* @return boolen
*/
public function rm($name) {
return wincache_ucache_delete($this->options['prefix'].$name);
public function rm($name)
{
return wincache_ucache_delete($this->options['prefix'] . $name);
}
/**
@@ -98,7 +107,8 @@ class Wincache {
* @access public
* @return boolen
*/
public function clear() {
return ;
public function clear()
{
return;
}
}

View File

@@ -10,18 +10,20 @@
// +----------------------------------------------------------------------
namespace think\cache\driver;
use think\Exception;
/**
* Xcache缓存驱动
* @author liu21st <liu21st@gmail.com>
*/
class Xcache {
class Xcache
{
protected $options = [
'prefix' => '',
'expire' => 0,
'length' => 0,
protected $options = [
'prefix' => '',
'expire' => 0,
'length' => 0,
];
/**
@@ -29,12 +31,13 @@ class Xcache {
* @param array $options 缓存参数
* @access public
*/
public function __construct($options=[]) {
if ( !function_exists('xcache_info') ) {
public function __construct($options = [])
{
if (!function_exists('xcache_info')) {
throw new Exception('_NOT_SUPPERT_:Xcache');
}
if(!empty($options)) {
$this->options = array_merge($this->options,$options);
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
}
@@ -44,8 +47,9 @@ class Xcache {
* @param string $name 缓存变量名
* @return mixed
*/
public function get($name) {
$name = $this->options['prefix'].$name;
public function get($name)
{
$name = $this->options['prefix'] . $name;
if (xcache_isset($name)) {
return xcache_get($name);
}
@@ -60,22 +64,26 @@ class Xcache {
* @param integer $expire 有效时间(秒)
* @return boolen
*/
public function set($name, $value,$expire=null) {
if(is_null($expire)) {
$expire = $this->options['expire'] ;
public function set($name, $value, $expire = null)
{
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$name = $this->options['prefix'].$name;
if(xcache_set($name, $value, $expire)) {
if($this->options['length']>0) {
$name = $this->options['prefix'] . $name;
if (xcache_set($name, $value, $expire)) {
if ($this->options['length'] > 0) {
// 记录缓存队列
$queue = xcache_get('__info__');
if(!$queue) {
$queue = [];
$queue = xcache_get('__info__');
if (!$queue) {
$queue = [];
}
if(false===array_search($name, $queue)) array_push($queue,$name);
if(count($queue) > $this->options['length']) {
if (false === array_search($name, $queue)) {
array_push($queue, $name);
}
if (count($queue) > $this->options['length']) {
// 出列
$key = array_shift($queue);
$key = array_shift($queue);
// 删除缓存
xcache_unset($key);
}
@@ -92,8 +100,9 @@ class Xcache {
* @param string $name 缓存变量名
* @return boolen
*/
public function rm($name) {
return xcache_unset($this->options['prefix'].$name);
public function rm($name)
{
return xcache_unset($this->options['prefix'] . $name);
}
/**
@@ -101,7 +110,8 @@ class Xcache {
* @access public
* @return boolen
*/
public function clear() {
return ;
public function clear()
{
return;
}
}