php - 从 Laravel 框架创建一个在线用户(问题)

标签 php mysql laravel frameworks

我不确定我是否已经完成了我的想法,我想使用Laravel框架在线创建一个用户页面,我想知道我用来将用户引入数据库的方法是否正确,因此在if之后删除时间超过限制。

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;

class Automatism {

    public function handle($request, Closure $next) {
        DB::table('users_online')->updateOrInsert(
          ['user-ip' => \Request::getClientIp(true)],
          [
            'user-viewing-url' => Route::getFacadeRoot()->current()->uri(),
            'user-ip' => \Request::getClientIp(true),
            'timestamps' => now()
          ]
        );

        DB::table('users_online')->where('timestamps', '<', 'now() - INTERVAL 5 MINUTE')->delete();
        return $next($request);
    }
}

然后,在这段代码旁边,我想对 24 小时内访问过该网站的用户执行相同的操作。

最佳答案

我曾经制作过一个中间件来跟踪用户事件,这也许可以给你一些想法:

public function handle($request, Closure $next)
    {
        $this->request = $request;
        if(Auth::check()) {
            Cache::put('user-is-online-' . Auth::user()->id, true, Carbon::now()->addMinutes(5));
            $this->storeDailyUserActivity();
        }

        return $next($request);
    }

    public function storeDailyUserActivity(){
        $daily_active_user = [];
        if(Cache::has('daily_active_user')){
            $daily_active_user = Cache::get('daily_active_user');
            if(!is_array($daily_active_user)){
                Cache::forget('daily_active_user');
                $this->storeDailyUserActivity();
                return;
            }
            $daily_active_user['cache_old_date'] = $daily_active_user['cache_date'] ?? 'err_no_cache_date'; //debug only
        }
        $daily_active_user[Auth::user()->id] = Carbon::now();
        $daily_active_user['cache_date'] = Carbon::now()->toTimeString() . ' - ' . $this->request->url(); //debug only
        Cache::forever('daily_active_user', $daily_active_user);
    }
  • 它使用缓存来防止在每个请求上执行数据库查询(如果您确实需要将其保存到数据库中,则需要进行调整)。注意:您需要一个支持缓存键的缓存驱动程序(查看 Laravel 文档)。
  • 它为每个用户存储一个唯一的缓存 key ,因此稍后在您的代码中,您可以检查特定用户的同一 key (例如:该用户在过去 5 分钟内是否处于事件状态)
  • 它还将所有用户的上次事件日期存储在数组中。因此,您可以循环播放,查找用户以及他们上次事件的时间(例如:显示过去 2 小时内事件的所有用户)。
  • 这是一个旧的片段,可以改进。然后可以轻松创建检查这些缓存值的帮助程序(例如:isUserActiveInLastMinutes($minutes = 5))。

如果您确实需要将其保存到数据库中,我会这样做:

  • 添加用户的 last_activity 时间戳列(或者用户表和 user_activity 表之间的一对一关系)
  • 根据每个用户请求更新此last_activity(我建议至少使用 5 分钟的缓存,以防止在每个用户请求上添加数据库查询)
  • 然后您可以 Eloquent 地查询“last_activity > Carbon::now()->subMinutes(5) 的所有用户”

关于php - 从 Laravel 框架创建一个在线用户(问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54919079/

相关文章:

laravel - 重写 Laravel get 和 First 方法

php - Laravel View 未发现异常

php - 未捕获的异常 'PDOException with message ' SQLSTATE[HY000] [2013]

mysql - 无法通过 Workbench 或 MS Access 更新 MySQL View 中的数据

mysql - 选择当前用户拥有的所有项目,如果不是,则显示较少的项目

php - Laravel Blade View 引擎的 @yield inside html 标签

php - 使用 HTML 表单作为方案动态查询数据库

php - 使用 MySQL 和 PHP 创建 "Secret Santa"生成器

php - 我将我的网站上传到托管商,但我遇到了以下问题

php - Laravel Mail 未将变量传递给 Blade 模板