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
boot
method callsregisterAtGate
, which resolves theGate
singleton and aSliber\Bouncer\Clipboard
instance, 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
’sboot
method. By calling the Bouncer facade, Laravel resolves theBouncer
singleton for the first time, which is created through theSilber\Bouncer\Factory
. The factory uses a newSliber\Bouncer\Clipboard
instance, as well as theGate
singleton. -
When the
create
method of the factory is called, thisClipboard
instance registers its before/after callbacks in theGate
, via theClipboard::registerAt()
method. Now theGate
has two before callbacks, and two after callbacks. -
Then, the
cache
method is called, which is instantiating aSilber\Bouncer\CachedClipboard
instance with thebefore
slot from the previous clipboard. TheCachedClipboard
instance 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-fix
and runcomposer update
.Please report back with your findings.