belongsTo(User::class, 'user_id', 'id'); } public function topic() { return $this->HasOne(MomentTopic::class, 'id', 'topic_id'); } public function unlinkLogs() { return $this->hasMany(UnlikeMomentLog::class)->where('user_id', auth()->id()); } public function unlikeMoments() { return $this->hasMany(UnlikeMomentLog::class); } public function vote() { return $this->belongsTo(Vote::class); } public static function cacheMoemntKey($moment_id) { return self::RDMOMENTKEY . $moment_id; } //缓存动态信息 public function cacheMoment() { try { $rd_moment_key = self::cacheMoemntKey($this->id); $result = Cache::put($rd_moment_key, $this, now()->addHours(24)); return $result; } catch (\Exception $e) { $this->getError($e); return false; } } public function getCacheMoment() { try { $rd_moment_key = self::cacheMoemntKey($this->id); $moment = Cache::get($rd_moment_key); return $moment; } catch (\Exception $e) { $this->getError($e); return false; } } //修改缓存点赞信息 public function updateCacheMomentLikeNum($type = 'ADD') { try { $moment = $this->getCacheMoment(); if ($moment) { if ($type == 'ADD') { $moment->momentLikerCount = $moment->momentLikerCount + 1; } else { $moment->momentLikerCount = $moment->momentLikerCount - 1; } } $this->cacheMoment(); return true; } catch (\Exception $e) { $this->getError($e); return false; } } //修改缓存点赞信息 public function updateCacheMomentLikeNumV2($momentLikerCount) { try { $moment = $this->getCacheMoment(); if ($moment && $moment->momentLikerCount != $momentLikerCount) { $moment->momentLikerCount = $momentLikerCount; $moment->cacheMoment(); } return true; } catch (\Exception $e) { $this->getError($e); return false; } } //修改缓存评论信息 public function updateCacheMomentCommentNum($type = 'ADD') { try { $moment = $this->getCacheMoment(); if (!$moment) { $moment = $this; } if ($type == 'ADD') { $moment->momentCommentCount = $moment->momentCommentCount + 1; } else { $moment->momentCommentCount = $moment->momentCommentCount - 1; } $moment->cacheMoment(); return true; } catch (\Exception $e) { $this->getError($e); return false; } } /** public function getPhotosAttribute(){ } * **/ }