【问题标题】:Laravel caching Database queriesLaravel 缓存数据库查询
【发布时间】:2019-05-12 02:55:06
【问题描述】:

我已经创建了具有用户角色和权限的项目

这是我的表格和模型

users--应用用户列表--型号名称[User],

roles--应用程序中可用的角色列表--Model Name [Role],

permissions--应用程序中可用的权限列表--型号名称[Permisions],

这是我的关系表

role_user 持有roles 表和users 表之间的关系

permission_role 持有permissions 表和roles 表之间的关系

permission_user 持有permissions 表和users 表之间的关系

我在模型中的关系代码

User.php 模型

/**
     * Many-to-Many relations with Role.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }
    /**
     * Many-to-Many relations with Permission.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function permissions()
    {
        return $this->belongsToMany(Permission::class);
    }

   public function hasPermission($permission)
    {
        return $this->hasPermissionThroughRole($permission) || (bool) $this->permissions->where('name',$permission->name)->count();
    }

    public function hasPermissionThroughRole($permission)
    {
        foreach($permission->roles as $role)
        {
            if($this->roles->contains($role))
            {
                return true;
            }
        }
        return false;
    }

    public function hasRoles($roles)
    {
        $roles = is_array($roles) ? $roles : func_get_args();
        foreach ($roles as $role) 
        {
            if ($this->hasRole($role)) 
            {
                return true;
            }
        }
        return false;
    }
    /**
     * Returns if the given user has an specific role.
     *
     * @param string $role
     *
     * @return bool
     */
    public function hasRole($role)
    {
        return $this->roles
            ->where('name', $role)
            ->first() != null;
    }

Role.php 模型

/**
     * Many-to-Many relations with Permissions.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function permissions()
    {
        return $this->belongsToMany(Permission::class);
    }
    /**
     * Many-to-Many relations with Users.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function users()
    {
        return $this->belongsToMany(User::class);
    }

Permission.php 模型

/**
     * Belongs-to-Many relations with Role.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }
    /**
     * Belongs-to-Many relations with User.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function users()
    {
        return $this->belongsToMany(User::class);
    }
    /**
     * Belongs-to-Many relations with Modules.
     *
     * @return \Illuminate\Database\Eloquent\Relations\belongsToMany
     */

最后我创建了ServiceProvider,命名为

PermissionServiceProvider

并且在 serviceprovider 的引导方法中我已经添加了代码

public function boot()
    {
        if (Schema::hasTable('permissions'))
        {         
             Permission::get()->map(function ($permission) 
             {
                 Gate::define($permission->name, function ($user) use ($permission) 
                {
                     return $user->hasPermission($permission);
                 });
             });


        }

        Blade::directive('role', function ($role)
            {
               return "<?php if(Auth::user()->hasRole({$role})): ?>";
            });
        Blade::directive('endrole', function ($role)
            {
                return "<?php endif; ?>";
            });
    }

每个功能和关系都运行良好,但每次我点击刷新按钮时查询都在运行

有没有办法缓存登录用户的所有权限和角色

编辑

根据一些建议,我尝试了 laravel 缓存包

它不适合我

【问题讨论】:

    标签: laravel-5.7


    【解决方案1】:

    您正在寻找 laravel 模型缓存包https://github.com/GeneaLabs/laravel-model-caching

    我建议使用 redis 安装软件包。在处理队列时也非常有用。

    按预期工作。我的项目截图。

    之前:

    之后:

    【讨论】:

    • 有什么方法可以创建我们自己的模型缓存
    • 是的,laravel 已经内置了工具laravel.com/docs/5.7/cache。但我认为您将花费大量时间来为所有模型方法和关系做好准备。
    • 你知道如何在没有任何包的情况下在 laravel 中制作具有用户角色和权限的项目
    • 不,我没有在 laravel 上编写自己的缓存系统。但我想您将不得不为此编写自己的查询构建器。所以,看起来不值得赌一把。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多