Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

改进缓存key生成逻辑,支持闭包条件也参与序列化 #649

Open
wants to merge 1 commit into
base: 3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/db/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -517,12 +517,24 @@ protected function getLazyFieldCacheKey(string $field, $id = null): string
*/
public function getQueryGuid($data = null): string
{
$logic_closure_string = '';
if (null === $data) {
$data = $this->options;
unset($data['scope'], $data['default_model']);
$logic_list = ['AND', 'OR', 'XOR'];
foreach ($logic_list as $logic) {
if (isset($data['where']) && isset($data['where'][$logic])) {
foreach ($this->options['where'][$logic] as $key => $val) {
if ($val instanceof \Closure) {
$reflection = new \ReflectionFunction($val);
$static_properties = $reflection->getStaticVariables();
$logic_closure_string .= $logic . print_r($static_properties, true);
}
}
}
}
}

return md5($this->getConfig('database') . serialize(var_export($data, true)) . serialize($this->getBind(false)));
return md5($this->getConfig('database') . serialize(var_export($data, true)) . serialize($this->getBind(false)) . serialize($logic_closure_string));
}

/**
Expand Down
17 changes: 9 additions & 8 deletions src/db/concern/WhereQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ trait WhereQuery
*/
public function where($field, $op = null, $condition = null)
{
$logic = 'AND';
if ($field instanceof $this) {
$this->parseQueryWhere($field);

return $this;
} elseif (true === $field || 1 === $field) {
$this->options['where']['AND'][] = true;
$this->options['where'][$logic][] = true;

return $this;
}
Expand All @@ -49,11 +50,11 @@ public function where($field, $op = null, $condition = null)
array_shift($param);

if (is_array($field)) {
return $this->where(function ($query) use ($param, $condition, $op, $field) {
return $query->parseWhereExp('AND', $field, $op, $condition, $param);
return $this->where(function ($query) use ($param, $condition, $op, $field, $logic) {
return $query->parseWhereExp($logic, $field, $op, $condition, $param);
});
}
return $this->parseWhereExp('AND', $field, $op, $condition, $param);
return $this->parseWhereExp($logic, $field, $op, $condition, $param);
}

/**
Expand Down Expand Up @@ -95,14 +96,14 @@ public function whereOr($field, $op = null, $condition = null)
{
$param = func_get_args();
array_shift($param);

$logic = 'OR';
if (is_array($field)) {
return $this->where(function ($query) use ($param, $condition, $op, $field) {
return $query->parseWhereExp('OR', $field, $op, $condition, $param);
return $this->where(function ($query) use ($param, $condition, $op, $field, $logic) {
return $query->parseWhereExp($logic, $field, $op, $condition, $param);
});
}

return $this->parseWhereExp('OR', $field, $op, $condition, $param);
return $this->parseWhereExp($logic, $field, $op, $condition, $param);
}

/**
Expand Down