@@ -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