[权限扩展] Entrust 缓存问题

以前给Entrust(以下称为权限管理)加上了缓存,当时没怎么细看源码,今天修改权限后发现,新改的权限不怎么好用,查看了源码发现了一些使用问题,在这里记录一下.
首先我们先看一下权限管理file
查看缓存源码

trait EntrustUserTrait
{
    //Big block of caching functionality.
    public function cachedRoles()
    {
        $userPrimaryKey = $this->primaryKey;
        $cacheKey = 'entrust_roles_for_user_'.$this->$userPrimaryKey;
        return Cache::tags(Config::get('entrust.role_user_table'))->remember($cacheKey, Config::get('cache.ttl'), function () {
                return $this->roles()->get();
        });
    }

查看去缓存源码

public function save(array $options = [])
{   //both inserts and updates
    $result = parent::save($options);
    Cache::tags(Config::get('entrust.role_user_table'))->flush();
    return $result;
}
public function delete(array $options = [])
{   //soft or hard
    $result = parent::delete($options);
    Cache::tags(Config::get('entrust.role_user_table'))->flush();
    return $result;
}
public function restore()
{   //soft delete undo's
    $result = parent::restore();
    Cache::tags(Config::get('entrust.role_user_table'))->flush();
    return $result;
}

还有trait EntrustRoleTrait 我这里就不一一列出了,总之这些不够我们用,下面我总结了什么时候移除缓存的情况,

① 用户表
当我们修改用户表中的某些字段时,需要清除缓存标签为entrust.role_user_table的标签,比如激活用户.
② 用户角色表
当我们修改用户角色表时,需要清除缓存标签为entrust.role_user_table的标签.
③ 角色表
当我们修改角色表时,需要清除缓存标签为entrust.role_user_table的标签,比如我们修改角色名称时.
④ 角色权限表
当我们修改角色权限表时,需要清除缓存标签为entrust.permission_role_table的标签.
⑤ 权限表
当我们修改权限表时,需要清除缓存标签为entrust.permission_role_table的标签,比如我们修改权限名称时.

希望以上总结能给大家带来帮助!!!   
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 4
Summer

Entrust 的缓存设计不合理,用户『更改资料』、『更新最近访问时间』这些都是常用的操作,但是却会触发到缓存更新。

合理的设计应该在用户删除或者创建的时候才更新。

6年前 评论

@Summer 还好及时发现了这个bug,也给自己提了个醒,扩展包使用时尽量提前看下源码

6年前 评论
巴啦啦

@Summer 你推荐用哪一个呢?

6年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!