23
23
use MongoDB \Driver \ReadPreference ;
24
24
use MongoDB \Driver \WriteConcern ;
25
25
use think \Collection ;
26
- use think \Container ;
27
26
use think \Db ;
28
27
use think \db \builder \Mongo as Builder ;
29
28
use think \db \Mongo as Query ;
@@ -61,6 +60,8 @@ class Mongo
61
60
protected $ options = [];
62
61
// 数据表信息
63
62
protected static $ info = [];
63
+ // 数据库日志
64
+ protected static $ log = [];
64
65
// 数据库连接参数配置
65
66
protected $ config = [
66
67
// 数据库类型
@@ -536,7 +537,12 @@ protected function triggerSql($sql, $runtime, $options = [])
536
537
537
538
public function logger ($ log , $ type = 'sql ' )
538
539
{
539
- $ this ->config ['debug ' ] && Container::get ('log ' )->record ($ log , $ type );
540
+ $ this ->config ['debug ' ] && self ::$ log [] = $ log ;
541
+ }
542
+
543
+ public function getSqlLog ()
544
+ {
545
+ return self ::$ log ;
540
546
}
541
547
542
548
/**
@@ -550,14 +556,10 @@ protected function debug($start, $sql = '')
550
556
{
551
557
if (!empty ($ this ->config ['debug ' ])) {
552
558
// 开启数据库调试模式
553
- $ debug = Container::get ('debug ' );
554
559
if ($ start ) {
555
- $ debug -> remark ( ' queryStartTime ' , ' time ' );
560
+ $ this -> queryStartTime = microtime ( true );
556
561
} else {
557
- // 记录操作结束时间
558
- $ debug ->remark ('queryEndTime ' , 'time ' );
559
-
560
- $ runtime = $ debug ->getRangeTime ('queryStartTime ' , 'queryEndTime ' );
562
+ $ runtime = number_format ((microtime (true ) - $ this ->queryStartTime ), 6 );
561
563
562
564
$ sql = $ sql ?: $ this ->queryStr ;
563
565
@@ -865,9 +867,9 @@ public function update(Query $query)
865
867
$ writeResult = $ this ->execute ($ options ['table ' ], $ bulk , $ writeConcern );
866
868
867
869
// 检测缓存
868
- if (isset ($ key ) && Container:: get ( ' cache ' ) ->get ($ key )) {
870
+ if ($ cache = Db:: getCacheHandler () && isset ($ key ) && $ cache ->get ($ key )) {
869
871
// 删除缓存
870
- Container:: get ( ' cache ' ) ->rm ($ key );
872
+ $ cache ->rm ($ key );
871
873
}
872
874
873
875
$ result = $ writeResult ->getModifiedCount ();
@@ -933,9 +935,9 @@ public function delete(Query $query)
933
935
$ writeResult = $ this ->execute ($ options ['table ' ], $ bulk , $ writeConcern );
934
936
935
937
// 检测缓存
936
- if (isset ($ key ) && Container:: get ( ' cache ' ) ->get ($ key )) {
938
+ if ($ cache = Db:: getCacheHandler () && isset ($ key ) && $ cache ->get ($ key )) {
937
939
// 删除缓存
938
- Container:: get ( ' cache ' ) ->rm ($ key );
940
+ $ cache ->rm ($ key );
939
941
}
940
942
941
943
$ result = $ writeResult ->getDeletedCount ();
@@ -988,14 +990,14 @@ public function getCursor(Query $query)
988
990
*/
989
991
public function select (Query $ query )
990
992
{
991
- $ options = $ query ->getOptions ();
992
-
993
- $ resultSet = false ;
994
- if (!empty ($ options ['cache ' ])) {
993
+ $ options = $ query ->getOptions ();
994
+ $ cacheHandler = Db:: getCacheHandler ();
995
+ $ resultSet = false ;
996
+ if ($ cacheHandler && !empty ($ options ['cache ' ])) {
995
997
// 判断查询缓存
996
998
$ cache = $ options ['cache ' ];
997
999
$ key = is_string ($ cache ['key ' ]) ? $ cache ['key ' ] : md5 (serialize ($ options ));
998
- $ resultSet = Container:: get ( ' cache ' ) ->get ($ key );
1000
+ $ resultSet = $ cacheHandler ->get ($ key );
999
1001
}
1000
1002
1001
1003
if (!$ resultSet ) {
@@ -1039,24 +1041,24 @@ public function select(Query $query)
1039
1041
public function find (Query $ query )
1040
1042
{
1041
1043
// 分析查询表达式
1042
- $ options = $ query ->getOptions ();
1043
- $ pk = $ query ->getPk ($ options );
1044
- $ data = $ options ['data ' ];
1045
-
1046
- if (!empty ($ options ['cache ' ]) && true === $ options ['cache ' ]['key ' ] && is_string ($ pk ) && isset ($ options ['where ' ]['$and ' ][$ pk ])) {
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 ])) {
1047
1049
$ key = $ this ->getCacheKey ($ options ['where ' ]['$and ' ][$ pk ], $ options );
1048
1050
}
1049
1051
1050
1052
$ result = false ;
1051
- if (!empty ($ options ['cache ' ])) {
1053
+ if ($ cacheHandler && !empty ($ options ['cache ' ])) {
1052
1054
// 判断查询缓存
1053
1055
$ cache = $ options ['cache ' ];
1054
1056
if (true === $ cache ['key ' ] && !is_null ($ data ) && !is_array ($ data )) {
1055
1057
$ key = 'mongo: ' . $ options ['table ' ] . '| ' . $ data ;
1056
1058
} elseif (!isset ($ key )) {
1057
1059
$ key = is_string ($ cache ['key ' ]) ? $ cache ['key ' ] : md5 (serialize ($ options ));
1058
1060
}
1059
- $ result = Container:: get ( ' cache ' ) ->get ($ key );
1061
+ $ result = $ cacheHandler ->get ($ key );
1060
1062
}
1061
1063
1062
1064
if (false === $ result ) {
@@ -1112,13 +1114,7 @@ public function find(Query $query)
1112
1114
*/
1113
1115
protected function cacheData ($ key , $ data , $ config = [])
1114
1116
{
1115
- $ cache = Container::get ('cache ' );
1116
-
1117
- if (isset ($ config ['tag ' ])) {
1118
- $ cache ->tag ($ config ['tag ' ])->set ($ key , $ data , $ config ['expire ' ]);
1119
- } else {
1120
- $ cache ->set ($ key , $ data , $ config ['expire ' ]);
1121
- }
1117
+ Db::getCacheHandler ()->set ($ key , $ data , $ config ['expire ' ]);
1122
1118
}
1123
1119
1124
1120
/**
@@ -1203,14 +1199,14 @@ public function getTableInfo($tableName, $fetch = '')
1203
1199
*/
1204
1200
public function value (Query $ query , $ field , $ default = null )
1205
1201
{
1206
- $ options = $ query ->getOptions ();
1207
-
1208
- $ result = null ;
1209
- if (!empty ($ options ['cache ' ])) {
1202
+ $ options = $ query ->getOptions ();
1203
+ $ cacheHandler = Db:: getCacheHandler ();
1204
+ $ result = null ;
1205
+ if ($ cacheHandler && !empty ($ options ['cache ' ])) {
1210
1206
// 判断查询缓存
1211
1207
$ cache = $ options ['cache ' ];
1212
1208
$ key = is_string ($ cache ['key ' ]) ? $ cache ['key ' ] : md5 ($ field . serialize ($ options ));
1213
- $ result = Container:: get ( ' cache ' ) ->get ($ key );
1209
+ $ result = $ cacheHandler ->get ($ key );
1214
1210
}
1215
1211
1216
1212
if (!$ result ) {
@@ -1256,14 +1252,14 @@ public function value(Query $query, $field, $default = null)
1256
1252
*/
1257
1253
public function column (Query $ query , $ field , $ key = '' )
1258
1254
{
1259
- $ options = $ query ->getOptions ();
1260
-
1261
- $ result = false ;
1262
- if (!empty ($ options ['cache ' ])) {
1255
+ $ options = $ query ->getOptions ();
1256
+ $ cacheHandler = Db:: getCacheHandler ();
1257
+ $ result = false ;
1258
+ if ($ cacheHandler && !empty ($ options ['cache ' ])) {
1263
1259
// 判断查询缓存
1264
1260
$ cache = $ options ['cache ' ];
1265
1261
$ guid = is_string ($ cache ['key ' ]) ? $ cache ['key ' ] : md5 ($ field . serialize ($ options ));
1266
- $ result = Container:: get ( ' cache ' ) ->get ($ guid );
1262
+ $ result = $ cacheHandler ->get ($ guid );
1267
1263
}
1268
1264
1269
1265
if (!$ result ) {
@@ -1359,10 +1355,10 @@ public function cmd(Query $query, $command, $extra = null, $db = null)
1359
1355
private static function parseConfig ($ config )
1360
1356
{
1361
1357
if (empty ($ config )) {
1362
- $ config = Container:: get ( ' config ' )-> pull ( ' database ' );
1358
+ $ config = Db:: getConfig ( );
1363
1359
} elseif (is_string ($ config ) && false === strpos ($ config , '/ ' )) {
1364
1360
// 支持读取配置参数
1365
- $ config = Container:: get ( ' config ' )-> get ( ' database. ' . $ config );
1361
+ $ config = Db:: getConfig ( $ config );
1366
1362
}
1367
1363
1368
1364
if (is_string ($ config )) {
0 commit comments