bouncer: Cross-request caching does not work as expected
Bouncer version: 1.0.0-rc3 Laravel version: 5.7.3
Hi @JosephSilber ,
I recently upgraded to Laravel 5.7 and I noticed that the cache does not work anymore. What I mean is that even with the cache enabled, Bouncer is still making queries to resolve the abilities at the Gate.
Here are some thoughts after my investigation:
-
Bouncer’s service provider is registering and booting. The
bootmethod callsregisterAtGate, which resolves theGatesingleton and aSliber\Bouncer\Clipboardinstance, and then the Clipboard registers its before/after callbacks in theGate, via theClipboard::registerAt()method. -
In my code, I’m calling
Bouncer::cache()in theAuthServiceProvider’sbootmethod. By calling the Bouncer facade, Laravel resolves theBouncersingleton for the first time, which is created through theSilber\Bouncer\Factory. The factory uses a newSliber\Bouncer\Clipboardinstance, as well as theGatesingleton. -
When the
createmethod of the factory is called, thisClipboardinstance registers its before/after callbacks in theGate, via theClipboard::registerAt()method. Now theGatehas two before callbacks, and two after callbacks. -
Then, the
cachemethod is called, which is instantiating aSilber\Bouncer\CachedClipboardinstance with thebeforeslot from the previous clipboard. TheCachedClipboardinstance is then bound into the container, and set to the Bouncer singleton.
So at this point the Bouncer instance has a CachedClipboard instance, but the Gate has two sets of callbacks which are bound on two Silber\Bouncer\Clipboard instances which are not being used by the Bouncer instance anymore. As a result, for each ability check the queries are performed twice, and the cache is never used.
However when I use the isA(), isAn() methods on the user, the CachedClipboard is being used.
I believe this issue is related to #345.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 3
- Comments: 18 (9 by maintainers)
I published RC4 this morning, with the same compatibility as RC3.
https://github.com/JosephSilber/bouncer/releases/tag/v1.0.0-rc.4
I have a fix in the works. Should be out in a few days.
Here’s a PR that fixes this issue #359.
If any of you want to test it, change your composer’s Bouncer dependency to
dev-clipboard-fixand runcomposer update.Please report back with your findings.