Skip to content

Commit

Permalink
改进Connection类
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Dec 8, 2017
1 parent cba6f2a commit cdae8ad
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 52 deletions.
49 changes: 24 additions & 25 deletions src/db/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ abstract class Connection
protected $linkID;
protected $linkRead;
protected $linkWrite;

// 当前缓存对象
protected $cache;
// 查询结果类型
protected $fetchType = PDO::FETCH_ASSOC;
// 字段属性大小写
Expand Down Expand Up @@ -141,6 +142,7 @@ protected function __construct(array $config = [])
$class = $this->getBuilderClass();

$this->builder = new $class($this);
$this->cache = Db::getCacheHandler();

// 执行初始化操作
$this->initialize();
Expand Down Expand Up @@ -759,18 +761,17 @@ public function execute($sql, $bind = [])
public function find(Query $query)
{
// 分析查询表达式
$options = $query->getOptions();
$pk = $query->getPk($options);
$cacheHandler = Db::getCacheHandler();
$options = $query->getOptions();
$pk = $query->getPk($options);

if ($cacheHandler && !empty($options['cache']) && true === $options['cache']['key'] && is_string($pk) && isset($options['where']['AND'][$pk])) {
if ($this->cache && !empty($options['cache']) && true === $options['cache']['key'] && is_string($pk) && isset($options['where']['AND'][$pk])) {
$key = $this->getCacheKey($options['where']['AND'][$pk], $options);
}

$data = $options['data'];
$result = false;

if ($cacheHandler && empty($options['fetch_sql']) && !empty($options['cache'])) {
if ($this->cache && empty($options['fetch_sql']) && !empty($options['cache'])) {
// 判断查询缓存
$cache = $options['cache'];

Expand All @@ -780,7 +781,7 @@ public function find(Query $query)
$key = $this->getCacheKey($data, $options, $query->getBind(false));
}

$result = $cacheHandler->get($key);
$result = $this->cache->get($key);
}

if (false === $result) {
Expand Down Expand Up @@ -866,14 +867,14 @@ public function cursor(Query $query)
public function select(Query $query)
{
// 分析查询表达式
$options = $query->getOptions();
$resultSet = false;
$cacheHandler = Db::getCacheHandler();
if ($cacheHandler && empty($options['fetch_sql']) && !empty($options['cache'])) {
$options = $query->getOptions();
$resultSet = false;

if ($this->cache && empty($options['fetch_sql']) && !empty($options['cache'])) {
// 判断查询缓存
$cache = $options['cache'];
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options) . serialize($query->getBind(false)));
$resultSet = $cacheHandler->get($key);
$resultSet = $this->cache->get($key);
}

if (false === $resultSet) {
Expand Down Expand Up @@ -1093,9 +1094,9 @@ public function update(Query $query)
return $this->getRealSql($sql, $bind);
} else {
// 检测缓存
if ($cache = Db::getCacheHandler() && isset($key) && $cache->get($key)) {
if ($this->cache && isset($key) && $this->cache->get($key)) {
// 删除缓存
$cache->rm($key);
$this->cache->rm($key);
}

// 执行操作
Expand Down Expand Up @@ -1156,9 +1157,9 @@ public function delete(Query $query)
}

// 检测缓存
if ($cache = Db::getCacheHandler() && isset($key) && $cache->get($key)) {
if ($this->cache && isset($key) && $this->cache->get($key)) {
// 删除缓存
$cache->rm($key);
$this->cache->rm($key);
}

// 执行操作
Expand Down Expand Up @@ -1191,14 +1192,13 @@ public function value(Query $query, $field, $default = null)
{
$options = $query->getOptions();

$result = false;
$cacheHandler = Db::getCacheHandler();
if ($cacheHandler && empty($options['fetch_sql']) && !empty($options['cache'])) {
$result = false;
if ($this->cache && empty($options['fetch_sql']) && !empty($options['cache'])) {
// 判断查询缓存
$cache = $options['cache'];

$key = is_string($cache['key']) ? $cache['key'] : md5($field . serialize($options) . serialize($query->getBind(false)));
$result = $cacheHandler->get($key);
$result = $this->cache->get($key);
}

if (false === $result) {
Expand Down Expand Up @@ -1253,14 +1253,13 @@ public function column(Query $query, $field, $key = '')
{
$options = $query->getOptions();

$result = false;
$cacheHandler = Db::getCacheHandler();
if ($cacheHandler && empty($options['fetch_sql']) && !empty($options['cache'])) {
$result = false;
if ($this->cache && empty($options['fetch_sql']) && !empty($options['cache'])) {
// 判断查询缓存
$cache = $options['cache'];

$guid = is_string($cache['key']) ? $cache['key'] : md5($field . serialize($options) . serialize($query->getBind(false)));
$result = $cacheHandler->get($guid);
$result = $this->cache->get($guid);
}

if (false === $result) {
Expand Down Expand Up @@ -1974,7 +1973,7 @@ public function __destruct()
*/
protected function cacheData($key, $data, $config = [])
{
Db::getCacheHandler()->set($key, $data, $config['expire']);
$this->cache->set($key, $data, $config['expire']);
}

/**
Expand Down
53 changes: 26 additions & 27 deletions src/db/connector/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class Mongo
protected $linkWrite;
// Builder对象
protected $builder;
// 缓存对象
protected $cache;
// 返回或者影响记录数
protected $numRows = 0;
// 错误信息
Expand Down Expand Up @@ -134,6 +136,7 @@ public function __construct(array $config = [])
}

$this->builder = new Builder($this);
$this->cache = Db::getCacheHandler();
}

/**
Expand Down Expand Up @@ -867,9 +870,9 @@ public function update(Query $query)
$writeResult = $this->execute($options['table'], $bulk, $writeConcern);

// 检测缓存
if ($cache = Db::getCacheHandler() && isset($key) && $cache->get($key)) {
if ($this->cache && isset($key) && $this->cache->get($key)) {
// 删除缓存
$cache->rm($key);
$this->cache->rm($key);
}

$result = $writeResult->getModifiedCount();
Expand Down Expand Up @@ -935,9 +938,9 @@ public function delete(Query $query)
$writeResult = $this->execute($options['table'], $bulk, $writeConcern);

// 检测缓存
if ($cache = Db::getCacheHandler() && isset($key) && $cache->get($key)) {
if ($this->cache && isset($key) && $this->cache->get($key)) {
// 删除缓存
$cache->rm($key);
$this->cache->rm($key);
}

$result = $writeResult->getDeletedCount();
Expand Down Expand Up @@ -990,14 +993,13 @@ public function getCursor(Query $query)
*/
public function select(Query $query)
{
$options = $query->getOptions();
$cacheHandler = Db::getCacheHandler();
$resultSet = false;
if ($cacheHandler && !empty($options['cache'])) {
$options = $query->getOptions();
$resultSet = false;
if ($this->cache && !empty($options['cache'])) {
// 判断查询缓存
$cache = $options['cache'];
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options));
$resultSet = $cacheHandler->get($key);
$resultSet = $this->cache->get($key);
}

if (!$resultSet) {
Expand Down Expand Up @@ -1041,24 +1043,23 @@ public function select(Query $query)
public function find(Query $query)
{
// 分析查询表达式
$options = $query->getOptions();
$pk = $query->getPk($options);
$data = $options['data'];
$cacheHandler = Db::getCacheHandler();
if ($cacheHandler && !empty($options['cache']) && true === $options['cache']['key'] && is_string($pk) && isset($options['where']['$and'][$pk])) {
$options = $query->getOptions();
$pk = $query->getPk($options);
$data = $options['data'];
if ($this->cache && !empty($options['cache']) && true === $options['cache']['key'] && is_string($pk) && isset($options['where']['$and'][$pk])) {
$key = $this->getCacheKey($options['where']['$and'][$pk], $options);
}

$result = false;
if ($cacheHandler && !empty($options['cache'])) {
if ($this->cache && !empty($options['cache'])) {
// 判断查询缓存
$cache = $options['cache'];
if (true === $cache['key'] && !is_null($data) && !is_array($data)) {
$key = 'mongo:' . $options['table'] . '|' . $data;
} elseif (!isset($key)) {
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options));
}
$result = $cacheHandler->get($key);
$result = $this->cache->get($key);
}

if (false === $result) {
Expand Down Expand Up @@ -1114,7 +1115,7 @@ public function find(Query $query)
*/
protected function cacheData($key, $data, $config = [])
{
Db::getCacheHandler()->set($key, $data, $config['expire']);
$this->cache->set($key, $data, $config['expire']);
}

/**
Expand Down Expand Up @@ -1199,14 +1200,13 @@ public function getTableInfo($tableName, $fetch = '')
*/
public function value(Query $query, $field, $default = null)
{
$options = $query->getOptions();
$cacheHandler = Db::getCacheHandler();
$result = null;
if ($cacheHandler && !empty($options['cache'])) {
$options = $query->getOptions();
$result = null;
if ($this->cache && !empty($options['cache'])) {
// 判断查询缓存
$cache = $options['cache'];
$key = is_string($cache['key']) ? $cache['key'] : md5($field . serialize($options));
$result = $cacheHandler->get($key);
$result = $this->cache->get($key);
}

if (!$result) {
Expand Down Expand Up @@ -1252,14 +1252,13 @@ public function value(Query $query, $field, $default = null)
*/
public function column(Query $query, $field, $key = '')
{
$options = $query->getOptions();
$cacheHandler = Db::getCacheHandler();
$result = false;
if ($cacheHandler && !empty($options['cache'])) {
$options = $query->getOptions();
$result = false;
if ($this->cache && !empty($options['cache'])) {
// 判断查询缓存
$cache = $options['cache'];
$guid = is_string($cache['key']) ? $cache['key'] : md5($field . serialize($options));
$result = $cacheHandler->get($guid);
$result = $this->cache->get($guid);
}

if (!$result) {
Expand Down

0 comments on commit cdae8ad

Please sign in to comment.