Skip to content

Commit 6e0ea67

Browse files
committed
改进缓存标识自动生成
1 parent 4556263 commit 6e0ea67

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

src/db/Query.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
namespace think\db;
1515

16+
use Closure;
1617
use PDOStatement;
18+
use ReflectionFunction;
1719
use think\db\exception\DbException as Exception;
1820

1921
/**
@@ -518,11 +520,29 @@ protected function getLazyFieldCacheKey(string $field, $id = null): string
518520
public function getQueryGuid($data = null): string
519521
{
520522
if (null === $data) {
521-
$data = $this->options;
523+
$data = $this->options;
524+
$data['table'] = $this->getConfig('database') . $this->getTable();
522525
unset($data['scope'], $data['default_model']);
526+
foreach (['AND', 'OR', 'XOR'] as $logic) {
527+
if (isset($data['where'][$logic])) {
528+
foreach ($data['where'][$logic] as $key => $val) {
529+
if ($val instanceof Closure) {
530+
$reflection = new ReflectionFunction($val);
531+
$properties = $reflection->getStaticVariables();
532+
if (empty($properties)) {
533+
$name = $reflection->getName() . $reflection->getStartLine() . '-' . $reflection->getEndLine();
534+
} else {
535+
$name = var_export($properties, true);
536+
}
537+
$data['Closure'][] = $name;
538+
unset($data['where'][$logic][$key]);
539+
}
540+
}
541+
}
542+
}
523543
}
524544

525-
return md5($this->getConfig('database') . serialize(var_export($data, true)) . serialize($this->getBind(false)));
545+
return md5(serialize(var_export($data, true)) . serialize($this->getBind(false)));
526546
}
527547

528548
/**

src/db/concern/WhereQuery.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,16 @@ public function where($field, $op = null, $condition = null)
4747
$this->options['key'] = is_null($condition) ? $op : $condition;
4848
}
4949

50+
$logic = 'AND';
5051
$param = func_get_args();
5152
array_shift($param);
5253

5354
if (is_array($field) && !empty($field) && array_is_list($field)) {
54-
return $this->where(function ($query) use ($param, $condition, $op, $field) {
55-
return $query->parseWhereExp('AND', $field, $op, $condition, $param);
55+
return $this->where(function ($query) use ($param, $condition, $op, $field, $logic) {
56+
return $query->parseWhereExp($logic, $field, $op, $condition, $param);
5657
});
5758
}
58-
return $this->parseWhereExp('AND', $field, $op, $condition, $param);
59+
return $this->parseWhereExp($logic, $field, $op, $condition, $param);
5960
}
6061

6162
/**
@@ -95,16 +96,17 @@ protected function parseQueryWhere(BaseQuery $query): void
9596
*/
9697
public function whereOr($field, $op = null, $condition = null)
9798
{
99+
$logic = 'OR';
98100
$param = func_get_args();
99101
array_shift($param);
100102

101103
if (is_array($field) && !empty($field) && array_is_list($field)) {
102-
return $this->where(function ($query) use ($param, $condition, $op, $field) {
103-
return $query->parseWhereExp('OR', $field, $op, $condition, $param);
104+
return $this->where(function ($query) use ($param, $condition, $op, $field, $logic) {
105+
return $query->parseWhereExp($logic, $field, $op, $condition, $param);
104106
});
105107
}
106108

107-
return $this->parseWhereExp('OR', $field, $op, $condition, $param);
109+
return $this->parseWhereExp($logic, $field, $op, $condition, $param);
108110
}
109111

110112
/**
@@ -118,15 +120,16 @@ public function whereOr($field, $op = null, $condition = null)
118120
*/
119121
public function whereXor($field, $op = null, $condition = null)
120122
{
123+
$logic = 'XOR';
121124
$param = func_get_args();
122125
array_shift($param);
123126

124127
if (is_array($field) && !empty($field) && array_is_list($field)) {
125-
return $this->where(function ($query) use ($param, $condition, $op, $field) {
126-
return $query->parseWhereExp('XOR', $field, $op, $condition, $param);
128+
return $this->where(function ($query) use ($param, $condition, $op, $field, $logic) {
129+
return $query->parseWhereExp($logic, $field, $op, $condition, $param);
127130
});
128131
}
129-
return $this->parseWhereExp('XOR', $field, $op, $condition, $param);
132+
return $this->parseWhereExp($logic, $field, $op, $condition, $param);
130133
}
131134

132135
/**

0 commit comments

Comments
 (0)