Skip to content

Commit cdae8ad

Browse files
committed
改进Connection类
1 parent cba6f2a commit cdae8ad

File tree

2 files changed

+50
-52
lines changed

2 files changed

+50
-52
lines changed

src/db/Connection.php

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ abstract class Connection
4242
protected $linkID;
4343
protected $linkRead;
4444
protected $linkWrite;
45-
45+
// 当前缓存对象
46+
protected $cache;
4647
// 查询结果类型
4748
protected $fetchType = PDO::FETCH_ASSOC;
4849
// 字段属性大小写
@@ -141,6 +142,7 @@ protected function __construct(array $config = [])
141142
$class = $this->getBuilderClass();
142143

143144
$this->builder = new $class($this);
145+
$this->cache = Db::getCacheHandler();
144146

145147
// 执行初始化操作
146148
$this->initialize();
@@ -759,18 +761,17 @@ public function execute($sql, $bind = [])
759761
public function find(Query $query)
760762
{
761763
// 分析查询表达式
762-
$options = $query->getOptions();
763-
$pk = $query->getPk($options);
764-
$cacheHandler = Db::getCacheHandler();
764+
$options = $query->getOptions();
765+
$pk = $query->getPk($options);
765766

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

770771
$data = $options['data'];
771772
$result = false;
772773

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

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

783-
$result = $cacheHandler->get($key);
784+
$result = $this->cache->get($key);
784785
}
785786

