SuggestionService.php 1011 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\Redis;
  4. use App\Profile;
  5. class SuggestionService {
  6. const CACHE_KEY = 'pf:services:suggestion:ids';
  7. public static function get($start = 0, $stop = -1)
  8. {
  9. return Redis::zrange(self::CACHE_KEY, $start, $stop);
  10. }
  11. public static function set($val)
  12. {
  13. return Redis::zadd(self::CACHE_KEY, 1, $val);
  14. }
  15. public static function del($val)
  16. {
  17. return Redis::zrem(self::CACHE_KEY, $val);
  18. }
  19. public static function add($val)
  20. {
  21. return self::set($val);
  22. }
  23. public static function rem($val)
  24. {
  25. return self::del($val);
  26. }
  27. public static function count()
  28. {
  29. return Redis::zcount(self::CACHE_KEY, '-inf', '+inf');
  30. }
  31. public static function warmCache($force = false)
  32. {
  33. if(self::count() == 0 || $force == true) {
  34. $ids = Profile::whereNull('domain')
  35. ->whereIsSuggestable(true)
  36. ->whereIsPrivate(false)
  37. ->whereHas('statuses')
  38. ->pluck('id');
  39. foreach($ids as $id) {
  40. self::set($id);
  41. }
  42. return 1;
  43. }
  44. return 0;
  45. }
  46. }