TopicUseCount.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Topic;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\Log;
  6. use Illuminate\Support\Facades\Redis;
  7. class TopicUseCount extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'post:create_bean';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '更新话题使用数';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct(Topic $topic)
  27. {
  28. parent::__construct();
  29. $this->topic = $topic;
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. $this->line("开始更新话题使用数");
  39. $key = 'topic.just_use_count';
  40. $ids = Redis::ZRANGE($key, 0 , -1, 'WITHSCORES');
  41. foreach($ids as $topicId => $val){
  42. try{
  43. $value = Redis::ZSCORE($key, $topicId);
  44. Redis::ZREM ($key, $topicId);
  45. $this->topic->where('post_id', $topicId)->increment('use_bean', $value);
  46. Log::debug('更新更新话题使用数成功'.$topicId.'-'.$value);
  47. }catch (\Exception $exception){
  48. Log::error('更新更新话题使用数失败'.$topicId.'-'.$val.$exception->getMessage());
  49. }
  50. usleep(10000);
  51. }
  52. $this->line("更新内容生成U米结束");
  53. }
  54. }