LiveStreamService.php 1012 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\Cache;
  4. use Illuminate\Support\Facades\Redis;
  5. class LiveStreamService
  6. {
  7. const CACHE_KEY = 'pf:services:livestream:';
  8. public static function getComments($id, $start = 0, $stop = 14)
  9. {
  10. $key = self::CACHE_KEY . 'chat:' . $id;
  11. return Redis::lrange($key, $start, $stop);
  12. }
  13. public static function addComment($id, $val)
  14. {
  15. $key = self::CACHE_KEY . 'chat:' . $id;
  16. if(config('database.redis.client') === 'phpredis') {
  17. if(self::commentsCount($id) >= config('livestreaming.comments.max_falloff')) {
  18. Redis::rpop($key);
  19. }
  20. }
  21. return Redis::lpush($key, $val);
  22. }
  23. public static function commentsCount($id)
  24. {
  25. $key = self::CACHE_KEY . 'chat:' . $id;
  26. return Redis::llen($key);
  27. }
  28. public static function deleteComment($id, $val)
  29. {
  30. $key = self::CACHE_KEY . 'chat:' . $id;
  31. return Redis::lrem($key, 0, $val);
  32. }
  33. public static function clearChat($id)
  34. {
  35. $key = self::CACHE_KEY . 'chat:' . $id;
  36. return Redis::del($key);
  37. }
  38. }