RelationRepository.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Repositories;
  3. use App\Model\Mongo\UserRelationModel;
  4. use App\Model\Mongo\VideoLikeModel;
  5. use Psr\SimpleCache\CacheInterface;
  6. class RelationRepository
  7. {
  8. /**
  9. * A用户是否关注了B用户(缓存).
  10. * @param string $sourceUid 来源用户UID
  11. * @param string $targetUid 关注目标UID
  12. * @param bool $cache 是否走缓存.
  13. * @return bool
  14. */
  15. public function userIsFocusOtherUser($sourceUid, $targetUid, $cache = true)
  16. {
  17. $sourceUid = strval($sourceUid);
  18. $targetUid = strval($targetUid);
  19. if (empty($sourceUid)) {
  20. // 没有登陆的用户,直接返回false
  21. return false;
  22. }
  23. if ($sourceUid === $targetUid) {
  24. // 自己=自己,就给关注了.
  25. return true;
  26. }
  27. $cacheKey = 'user_favorite:'. (string)$sourceUid . '_' . (string)$targetUid;
  28. if (!$cache) {
  29. $isFocus = $this->userIsFocusOtherUserWithoutCache($sourceUid, $targetUid);
  30. } else {
  31. $isFocus = make(CacheInterface::class)->rememberForever($cacheKey, function () use ($sourceUid, $targetUid) {
  32. return $this->userIsFocusOtherUserWithoutCache($sourceUid, $targetUid);
  33. });
  34. }
  35. return $isFocus;
  36. }
  37. /**
  38. * A用户是否关注了B用户(没缓存).
  39. * @param $sourceUid
  40. * @param $targetUid
  41. * @return bool
  42. */
  43. public function userIsFocusOtherUserWithoutCache($sourceUid, $targetUid)
  44. {
  45. $isFocus = false; // 默认未关注.
  46. $mongoKeyId = md5('favorite_' . (string)$sourceUid . '_' . (string)$targetUid);
  47. $favoriteData = [
  48. '_id' => $mongoKeyId,
  49. ];
  50. $count = make(UserRelationModel::class)->db()
  51. ->countDocuments($favoriteData);
  52. $isFocus = boolval($count);
  53. return $isFocus;
  54. }
  55. /**
  56. * 用户是否点赞过某一个视频(缓存).
  57. * @param $userCode
  58. * @param $videoId
  59. * @param bool $cache
  60. * @return bool
  61. */
  62. public function userIsLikeVideo($userCode, $videoId, $cache = true)
  63. {
  64. $cacheKey = 'video_like:'. (string)$userCode . '_' . (string)$videoId;
  65. if (!$cache) {
  66. $isLike = $this->userIsLikeVideoWithoutCache($userCode, $videoId);
  67. } else {
  68. $isLike = make(CacheInterface::class)->rememberForever($cacheKey, function () use ($userCode, $videoId) {
  69. return $this->userIsLikeVideoWithoutCache($userCode, $videoId);
  70. });
  71. }
  72. return $isLike;
  73. }
  74. /**
  75. * 用户是否点赞过某一个视频(没缓存).
  76. * @param $userCode
  77. * @param $videoId
  78. * @return bool
  79. */
  80. public function userIsLikeVideoWithoutCache($userCode, $videoId)
  81. {
  82. $isLike = false; // 默认未点赞.
  83. $mongoKeyId = substr(md5($userCode . '_' . strval($videoId)), 8, 16);
  84. $data = [
  85. '_id' => $mongoKeyId,
  86. ];
  87. $count = make(VideoLikeModel::class)->db()
  88. ->countDocuments($data);
  89. $isLike = boolval($count);
  90. return $isLike;
  91. }
  92. }