-
-
Notifications
You must be signed in to change notification settings - Fork 487
[4.x] Make globalCache always use the central connection with database cache store
#1462
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
Open
lukinovec
wants to merge
8
commits into
master
Choose a base branch
from
fix-cache-invalidation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+62
−6
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0cf7043
Add datasets to globalCache/invalidation tests as regression
lukinovec 5e65c67
Make globalCache always use the central connection
lukinovec fd1aa24
Merge branch 'master' into fix-cache-invalidation
lukinovec 20036ef
Clean up $adjustCacheManagerUsing in before and afterEach
lukinovec 566f504
Improve comments
lukinovec 5e708bb
remove seemingly unnecessary static prop resets from tests
stancl c6ba6a5
Merge branch 'master' into fix-cache-invalidation
stancl 602a151
Expand CacheTenancyBootstrapper docblock
lukinovec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid materializing every database cache store in
globalCache.This loop resolves
$manager->store($name)for every configured database cache store each timeglobalCacheis built. That turns unused database stores into hard dependencies ofglobal_cache()and adds connection/setup cost even when callers only use a different store. Keep the central-connection fix lazy per requested store instead of eagerly instantiating the fullcache.storesset.🧰 Tools
🪛 PHPMD (2.15.0)
[warning] 128-128: Avoid excessively long variable names like $notAValidDatabaseStore. Keep variable name length under 20. (undefined)
(LongVariable)
[error] 139-139: Avoid using static access to class '\Illuminate\Support\Facades\DB' in method 'makeDatabaseCacheStoresCentral'. (undefined)
(StaticAccess)
[error] 140-140: Avoid using static access to class '\Illuminate\Support\Facades\DB' in method 'makeDatabaseCacheStoresCentral'. (undefined)
(StaticAccess)
🤖 Prompt for AI Agents
✅ Confirmed as addressed by @lukinovec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this logic should mimic DatabaseCacheBootstrapper's $adjustGlobalCacheManager in a way. That also modifies all "scoped stores" (see the scopedStoreNames method).
With that in mind, is the thing you mention still a problem?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Length of output: 8917
@lukinovec, you're right — the concern is substantially mitigated. Looking atDatabaseCacheBootstrapper, the$adjustCacheManagerUsingcallback it sets does the exact same thing: it iterates all scoped stores and calls$manager->store($storeName)->getStore()eagerly.scopedStoreNames()also resolvescache.storesup-front. So the eager-materialization pattern is already established and accepted in this codebase.The one remaining (minor) difference is that
makeDatabaseCacheStoresCentraltouches all configured database stores unconditionally, whereasDatabaseCacheBootstrapper's callback only touches the stores it actually scoped ($originalConnections). WhenDatabaseCacheBootstrapper::$storesisnull(the default), both sets are identical, so in practice there's no real difference. Either way, the concern doesn't introduce a new problem — the trade-off is pre-existing. I'll mark this resolved.🧬 Code Graph Analysis Results
Relevant snippets
src/TenancyServiceProvider.phpLine 29-30
Line 80-113
Line 123-142
🧠 Learnings used
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me the crucial difference here seems to be that DatabaseCacheBootstrapper undoes what it does — only for the stores which it touches — for the global cache manager purposes. Whereas here we loop over every single database-based cache store.
In the vast majority of apps there are only going to be one or two database-based cache stores so this isn't a huge issue performance-wise, but the approach does feel a bit crude and if possible I'd prefer something cleaner.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coderabbitai
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stancl, the core issue is that the current loop materializes everydatabase-driver store, including those that already carry an explicitconnectionvalue in config and therefore can never be affected byDatabaseTenancyBootstrapperswitching the default connection. The only stores that actually need correction are those whoseconnection(orlock_connection) config isnull, becausenullmeans "use the current default DB connection" — which is the value that gets flipped totenant.A targeted version that skips already-correct stores:
Key changes from the current version:
connectionandlock_connectionexplicitly set are skipped entirely — no eager materialization.setConnection/setLockConnectionare only called when the relevant config key isnull(the case that actually causes the wrong connection to be used).connection: null, so typically only that one store is materialized.This mirrors the spirit of
DatabaseCacheBootstrapper— touch only what actually needs fixing — without requiring knowledge of which stores were previously scoped.🧠 Learnings used