Role and Permission data are cached to speed up performance.
Automatic Cache Refresh Using Built-In Functions
When you use the built-in functions for manipulating roles and permissions, the cache is automatically reset for you, and relations are automatically reloaded for the current model record:
HOWEVER, if you manipulate permission/role data directly in the database instead of calling the supplied methods, then you will not see the changes reflected in the application unless you manually reset the cache.
Additionally, because the Role and Permission models are Eloquent models which implement the
RefreshesPermissionCache trait, creating and deleting Roles and Permissions will automatically clear the cache. If you have created your own models which do not extend the default models then you will need to implement the trait yourself.
Manual cache reset
To manually reset the cache for this package, you can run the following in your app code:
Or you can use an Artisan command:
php artisan permission:cache-reset
(This command is effectively an alias for
artisan cache:forget spatie.permission.cache but respects the package config as well.)
Cache Expiration Time
The default cache
If you wish to alter the expiration time you may do so in the
config/permission.php file, in the
The default cache key is
We recommend not changing the cache "key" name. Usually changing it is a bad idea. More likely setting the cache
prefix is better, as mentioned above.
Cache Identifier / Prefix
Laravel Tip: If you are leveraging a caching service such as
memcached and there are other sites running on your server, you could run into cache clashes between apps.
To prevent other applications from accidentally using/changing your cached data, it is prudent to set your own cache
prefix in Laravel's
/config/cache.php to something unique for each application which shares the same caching service.
Most multi-tenant "packages" take care of this for you when switching tenants.
Custom Cache Store
You can configure the package to use any of the Cache Stores you've configured in Laravel's
config/cache.php. This way you can point this package's caching to its own specified resource.
cache.store to the name of any one of the
config/cache.php stores you've defined.
'cache.store' => 'array' in
config/permission.php will effectively disable caching by this package between requests (it will only cache in-memory until the current request is completed processing, never persisting it).
Alternatively, in development mode you can bypass ALL of Laravel's caching between visits by setting
.env. You can see an example of this in the default
phpunit.xml file that comes with a new Laravel install. Of course, don't do this in production though!