改进Cache类 及 驱动 改进Connection类及驱动

This commit is contained in:
thinkphp
2016-05-05 16:32:47 +08:00
parent fd5447fc4f
commit 855c2c75da
31 changed files with 147 additions and 599 deletions

View File

@@ -11,15 +11,6 @@
namespace think;
/**
* Class Cache
*
* @package think
* @method static mixed get() get(string $name)
* @method static bool set() set(string $name, mixed $value, mixed $expire = null)
* @method static bool rm() rm(string $name, bool $expire = false)
* @method static bool clear() clear()
*/
class Cache
{
protected static $instance = [];
@@ -37,29 +28,93 @@ class Cache
* 连接缓存
* @access public
* @param array $options 配置数组
* @param bool|string $name 缓存连接标识 true 强制重新连接
* @return object
*/
public static function connect(array $options = [])
public static function connect(array $options = [], $name = false)
{
$md5 = md5(serialize($options));
if (!isset(self::$instance[$md5])) {
$type = !empty($options['type']) ? $options['type'] : 'File';
$type = !empty($options['type']) ? $options['type'] : 'File';
if (false === $name) {
$name = $type;
}
if (true === $name || !isset(self::$instance[$name])) {
$class = (!empty($options['namespace']) ? $options['namespace'] : '\\think\\cache\\driver\\') . ucwords($type);
unset($options['type']);
self::$instance[$md5] = new $class($options);
// 记录初始化信息
APP_DEBUG && Log::record('[ CACHE ] INIT ' . $type . ':' . var_export($options, true), 'info');
if (true === $name) {
return new $class($options);
} else {
self::$instance[$name] = new $class($options);
}
}
self::$handler = self::$instance[$md5];
self::$handler = self::$instance[$name];
return self::$handler;
}
public static function __callStatic($method, $params)
/**
* 自动初始化缓存
* @access public
* @return void
*/
public static function init()
{
if (is_null(self::$handler)) {
// 自动初始化缓存
self::connect(Config::get('cache'));
}
return call_user_func_array([self::$handler, $method], $params);
}
/**
* 读取缓存
* @access public
* @param string $name 缓存标识
* @return mixed
*/
public static function get($name)
{
self::init();
self::$readTimes++;
return self::$hander->get($name);
}
/**
* 写入缓存
* @access public
* @param string $name 缓存标识
* @param mixed $value 存储数据
* @param int|null $expire 有效时间 0为永久
* @return boolean
*/
public static function set($name, $value, $expire = null)
{
self::init();
self::$writeTimes++;
return self::$hander->set($name, $value, $expire);
}
/**
* 删除缓存
* @access public
* @param string $name 缓存标识
* @return boolean
*/
public static function rm($name)
{
self::init();
return self::$hander->rm($name);
}
/**
* 清除缓存
* @access public
* @return boolean
*/
public static function clear()
{
self::init();
return self::$hander->clear();
}
}