SystemCache.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\User;
  5. use App\Models\Order;
  6. use App\Models\Server;
  7. use App\Models\ServerLog;
  8. use App\Utils\Helper;
  9. use Illuminate\Support\Facades\Redis;
  10. class SystemCache extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'system:cache';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = '系统缓存任务';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return mixed
  37. */
  38. public function handle()
  39. {
  40. $this->setMonthIncome();
  41. $this->setMonthRegisterTotal();
  42. $this->setMonthServerTrafficTotal();
  43. }
  44. private function setMonthIncome() {
  45. Redis::set(
  46. 'month_income',
  47. Order::where('created_at', '>=', strtotime(date('Y-m-1')))
  48. ->where('created_at', '<', time())
  49. ->where('status', '3')
  50. ->sum('total_amount')
  51. );
  52. }
  53. private function setMonthRegisterTotal() {
  54. Redis::set(
  55. 'month_register_total',
  56. User::where('created_at', '>=', strtotime(date('Y-m-1')))
  57. ->where('created_at', '<', time())
  58. ->count()
  59. );
  60. }
  61. private function setMonthServerTrafficTotal () {
  62. $servers = Server::get();
  63. foreach ($servers as $item) {
  64. $serverLog = ServerLog::where('created_at', '>=', strtotime(date('Y-m-1')))
  65. ->where('created_at', '<', time())
  66. ->where('node_id', $item->id);
  67. Redis::set('month_server_traffic_total_u_' . $item->id, $serverLog->sum('u'));
  68. Redis::set('month_server_traffic_total_d_' . $item->id, $serverLog->sum('d'));
  69. }
  70. }
  71. }