Skip to content

Commit c1bec87

Browse files
[11.x] Configuration to disable events on Cache Repository (#51032)
* Configuration option to disable events on cache Allow caches to be defined without auto-registering events dispatcher. On in-memory caches like array or apc, the overhead of triggering events results in 80%+ overhead in basic cache operations. This change allows adding a config value to disable events for that particular Cache Repository. * Update CacheManager.php * Update CacheManagerTest.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 4341063 commit c1bec87

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/Illuminate/Cache/CacheManager.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,10 @@ protected function newDynamodbClient(array $config)
288288
*/
289289
public function repository(Store $store, array $config = [])
290290
{
291-
return tap(new Repository($store, Arr::only($config, ['store'])), function ($repository) {
292-
$this->setEventDispatcher($repository);
291+
return tap(new Repository($store, Arr::only($config, ['store'])), function ($repository) use ($config) {
292+
if ($config['events'] ?? true) {
293+
$this->setEventDispatcher($repository);
294+
}
293295
});
294296
}
295297

tests/Cache/CacheManagerTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,36 @@ public function testThrowExceptionWhenUnknownStoreIsUsed()
277277
$cacheManager->store('alien_store');
278278
}
279279

280+
public function testMakesRepositoryWithoutDispatcherWhenEventsDisabled()
281+
{
282+
$userConfig = [
283+
'cache' => [
284+
'stores' => [
285+
'my_store' => [
286+
'driver' => 'array',
287+
],
288+
'my_store_without_events' => [
289+
'driver' => 'array',
290+
'events' => false,
291+
],
292+
],
293+
],
294+
];
295+
296+
$app = $this->getApp($userConfig);
297+
$app->bind(Dispatcher::class, fn () => new Event);
298+
299+
$cacheManager = new CacheManager($app);
300+
301+
// The repository will have an event dispatcher
302+
$repo = $cacheManager->store('my_store');
303+
$this->assertNotNull($repo->getEventDispatcher());
304+
305+
// This repository will not have an event dispatcher as 'with_events' is false
306+
$repoWithoutEvents = $cacheManager->store('my_store_without_events');
307+
$this->assertNull($repoWithoutEvents->getEventDispatcher());
308+
}
309+
280310
protected function getApp(array $userConfig)
281311
{
282312
$app = new Container;

0 commit comments

Comments
 (0)