Laravel-停止并发访问记录 [英] Laravel - Stop concurrent access to record

查看:138
本文介绍了Laravel-停止并发访问记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel中,有任何方法可以停止同时与同一记录进行交互.例如,如果用户A正在编辑记录,那么同时我需要停止用户B编辑同一记录.

In Laravel is there any way to stop interacting with the same record simultaneously. For example, if User A is editing a record then at the same time I need to stop User B from editing same record.

注意:我正在使用SESSION_DRIVER =文件& Laravel 5.2

当前大约有500多个用户在使用该系统,这是一种管理"面板,管理员可以在其中与记录进行交互,但是我需要停止并发访问.什么是最佳解决方案.

There are around 500+ users using the system currently and it is kind of Admin panel where admins interact with the records but I need to stop concurrent access. what would be the optimum solution.

推荐答案

这是我能够实现此目标的方法.

Here is how I am able to achieve this.

1)创建一个迁移以在模型is_editingediting_byupdated_at中添加2列 (无需创建updated_at(如果已经存在的话))

1) Create a migration to add 2 columns in the model is_editing and editing_by and updated_at (no need to created updated_at if already there)

2)制作中间件php artisan make:middleware StopConcurrentOperations.

3)在$routeMiddleware下的app/Http/Kernel.php中注册中间件.

3) Register the middleware in app/Http/Kernel.php under $routeMiddleware.

4)将中间件应用于控制器

4) Apply middleware to the controller

$this->middleware('concurrent.operations',["only"=>["edit","update"]]);

5)阻止中间件中的并发编辑

5) Block concurrent edits in middleware

<?php

namespace App\Http\Middleware;

use App\OperationActivity;
use Carbon\Carbon;
use Closure;
use Illuminate\Support\Facades\Auth;

class StopConcurrentOperations
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        $operation_id = current($request->route()->parameters());
        $user_id = Auth::guard('admin')->user()->id;
        if(Auth::guard('admin')->user() && !empty($operation_id)){
            $activity = OperationActivity::firstOrNew(["operation_id" => $operation_id]);
            if($activity->is_editing && $activity->updated_at->diffInMinutes(Carbon::now())<5 && $activity->editing_by!=$user_id)
                abort(409);
            else {
                $activity->is_editing = true;
                $activity->editing_by = $user_id;
                $activity->save();
            }
        }
        return $next($request);
    }
}

在这里,我正在检查是否有人已经在编辑记录,并正在检查执行编辑的时间.

here I am checking if someone already editing the record and checking the time when was editing performed.

6)(可选)在客户端,如果编辑用户关闭窗口,则可以取消阻止记录,例如

6) Optionally on the client side if the editing user closes the windows then to unblock the record like

  window.onbeforeunload = function() {
    var url = "{{route('unlock_route_name',$operation->id)}}";
    $.ajax({ url: url, method: 'post' })
  };

要从客户端完成此过程,您必须在控制器中创建路由方法,然后像

to complete the process from the client side you have to create a route and a method in the controller and just unlock the record like

      $operation = Operation::findOrFail($id);
      $activity = $operation->activities()->where("is_editing",true)->first();
      $activity->is_editing = false;
      $activity->save();

注意:在我的示例中,我有不同的表来记录活动OperationActivity,并且可能模型名称为Operation

Note: In my example, I have different table to log activites alerady OperationActivity and likely Model name is Operation

这篇关于Laravel-停止并发访问记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