[Docs] Add instructions to reinitialize cache for multi-tenancy key settings #2804
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 asPermissionRegistrar
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 theCacheStore->getPrefix()
and it was incorrect during Tenant switching. That meant theCacheManager
was already resolved and was using the old keys in it's store. So I reinitialized theCacheStore
in thePermissionRegistrar
by callinginitializeCache
and it resulted in the correct store during TenantSwitching.Please correct me if I am wrong. I hope this helps someone else as well.