OnlineLimitService.php 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: gevnc
  5. * Date: 2018/7/3
  6. * Time: 11:23
  7. */
  8. namespace App\Services;
  9. use App\Models\OnlineLimits;
  10. use App\Traits\Singleton;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Redis;
  13. class OnlineLimitService
  14. {
  15. use Singleton;
  16. const REDIS_ONLINE = "bs_online";
  17. const REDIS_ONLINE_LIMIT = "bs_online_limit";
  18. const REDIS_APPS = "agh_apps";
  19. /**
  20. * 判断是否超过在线人数
  21. */
  22. public function isOverOnline($activityId){
  23. $onlineLimitNumber = 0;
  24. if(!Redis::exists(self::REDIS_ONLINE_LIMIT.":".$activityId)){
  25. $brainstorming = BrainstormingService::getInstance()->get($activityId);
  26. if(isset($brainstorming["activity_type"])){
  27. $freeNum = $this->getFreeNum();
  28. if($brainstorming["activity_type"]==1){
  29. $onlineLimitNumber = $freeNum;
  30. }
  31. }
  32. Redis::setex(self::REDIS_ONLINE_LIMIT.":".$activityId,300,$onlineLimitNumber);
  33. }else{
  34. $onlineLimitNumber = Redis::get(self::REDIS_ONLINE_LIMIT.":".$activityId);
  35. }
  36. if($onlineLimitNumber>0){
  37. $onlineCount = Redis::get(self::REDIS_ONLINE.":".$activityId);
  38. if($onlineCount>=$onlineLimitNumber){
  39. return true;
  40. }
  41. }
  42. return false;
  43. }
  44. public function onlineLimitCount($activityId){
  45. return Redis::get(self::REDIS_ONLINE_LIMIT.":".$activityId);
  46. }
  47. public function getFreeNum(){
  48. $freeNum = 0;
  49. if(!Redis::exists(self::REDIS_APPS.":brainstorming")){
  50. $app = DB::table("agh_apps")->where("app_code","brainstorming")->first();
  51. if($app){
  52. $freeNum = $app->free_num;
  53. Redis::setex(self::REDIS_APPS.":brainstorming",300,json_encode($app,256));
  54. }
  55. }
  56. return $freeNum;
  57. }
  58. }