786787
if (false === $result) {
@@ -866,14 +867,14 @@ public function cursor(Query $query)
866867
public function select(Query $query)
867868
{
868869
// 分析查询表达式
869-
$options = $query->getOptions();
870-
$resultSet = false;
871-
$cacheHandler = Db::getCacheHandler();
872-
if ($cacheHandler && empty($options['fetch_sql']) && !empty($options['cache'])) {
870+
$options = $query->getOptions();
871+
$resultSet = false;
872+
873+
if ($this->cache && empty($options['fetch_sql']) && !empty($options['cache'])) {
873874
// 判断查询缓存
874875
$cache = $options['cache'];
875876
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options) . serialize($query->getBind(false)));
876-
$resultSet = $cacheHandler->get($key);
877+
$resultSet = $this->cache->get($key);
877878
}
878879

879880
if (false === $resultSet) {
@@ -1093,9 +1094,9 @@ public function update(Query $query)
10931094
return $this->getRealSql($sql, $bind);
10941095
} else {
10951096
// 检测缓存
1096-
if ($cache = Db::getCacheHandler() && isset($key) && $cache->get($key)) {
1097+
if ($this->cache && isset($key) && $this->cache->get($key)) {
10971098
// 删除缓存
1098-
$cache->rm($key);
1099+
$this->cache->rm($key);
10991100
}
11001101

11011102
// 执行操作
@@ -1156,9 +1157,9 @@ public function delete(Query $query)
11561157
}
11571158

11581159
// 检测缓存
1159-
if ($cache = Db::getCacheHandler() && isset($key) && $cache->get($key)) {
1160+
if ($this->cache && isset($key) && $this->cache->get($key)) {
11601161
// 删除缓存
1161-
$cache->rm($key);
1162+
$this->cache->rm($key);
11621163
}
11631164

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

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

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

12041204
if (false === $result) {
@@ -1253,14 +1253,13 @@ public function column(Query $query, $field, $key = '')
12531253
{
12541254
$options = $query->getOptions();
12551255

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

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

12661265
if (false === $result) {
@@ -1974,7 +1973,7 @@ public function __destruct()
19741973
*/
19751974
protected function cacheData($key, $data, $config = [])
19761975
{
1977-
Db::getCacheHandler()->set($key, $data, $config['expire']);
1976+
$this->cache->set($key, $data, $config['expire']);
19781977
}
19791978

19801979
/**

src/db/connector/Mongo.php

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class Mongo
5252
protected $linkWrite;
5353
// Builder对象
5454
protected $builder;
55+
// 缓存对象
56+
protected $cache;
5557
// 返回或者影响记录数
5658
protected $numRows = 0;
5759
// 错误信息
@@ -134,6 +136,7 @@ public function __construct(array $config = [])
134136
}
135137

136138
$this->builder = new Builder($this);
139+
$this->cache = Db::getCacheHandler();
137140
}
138141

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

869872
// 检测缓存
870-
if ($cache = Db::getCacheHandler() && isset($key) && $cache->get($key)) {
873+
if ($this->cache && isset($key) && $this->cache->get($key)) {
871874
// 删除缓存
872-
$cache->rm($key);
875+
$this->cache->rm($key);
873876
}
874877

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

937940
// 检测缓存
938-
if ($cache = Db::getCacheHandler() && isset($key) && $cache->get($key)) {
941+
if ($this->cache && isset($key) && $this->cache->get($key)) {
939942
// 删除缓存
940-
$cache->rm($key);
943+
$this->cache->rm($key);
941944
}
942945

943946
$result = $writeResult->getDeletedCount();
@@ -990,14 +993,13 @@ public function getCursor(Query $query)
990993
*/
991994
public function select(Query $query)
992995
{
993-
$options = $query->getOptions();
994-
$cacheHandler = Db::getCacheHandler();
995-
$resultSet = false;
996-
if ($cacheHandler && !empty($options['cache'])) {
996+
$options = $query->getOptions();
997+
$resultSet = false;
998+
if ($this->cache && !empty($options['cache'])) {
997999
// 判断查询缓存
9981000
$cache = $options['cache'];
9991001
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options));
1000-
$resultSet = $cacheHandler->get($key);
1002+
$resultSet = $this->cache->get($key);
10011003
}
10021004

10031005
if (!$resultSet) {
@@ -1041,24 +1043,23 @@ public function select(Query $query)
10411043
public function find(Query $query)
10421044
{
10431045
// 分析查询表达式
1044-
$options = $query->getOptions();
1045-
$pk = $query->getPk($options);
1046-
$data = $options['data'];
1047-
$cacheHandler = Db::getCacheHandler();
1048-
if ($cacheHandler && !empty($options['cache']) && true === $options['cache']['key'] && is_string($pk) && isset($options['where']['$and'][$pk])) {
1046+
$options = $query->getOptions();
1047+
$pk = $query->getPk($options);
1048+
$data = $options['data'];
1049+
if ($this->cache && !empty($options['cache']) && true === $options['cache']['key'] && is_string($pk) && isset($options['where']['$and'][$pk])) {
10491050
$key = $this->getCacheKey($options['where']['$and'][$pk], $options);
10501051
}
10511052

10521053
$result = false;
1053-
if ($cacheHandler && !empty($options['cache'])) {
1054+
if ($this->cache && !empty($options['cache'])) {
10541055
// 判断查询缓存
10551056
$cache = $options['cache'];
10561057
if (true === $cache['key'] && !is_null($data) && !is_array($data)) {
10571058
$key = 'mongo:' . $options['table'] . '|' . $data;
10581059
} elseif (!isset($key)) {
10591060
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options));
10601061
}
1061-
$result = $cacheHandler->get($key);
1062+
$result = $this->cache->get($key);
10621063
}
10631064

10641065
if (false === $result) {
@@ -1114,7 +1115,7 @@ public function find(Query $query)
11141115
*/
11151116
protected function cacheData($key, $data, $config = [])
11161117
{
1117-
Db::getCacheHandler()->set($key, $data, $config['expire']);
1118+
$this->cache->set($key, $data, $config['expire']);
11181119
}
11191120

11201121
/**
@@ -1199,14 +1200,13 @@ public function getTableInfo($tableName, $fetch = '')
11991200
*/
12001201
public function value(Query $query, $field, $default = null)
12011202
{
1202-
$options = $query->getOptions();
1203-
$cacheHandler = Db::getCacheHandler();
1204-
$result = null;
1205-
if ($cacheHandler && !empty($options['cache'])) {
1203+
$options = $query->getOptions();
1204+
$result = null;
1205+
if ($this->cache && !empty($options['cache'])) {
12061206
// 判断查询缓存
12071207
$cache = $options['cache'];
12081208
$key = is_string($cache['key']) ? $cache['key'] : md5($field . serialize($options));
1209-
$result = $cacheHandler->get($key);
1209+
$result = $this->cache->get($key);
12101210
}
12111211

12121212
if (!$result) {
@@ -1252,14 +1252,13 @@ public function value(Query $query, $field, $default = null)
12521252
*/
12531253
public function column(Query $query, $field, $key = '')
12541254
{
1255-
$options = $query->getOptions();
1256-
$cacheHandler = Db::getCacheHandler();
1257-
$result = false;
1258-
if ($cacheHandler && !empty($options['cache'])) {
1255+
$options = $query->getOptions();
1256+
$result = false;
1257+
if ($this->cache && !empty($options['cache'])) {
12591258
// 判断查询缓存
12601259
$cache = $options['cache'];
12611260
$guid = is_string($cache['key']) ? $cache['key'] : md5($field . serialize($options));
1262-
$result = $cacheHandler->get($guid);
1261+
$result = $this->cache->get($guid);
12631262
}
12641263

12651264
if (!$result) {

0 commit comments

Comments
 (0)