@@ -52,6 +52,8 @@ class Mongo
52
52
protected $ linkWrite ;
53
53
// Builder对象
54
54
protected $ builder ;
55
+ // 缓存对象
56
+ protected $ cache ;
55
57
// 返回或者影响记录数
56
58
protected $ numRows = 0 ;
57
59
// 错误信息
@@ -134,6 +136,7 @@ public function __construct(array $config = [])
134
136
}
135
137
136
138
$ this ->builder = new Builder ($ this );
139
+ $ this ->cache = Db::getCacheHandler ();
137
140
}
138
141
139
142
/**
@@ -867,9 +870,9 @@ public function update(Query $query)
867
870
$ writeResult = $ this ->execute ($ options ['table ' ], $ bulk , $ writeConcern );
868
871
869
872
// 检测缓存
870
- if ($ cache = Db:: getCacheHandler () && isset ($ key ) && $ cache ->get ($ key )) {
873
+ if ($ this -> cache && isset ($ key ) && $ this -> cache ->get ($ key )) {
871
874
// 删除缓存
872
- $ cache ->rm ($ key );
875
+ $ this -> cache ->rm ($ key );
873
876
}
874
877
875
878
$ result = $ writeResult ->getModifiedCount ();
@@ -935,9 +938,9 @@ public function delete(Query $query)
935
938
$ writeResult = $ this ->execute ($ options ['table ' ], $ bulk , $ writeConcern );
936
939
937
940
// 检测缓存
938
- if ($ cache = Db:: getCacheHandler () && isset ($ key ) && $ cache ->get ($ key )) {
941
+ if ($ this -> cache && isset ($ key ) && $ this -> cache ->get ($ key )) {
939
942
// 删除缓存
940
- $ cache ->rm ($ key );
943
+ $ this -> cache ->rm ($ key );
941
944
}
942
945
943
946
$ result = $ writeResult ->getDeletedCount ();
@@ -990,14 +993,13 @@ public function getCursor(Query $query)
990
993
*/
991
994
public function select (Query $ query )
992
995
{
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 ' ])) {
997
999
// 判断查询缓存
998
1000
$ cache = $ options ['cache ' ];
999
1001
$ key = is_string ($ cache ['key ' ]) ? $ cache ['key ' ] : md5 (serialize ($ options ));
1000
- $ resultSet = $ cacheHandler ->get ($ key );
1002
+ $ resultSet = $ this -> cache ->get ($ key );
1001
1003
}
1002
1004
1003
1005
if (!$ resultSet ) {
@@ -1041,24 +1043,23 @@ public function select(Query $query)
1041
1043
public function find (Query $ query )
1042
1044
{
1043
1045
// 分析查询表达式
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 ])) {
1049
1050
$ key = $ this ->getCacheKey ($ options ['where ' ]['$and ' ][$ pk ], $ options );
1050
1051
}
1051
1052
1052
1053
$ result = false ;
1053
- if ($ cacheHandler && !empty ($ options ['cache ' ])) {
1054
+ if ($ this -> cache && !empty ($ options ['cache ' ])) {
1054
1055
// 判断查询缓存
1055
1056
$ cache = $ options ['cache ' ];
1056
1057
if (true === $ cache ['key ' ] && !is_null ($ data ) && !is_array ($ data )) {
1057
1058
$ key = 'mongo: ' . $ options ['table ' ] . '| ' . $ data ;
1058
1059
} elseif (!isset ($ key )) {
1059
1060
$ key = is_string ($ cache ['key ' ]) ? $ cache ['key ' ] : md5 (serialize ($ options ));
1060
1061
}
1061
- $ result = $ cacheHandler ->get ($ key );
1062
+ $ result = $ this -> cache ->get ($ key );
1062
1063
}
1063
1064
1064
1065
if (false === $ result ) {
@@ -1114,7 +1115,7 @@ public function find(Query $query)
1114
1115
*/
1115
1116
protected function cacheData ($ key , $ data , $ config = [])
1116
1117
{
1117
- Db:: getCacheHandler () ->set ($ key , $ data , $ config ['expire ' ]);
1118
+ $ this -> cache ->set ($ key , $ data , $ config ['expire ' ]);
1118
1119
}
1119
1120
1120
1121
/**
@@ -1199,14 +1200,13 @@ public function getTableInfo($tableName, $fetch = '')
1199
1200
*/
1200
1201
public function value (Query $ query , $ field , $ default = null )
1201
1202
{
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 ' ])) {
1206
1206
// 判断查询缓存
1207
1207
$ cache = $ options ['cache ' ];
1208
1208
$ key = is_string ($ cache ['key ' ]) ? $ cache ['key ' ] : md5 ($ field . serialize ($ options ));
1209
- $ result = $ cacheHandler ->get ($ key );
1209
+ $ result = $ this -> cache ->get ($ key );
1210
1210
}
1211
1211
1212
1212
if (!$ result ) {
@@ -1252,14 +1252,13 @@ public function value(Query $query, $field, $default = null)
1252
1252
*/
1253
1253
public function column (Query $ query , $ field , $ key = '' )
1254
1254
{
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 ' ])) {
1259
1258
// 判断查询缓存
1260
1259
$ cache = $ options ['cache ' ];
1261
1260
$ guid = is_string ($ cache ['key ' ]) ? $ cache ['key ' ] : md5 ($ field . serialize ($ options ));
1262
- $ result = $ cacheHandler ->get ($ guid );
1261
+ $ result = $ this -> cache ->get ($ guid );
1263
1262
}
1264
1263
1265
1264
if (!$ result ) {
0 commit comments