Skip to content

Commit 5cbb573

Browse files
authored
Merge pull request #34 from teamones-open/dev_weijer
add: 支持缓存字段
2 parents d495f6b + b077ed4 commit 5cbb573

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

src/think/Model.php

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -301,30 +301,40 @@ protected function _checkTableInfo()
301301
// 如果不是Model类 自动记录数据表信息
302302
// 只在第一次执行记录
303303
if (empty($this->fields)) {
304-
// 如果数据表字段没有定义则自动获取
305-
if (C('DB_FIELDS_CACHE')) {
306-
$fields = F('_fields/' . strtolower($this->getTableName()));
307-
if ($fields) {
308-
$this->fields = $fields;
309-
if (!empty($fields['_pk'])) {
310-
$this->pk = $fields['_pk'];
311-
}
312-
return;
304+
305+
// 每次都会读取数据表信息
306+
$fields = $this->flush();
307+
308+
if (!empty($fields)) {
309+
$this->fields = $fields;
310+
if (!empty($fields['_pk'])) {
311+
$this->pk = $fields['_pk'];
313312
}
313+
return;
314314
}
315-
// 每次都会读取数据表信息
316-
$this->flush();
317315
}
318316
}
319317

320318
/**
321319
* 获取字段信息并缓存
320+
* @param string $tableName
321+
* @return mixed
322322
*/
323-
public function flush()
323+
public function flush($tableName = '')
324324
{
325+
if(empty($tableName)){
326+
$tableName = $this->getTableName();
327+
}
328+
329+
if (C('DB_FIELDS_CACHE')) {
330+
$fieldsCache = S('fields_' . strtolower($tableName));
331+
if(!empty($fieldsCache)){
332+
return $fieldsCache;
333+
}
334+
}
335+
325336
// 缓存不存在则查询数据表信息
326337
$this->db->setModel($this->name);
327-
$tableName = $this->getTableName();
328338
$fields = $this->db->getFields($tableName);
329339

330340
if (!empty($fields)) {
@@ -357,8 +367,8 @@ public function flush()
357367

358368
// 增加缓存开关控制
359369
if (C('DB_FIELDS_CACHE')) {
360-
// 永久缓存数据表信息
361-
F('_fields/' . strtolower($tableName), $this->fields);
370+
// 永久缓存数据表信息, 缓存一个小时
371+
S('fields_' . strtolower($tableName), $this->fields, 3600);
362372
}
363373
}
364374
}
@@ -2411,8 +2421,15 @@ public function getDbFields()
24112421
return false;
24122422
}
24132423
}
2414-
$fields = $this->db->getFields($table);
2415-
return $fields ? array_keys($fields) : false;
2424+
2425+
$fields = $this->flush($table);
2426+
2427+
if(!empty($fields)){
2428+
unset($fields['_type'], $fields['_pk']);
2429+
return $fields;
2430+
}
2431+
2432+
return false;
24162433
}
24172434
if ($this->fields) {
24182435
$fields = $this->fields;

0 commit comments

Comments
 (0)