Skip to content

Commit

Permalink
模型增加setCacheKey方法用于设置自动缓存key
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Nov 14, 2023
1 parent 08bcede commit c6398da
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
*/
private $lazySave = false;

/**
* 缓存自动更新标识
* @var bool|string
*/
protected $cacheKey = false;

/**
* Db对象
* @var DbManager
Expand Down Expand Up @@ -335,6 +341,18 @@ public function setSuffix(string $suffix)
return $this;
}

/**
* 设置当前查询的自动缓存标识
* @access public
* @param string|bool $key 缓存标识
* @return $this
*/
public function setCacheKey(string | bool $key)
{
$this->cacheKey = $key;
return $this;
}

/**
* 获取当前模型的数据表后缀
* @access public
Expand Down Expand Up @@ -641,7 +659,7 @@ protected function updateData(): bool

$result = $db->where($where)
->strict(false)
->cache(true)
->cache($this->cacheKey)
->setOption('key', $this->key)
->field($allowFields)
->update($data);
Expand Down Expand Up @@ -813,7 +831,7 @@ public function delete(): bool

$db->transaction(function () use ($where, $db) {
// 删除当前模型数据
$db->where($where)->delete();
$db->where($where)->cache($this->cacheKey)->delete();

// 关联删除
if (!empty($this->relationWrite)) {
Expand Down

0 comments on commit c6398da

Please sign in to comment.