写一个程序结合laravel和redis统计访问链接时不同User-Agent的访问次数和访问用户数,要求按天,按周,按月统计
时间: 2024-02-11 08:07:01 浏览: 101
Java实现:月,日,年,周,访问量统计
5星 · 资源好评率100%
好的,根据你的需求,这里给出一个稍微复杂一些的示例程序:
首先,同样需要安装 Redis 扩展和配置 Redis。
然后,在 Laravel 中创建一个服务提供者,用于注册 Redis 相关的统计类:
```
php artisan make:provider RedisStatsServiceProvider
```
在 `app/Providers/RedisStatsServiceProvider.php` 文件中编写代码:
```php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Redis;
class RedisStatsServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->app->singleton(DayStats::class, function () {
return new DayStats(Redis::connection());
});
$this->app->singleton(WeekStats::class, function () {
return new WeekStats(Redis::connection());
});
$this->app->singleton(MonthStats::class, function () {
return new MonthStats(Redis::connection());
});
}
}
```
这里使用了 Laravel 的服务容器,注册了三个 Redis 统计类 `DayStats`、`WeekStats` 和 `MonthStats`,并将它们绑定到容器中。
接下来,定义三个统计类,分别对应按天、按周和按月统计:
在 `app/Stats/DayStats.php` 文件中编写代码:
```php
<?php
namespace App\Stats;
use Illuminate\Redis\Connections\Connection;
class DayStats
{
protected $redis;
public function __construct(Connection $redis)
{
$this->redis = $redis;
}
public function count($userAgent)
{
$key = 'user_agents_counts:day:' . date('Ymd');
return $this->redis->hget($key, $userAgent) ?: 0;
}
public function users($userAgent)
{
$key = 'user_agents_ips:day:' . date('Ymd') . ':' . $userAgent;
return $this->redis->scard($key);
}
}
```
在 `app/Stats/WeekStats.php` 文件中编写代码:
```php
<?php
namespace App\Stats;
use Illuminate\Redis\Connections\Connection;
class WeekStats
{
protected $redis;
public function __construct(Connection $redis)
{
$this->redis = $redis;
}
public function count($userAgent)
{
$keys = $this->getKeys();
return $this->redis->hget($keys, $userAgent) ?: 0;
}
public function users($userAgent)
{
$keys = $this->getKeys();
$members = $this->redis->sunion($keys);
return count($members);
}
protected function getKeys()
{
$keys = [];
for ($i = 0; $i < 7; $i++) {
$key = 'user_agents_ips:day:' . date('Ymd', strtotime("-$i day")) . ':' . $userAgent;
$keys[] = $key;
}
return $keys;
}
}
```
在 `app/Stats/MonthStats.php` 文件中编写代码:
```php
<?php
namespace App\Stats;
use Illuminate\Redis\Connections\Connection;
class MonthStats
{
protected $redis;
public function __construct(Connection $redis)
{
$this->redis = $redis;
}
public function count($userAgent)
{
$keys = $this->getKeys();
return $this->redis->hget($keys, $userAgent) ?: 0;
}
public function users($userAgent)
{
$keys = $this->getKeys();
$members = $this->redis->sunion($keys);
return count($members);
}
protected function getKeys()
{
$keys = [];
$start = strtotime(date('Y-m-01'));
$end = strtotime(date('Y-m-t'));
for ($i = $start; $i <= $end; $i += 86400) {
$key = 'user_agents_ips:day:' . date('Ymd', $i) . ':' . $userAgent;
$keys[] = $key;
}
return $keys;
}
}
```
这里三个统计类的代码逻辑很类似,只是计算时间范围不同,可以根据需要自行调整。
最后,在需要统计的路由中使用这些统计类:
```php
use App\Stats\DayStats;
use App\Stats\WeekStats;
use App\Stats\MonthStats;
Route::get('/test', function () {
$dayStats = app(DayStats::class);
$weekStats = app(WeekStats::class);
$monthStats = app(MonthStats::class);
$userAgent = request()->header('User-Agent');
$dayCount = $dayStats->count($userAgent);
$dayUsers = $dayStats->users($userAgent);
$weekCount = $weekStats->count($userAgent);
$weekUsers = $weekStats->users($userAgent);
$monthCount = $monthStats->count($userAgent);
$monthUsers = $monthStats->users($userAgent);
return [
'day_count' => $dayCount,
'day_users' => $dayUsers,
'week_count' => $weekCount,
'week_users' => $weekUsers,
'month_count' => $monthCount,
'month_users' => $monthUsers,
];
})->middleware('track_user_agent');
```
这里通过 Laravel 的依赖注入功能,获取了之前注册的三个统计类的实例,并调用了它们的方法来获取不同时间范围内的统计结果。
这样,就可以按天、按周和按月统计不同 User-Agent 的访问次数和访问用户数了。
阅读全文