diff --git a/app/model/Post.php b/app/model/Post.php index 0380a23..fba868d 100644 --- a/app/model/Post.php +++ b/app/model/Post.php @@ -269,24 +269,20 @@ class Post extends Base public function getHitsTitleAttr() { - // 新建对象时直接返回 0 - if (!isset($this->data['id']) || empty($this->data['id'])) { + $id = $this->getOrigin('id'); + if (empty($id)) { return 0; } - $id = $this->data['id']; - $cache_key = static::CACHE_KEY_HITS . $id; $value = Cache::get($cache_key); - if (!is_null($value)) { - return $value; + if (!is_null($value) && $value !== '') { + return (int) $value; } - // 安全获取:hits 可能尚未存在于 data 中 - $value = array_key_exists('hits', $this->data) ? $this->data['hits'] : 0; - + $value = (int) ($this->getOrigin('hits') ?: 0); Cache::set($cache_key, $value, 600); return $value; @@ -294,15 +290,14 @@ class Post extends Base public function setHitsAttr($value) { - // 新建对象(无 id)时,直接返回值,跳过缓存操作 - if (!isset($this->data['id']) || empty($this->data['id'])) { + $id = $this->getOrigin('id'); + if (empty($id)) { return $value; } - $id = $this->data['id']; - $cache_key = static::CACHE_KEY_HITS . $id; + $this->getAttr('hits_title'); Cache::inc($cache_key); return $value;