KeywordsHotRepository.php 989 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace App\Repositories;
  3. use App\Model\KeywordsHotModel;
  4. use Hyperf\Database\Model\Builder;
  5. use KZ2\Repositories\Model\Repository;
  6. use Psr\SimpleCache\CacheInterface;
  7. class KeywordsHotRepository extends Repository
  8. {
  9. public function model()
  10. {
  11. return KeywordsHotModel::class;
  12. }
  13. /**
  14. * 关键词热度排行10
  15. * @return mixed
  16. */
  17. public function hotTop()
  18. {
  19. $limitNum = 10;
  20. $ttl = 2 * 24 * 60 * 60;
  21. $cacheKey = 'keywords_hot_top_'. (string)$limitNum;
  22. $cacheRepo = make(CacheInterface::class);
  23. $result = $cacheRepo->remember($cacheKey, $ttl, function () use ($limitNum) {
  24. $result = [];
  25. $items = KeywordsHotModel::query()->orderBy('hot_degree', 'DESC')->take($limitNum)->get(['id', 'name', 'hot_degree']);
  26. if ($items->count()) {
  27. $result = $items->toArray();
  28. }
  29. return $result;
  30. });
  31. return $result;
  32. }
  33. }