Skip to content

Commit

Permalink
inc、dec助手函数增加lazyTime参数支持
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Jan 14, 2025
1 parent 0ee0bf9 commit d6658ea
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
10 changes: 9 additions & 1 deletion src/db/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,15 @@ protected function parseData(Query $query, array $data = [], array $fields = [],
throw new Exception('fields not exists:[' . $key . ']');
}
} elseif ($val instanceof Express) {
$result[$item] = $item . $this->parseExpress($query, $val);
if ($val->getLazyTime() && in_array($val->getType(), ['+', '-'])) {
$step = $query->lazyWrite($key, $val->getType() == '+' ? 'inc' : 'dec', $guid, $val->getStep(), $val->getLazyTime());
if (false === $step) {
continue;
}
$result[$item] = $item . ' + ' . $step;
} else {
$result[$item] = $item . $this->parseExpress($query, $val);
}
} elseif (is_array($val) && !empty($val) && is_string($val[0])) {
if (in_array(strtoupper($val[0]), ['INC', 'DEC'])) {
$result[$item] = match (strtoupper($val[0])) {
Expand Down
8 changes: 7 additions & 1 deletion src/db/Express.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ class Express
*
* @param string $type
* @param float $value
* @param int $lazyTime
*
* @return void
*/
public function __construct(protected string $type, protected float $step)
public function __construct(protected string $type, protected float $step = 1, protected int $lazyTime = 0)
{
}

Expand All @@ -40,6 +41,11 @@ public function getType()
return $this->type;
}

public function getLazyTime()
{
return $this->lazyTime;
}

/**
* 获取表达式.
*
Expand Down
12 changes: 6 additions & 6 deletions src/db/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,7 @@ public function getAutoInc()
public function inc(string $field, float $step = 1, int $lazyTime = 0)
{
if ($lazyTime > 0) {
$guid = $this->getLazyFieldCacheKey($field);
$step = $this->lazyWrite('inc', $guid, $step, $lazyTime);
$step = $this->lazyWrite($field, 'inc', $guid, $step, $lazyTime);
if (false === $step) {
return $this;
}
Expand All @@ -394,8 +393,7 @@ public function inc(string $field, float $step = 1, int $lazyTime = 0)
public function dec(string $field, float $step = 1, int $lazyTime = 0)
{
if ($lazyTime > 0) {
$guid = $this->getLazyFieldCacheKey($field);
$step = $this->lazyWrite('dec', $guid, $step, $lazyTime);
$step = $this->lazyWrite($field, 'dec', $guid, $step, $lazyTime);
if (false === $step) {
return $this;
}
Expand Down Expand Up @@ -456,15 +454,17 @@ public function setDec(string $field, float $step = 1, int $lazyTime = 0)
/**
* 延时更新检查 返回false表示需要延时
* 否则返回实际写入的数值
* @access protected
* @access public
* @param string $field 字段名
* @param string $type 自增或者自减
* @param string $guid 写入标识
* @param float $step 写入步进值
* @param int $lazyTime 延时时间(s)
* @return false|integer
*/
protected function lazyWrite(string $type, string $guid, float $step, int $lazyTime)
public function lazyWrite(string $field, string $type, string $guid, float $step, int $lazyTime)
{
$guid = $this->getLazyFieldCacheKey($field);
$cache = $this->getCache();
if (!$cache->has($guid . '_time')) {
// 计时开始
Expand Down
8 changes: 4 additions & 4 deletions src/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ function raw(string $value, array $bind = []): Raw
}

if (!function_exists('inc')) {
function inc(float $step = 1): Express
function inc(float $step = 1, int $lazyTime = 0): Express
{
return new Express('+', $step);
return new Express('+', $step, $lazyTime);
}
}

if (!function_exists('dec')) {
function dec(float $step = 1): Express
function dec(float $step = 1, int $lazyTime = 0): Express
{
return new Express('-', $step);
return new Express('-', $step, $lazyTime);
}
}

0 comments on commit d6658ea

Please sign in to comment.