AddHyperLogLogJob.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://doc.hyperf.io
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
  10. */
  11. namespace App\Jobs;
  12. use App\Repositories\UserRepository;
  13. use Carbon\Carbon;
  14. use Hyperf\AsyncQueue\Job;
  15. use Hyperf\Redis\Redis;
  16. class AddHyperLogLogJob extends Job
  17. {
  18. public $params;
  19. public function __construct($params)
  20. {
  21. // 这里最好是普通数据,不要使用携带 IO 的对象,比如 PDO 对象
  22. $this->params = $params;
  23. }
  24. public function handle()
  25. {
  26. try {
  27. $cachePrefix = env('SERVER_CACHE_PREFIX', 'c:');
  28. $redis = make(Redis::class);
  29. $prfix = $cachePrefix .'active:' . Carbon::now()->toDateString();
  30. //日活统计key
  31. $device = $prfix . ':' . $this->params['device_type'];
  32. //今天的单设备
  33. $redis->pfadd($device, array($this->params['user_id']));
  34. //用户是否为会员
  35. $info = make(UserRepository::class)
  36. ->getUserInfoFromCache($this->params['user_id']);
  37. $redis->pfadd(
  38. $prfix . ':' . $info['is_vip'],
  39. array($this->params['user_id']
  40. )
  41. );
  42. //今天的总设备
  43. $redis->pfadd($prfix, array($this->params['user_id']));
  44. } catch (\Exception $exception) {
  45. logger('AddHyperLogLogJob', [$exception->getMessage()]);
  46. }
  47. }
  48. }