Laravel-permission: Add option to disable caching

Created on 1 Aug 2016  路  5Comments  路  Source: spatie/laravel-permission

It would be great if there was an option to disable permission caching in config file.

What do you guys think?

Most helpful comment

I got a good argument - Testing.

I need to clear the cache before each test. Woot be nice just to disable it.

All 5 comments

Why would you want to disable the caching?

Correct me if i'm wrong, but the cache gets reset every time you create/update/delete some model.

For example, I have "last_seen" date for User, every time user loads a page, the "last_seen" date gets updated and the cache gets reset, so for me there is no point in caching and I would disable it.

But it would be cool to reset cache only when changes are made to roles and permissions tables, if this is a viable option.

You're certainly right, the cache does get deleted whenever the user gets updated.

Unless you have some benchmarks that prove otherwise, I don't think the extra overhead of the deletion of the cache has a big hit on the performance of the package. So I'd like to keep it simple and just leave the code as it is.

I got a good argument - Testing.

I need to clear the cache before each test. Woot be nice just to disable it.

Here is my solution to this and how I went about it:

```trait RefreshesPermissionCache
{
public static function bootRefreshesPermissionCache()
{
static::saved(function () {
app(PermissionRegistrar::class)->forgetCachedPermissions();
});

    static::deleted(function () {
        app(PermissionRegistrar::class)->forgetCachedPermissions();
    });
}

}

Spatie Runs this every single time a new permission or role is saved in the roles and permissions model, that way it can refresh the cache. 

My work around it was to create a manual way to clear cache each time I need to,

public function cache(Request $request)
{

    if($request->method("get") == "GET")
    {
        return view("roles.cache");
    }

    app(PermissionRegistrar::class)->forgetCachedPermissions();
    session()->flash("alert-success", "Cache cleared successfully.");
    return back();
}
alternatively you could just set the cache in the permissions config file to last for a little, time like a couple of minutes 

expiration_time' => \DateInterval::createFromDateString('5 minutes'),

Change the cache driver to array, that way it would only persist per request. Which is same as disabling the cache.

'store' => 'array',
```

Was this page helpful?
0 / 5 - 0 ratings

Related issues

holymp2006 picture holymp2006  路  4Comments

neoreids picture neoreids  路  3Comments

notflip picture notflip  路  3Comments

enghelewa picture enghelewa  路  4Comments

bbdangar picture bbdangar  路  4Comments