Skip to content

Commit

Permalink
改进查询缓存标识获取
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Jan 14, 2025
1 parent 4e413d8 commit da23671
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
10 changes: 6 additions & 4 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -625,12 +625,13 @@ public function isEmpty(): bool
*
* @param string $field 字段名
* @param float $step 增长值
* @param int $lazyTime 延迟时间(秒)
*
* @return $this
*/
public function inc(string $field, float $step = 1)
public function inc(string $field, float $step = 1, int $lazyTime = 0)
{
$this->setAttr($field, new Express('+', $step));
$this->setAttr($field, new Express('+', $step, $lazyTime));
return $this;
}

Expand All @@ -639,12 +640,13 @@ public function inc(string $field, float $step = 1)
*
* @param string $field 字段名
* @param float $step 增长值
* @param int $lazyTime 延迟时间(秒)
*
* @return $this
*/
public function dec(string $field, float $step = 1)
public function dec(string $field, float $step = 1, int $lazyTime = 0)
{
$this->setAttr($field, new Express('-', $step));
$this->setAttr($field, new Express('-', $step, $lazyTime));
return $this;
}

Expand Down
7 changes: 4 additions & 3 deletions src/db/BaseQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ public function cache($key = true, $expire = null, $tag = null)
$key = true;
}

$this->options['cache'] = [$key, $expire, $tag ?: $this->getTable()];
$this->options['cache'] = [$key, $expire, $tag ?: var_export($this->getTable(), true)];
return $this;
}

Expand Down Expand Up @@ -1666,9 +1666,10 @@ protected function getModelUpdateCondition(array $options)
*/
public function getQueryGuid(): string
{
$table = $this->getConfig('database') . $this->getTable();
$data = $this->options;
unset($data['scope'], $data['default_model']);
$data['database'] = $this->getConfig('database');
$data['table'] = $this->getTable();
foreach (['AND', 'OR', 'XOR'] as $logic) {
if (isset($data['where'][$logic])) {
foreach ($data['where'][$logic] as $key => $val) {
Expand All @@ -1687,6 +1688,6 @@ public function getQueryGuid(): string
}
}

return md5($table . serialize(var_export($data, true)) . serialize($this->getBind(false)));
return md5(serialize(var_export($data, true)) . serialize($this->getBind(false)));
}
}
2 changes: 1 addition & 1 deletion src/db/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ protected function cacheData(CacheItem $cacheItem)
protected function getCacheKey(BaseQuery $query, string $method = ''): string
{
if (!empty($query->getOptions('key')) && empty($method)) {
$key = 'think_' . $this->getConfig('database') . '.' . $query->getTable() . '|' . var_export($query->getOptions('key'), true);
$key = 'think_' . $this->getConfig('database') . '.' . var_export($query->getTable(), true) . '|' . var_export($query->getOptions('key'), true);
} else {
$key = $query->getQueryGuid();
}
Expand Down

0 comments on commit da23671

Please sign in to comment.