内容简介:之前写过使用 Linux 的进行定时任务,实际上 laravel 也可以执行定时任务。需求是统计每日访问的 IP 数,虽然数据表中有数据,为了演示,新建监听器统计。这篇文章中介绍了实现了事件/监听器,在此基础上进行扩展。添加完成后执行
导语
之前写过使用 Linux 的进行定时任务,实际上 laravel 也可以执行定时任务。需求是统计每日访问的 IP 数,虽然数据表中有数据,为了演示,新建监听器统计。
记录 IP
这篇文章中介绍了实现了事件/监听器,在此基础上进行扩展。
-
注册一个新的监听器,在
app/Providers/EventServiceProvider.php文件中新添加CreateUserIpLog
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
'App\Events\UserBrowse' => [
'App\Listeners\CreateBrowseLog',// 用户访问记录
'App\Listeners\CreateUserIpLog',// 用户 IP 记录
],
];
添加完成后执行 php artisan event:generate
,创建好了 app/Listeners/CreateUserIpLog.php
文件;
- 在新建监听器中,记录用户的 IP,使用 Redis 的 Set 数据类型进行记录,代码如下
/**
* Handle the event.
* 记录用户 IP
* @param UserBrowse $event
* @return void
*/
public function handle(UserBrowse $event)
{
$redis = Redis::connection('cache');
$redisKey = 'user_ip:' . Carbon::today()->format('Y-m-d');
$isExists = $redis->exists($redisKey);
$redis->sadd($redisKey, $event->ip_addr);
if (!$isExists) {
// key 不存在,说明是当天第一次存储,设置过期时间三天
$redis->expire($redisKey, 259200);
}
}
统计访问
上面将用户的 IP 记录下来,然后就是编写统计代码
-
新建一个任务
php artisan make:command CountIpDay,新建了app/Console/Commands/CountIpDay.php文件; -
设置签名
protected $signature = 'countIp:day';和描述protected $description = '统计每日访问 IP'; -
在
handle方法中编写代码,也可以在kernel.php中使用emailOutputTo方法发送邮件
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$redis = Redis::connection('cache');
$yesterday = Carbon::yesterday()->format('Y-m-d');
$redisKey = 'user_ip:' . $yesterday;
$data = $yesterday . ' 访问 IP 总数为 ' . $redis->scard($redisKey);
// 发送邮件
Mail::to(env('ADMIN_EMAIL'))->send(new SendSystemInfo($data));
}
设置任务调度
-
编辑
app/Console/Kernel.php的$commands
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
\App\Console\Commands\CountIpDay::class,
];
-
在
schedule方法中设置定时任务,执行时间为每天凌晨一点
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('countIp:day')->dailyAt('1:00');
}
-
最后是在 Linux 中添加定时任务,每分钟执行一次
artisan schedule:run,如下* * * * * /you_php you_path/artisan schedule:run >> /dev/null 2>&1
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 理解golang调度之一 :操作系统调度
- 理解golang调度之二 :Go调度器
- Golang 源码学习调度逻辑(三):工作线程的执行流程与调度循环
- Node.js CPU调度优化(多服务器多核心分配调度)
- Hadoop 容器调度器与公平调度器原理和实践深入剖析-Hadoop商业环境实战
- golang调度器
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Data Structures and Algorithms in Java
Robert Lafore / Sams / 2002-11-06 / USD 64.99
Data Structures and Algorithms in Java, Second Edition is designed to be easy to read and understand although the topic itself is complicated. Algorithms are the procedures that software programs use......一起来看看 《Data Structures and Algorithms in Java》 这本书的介绍吧!