Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docs] Add instructions to reinitialize cache for multi-tenancy key settings #2804

Merged
merged 3 commits into from
Feb 5, 2025

Conversation

sudkumar
Copy link
Contributor

@sudkumar sudkumar commented Feb 5, 2025

When switching cache prefix/key/store during a request lifecycle, after the service provider has been loaded, the cache store should be reinitialized to make sure that the updated CacheStore with latest prefix/keys is used for upcoming cache hits.

app()->make(\Spatie\Permission\PermissionRegistrar::class)->initializeCache();

Context

I have recently integrated laravel-multitenancy in a project to use the per-tenant cache.prefix. I am using single-db multi-tenancy and was setting current Tenant during a request cycle using the Authentication events. This was working perfectly as PermissionRegistrar is a singleton and it was resolved with correct prefix when resolved from the service container. But in some of my flow, I needed to update the permissions for multiple tenants during a single request life cycle. I found out that my database was in correct state with respect to the latest permissions but the permissions cache was not.

After a day or two debugging, I found out that the cache prefix during the loadPermissions was incorrect when updating the permissions of multiple tenants during a single request. Then I logged the CacheStore->getPrefix() and it was incorrect during Tenant switching. That meant the CacheManager was already resolved and was using the old keys in it's store. So I reinitialized the CacheStore in the PermissionRegistrar by calling initializeCache and it resulted in the correct store during TenantSwitching.

Please correct me if I am wrong. I hope this helps someone else as well.

sudkumar and others added 3 commits February 5, 2025 12:57
when switching cache prefix/key/store during a request lifecycle, after the service provider has been loaded, the cache store should be reinitialized to make sure that the updated CacheStore is used for upcoming cache hits.
@drbyte drbyte changed the title Add instructions to reinitialize cache for multi-tenancy key settings [Docs] Add instructions to reinitialize cache for multi-tenancy key settings Feb 5, 2025
@drbyte
Copy link
Collaborator

drbyte commented Feb 5, 2025

Thanks

@drbyte drbyte merged commit bb3ad22 into spatie:main Feb 5, 2025
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants