Cache and events | laravel-permission-rust | Spatie       

 SPATIE  

laravel-permission-rust
=======================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-permission-rust](https://spatie.be/docs/laravel-permission-rust/v1)  Cache and events

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-permission-rust/v1) 

  Cache and events    
- [ Introduction ](https://spatie.be/docs/laravel-permission-rust/v1/introduction)
- [ Install and connect the application ](https://spatie.be/docs/laravel-permission-rust/v1/installation-laravel)
- [ Roles and permissions ](https://spatie.be/docs/laravel-permission-rust/v1/basic-usage)
- [ Authenticated routes and policies ](https://spatie.be/docs/laravel-permission-rust/v1/authorization)
- [ Querying users and bulk assignment ](https://spatie.be/docs/laravel-permission-rust/v1/querying-users)
- [ Console commands ](https://spatie.be/docs/laravel-permission-rust/v1/commands)
- [ Custom models and IDs ](https://spatie.be/docs/laravel-permission-rust/v1/models-and-ids)
- [ Cache and events ](https://spatie.be/docs/laravel-permission-rust/v1/cache-and-events)
- [ Integration tests ](https://spatie.be/docs/laravel-permission-rust/v1/testing)

 Cache and events
================

Cache and events
========================================================================================================

Authorization queries the real database on each check. Native ORM revocation or rename takes effect on the next check, even if a registrar or session has been kept alive. There are no cached user authorization decisions.

`cached_permission_names(guard).await?` is an administrative listing stored in Laravel's selected cache store. Configure `cache_store`, `cache_key` and `cache_expiration` on `PermissionConfig`. `None` or `Some("default")` uses Laravel's default store; a name selects `cache::store(name)`. An unknown store errors rather than silently falling back. The cache key must be unique per database/application when the store is shared. Creating/deleting a permission through the registrar resets the listing after the surrounding transaction commits. Direct ORM/SQL edits require `forget_cached_permissions().await?` or waiting for TTL. Inside a transaction, the listing can remain stale until commit. Cache misses are stored only after commit, so rolled-back names cannot populate the shared cache. Do not use that listing to make authorization decisions. Cache backend errors propagate. A post-commit reset failure does not undo committed database writes.

Set `events_enabled: true` to dispatch events through Laravel's real event dispatcher:

- `RoleAttached` and `RoleDetached` for user role changes.
- `PermissionAttached` and `PermissionDetached` for direct user grants and role permissions.
- `AssignmentChanged` for a combined delta, retaining the existing API.

The four specific events contain `subject` and typed `ids`. For a role's permission changes, the subject uses the selected role model's morph alias and key. Generic event keys follow the selected model, including UUIDs and ULIDs.

Register a typed listener with `.with_events(|events, _| { events.listen(listener); })`. `AssignmentChanged` contains `subject`, `relation` (`role_id` or `permission_id`), `attached` and `detached`. Repeating an assignment emits no change. Native transaction rollback discards listeners; successful outer commit runs them. Listener failures after commit do not roll back the grant.

These events cover package user, role-permission and bulk assignment helpers. Direct SQL, native relation attach/detach and cascade deletes do not emit them. Model creation/update/deletion use Laravel's native ORM lifecycle events.

The provider registers an at-dispatch listener for native `Deleted<Account>` events. It removes the selected account model's assignments before dispatch returns, inside the deletion transaction. For other subject types, query deletes, or muted model events, call `registrar.delete_model_assignments(morph, key).await?` in the same transaction or native model hook. Polymorphic pivots cannot reference every application table with a database foreign key. Role/Permission deletion cascades through real foreign keys.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
