Skip to content
This repository was archived by the owner on Jan 8, 2024. It is now read-only.

Commit 7a28144

Browse files
committed
Added stopwatch profiling on instance creation
1 parent 87b4a9a commit 7a28144

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

Resources/config/services.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ services:
88
class: "%php_fast_cache.cache.class%"
99
arguments:
1010
- "%phpfastcache%"
11+
- "@?debug.stopwatch"
1112

1213
phpfastcache.request_collector:
1314
class: "%php_fast_cache.data_collector.class%"

Service/Cache.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,24 @@
55
use phpFastCache\Cache\ExtendedCacheItemPoolInterface;
66
use phpFastCache\CacheManager;
77
use phpFastCache\Exceptions\phpFastCacheDriverException;
8+
use Symfony\Component\Stopwatch\Stopwatch;
89

910
/**
1011
* Class Cache
1112
* @package phpFastCache\Bundle\Service
1213
*/
1314
class Cache
1415
{
16+
/**
17+
* @var array
18+
*/
1519
private $drivers = [];
1620

21+
/**
22+
* @var Stopwatch
23+
*/
24+
protected $stopwatch;
25+
1726
/**
1827
* Contains all cache instances
1928
*
@@ -25,12 +34,14 @@ class Cache
2534
* Cache constructor.
2635
*
2736
* @param $drivers
37+
* @param Stopwatch $stopwatch
2838
*
2939
* @throws \phpFastCache\Exceptions\phpFastCacheDriverException
3040
*/
31-
public function __construct($drivers)
41+
public function __construct($drivers, Stopwatch $stopwatch = null)
3242
{
3343
$this->drivers = (array) $drivers[ 'drivers' ];
44+
$this->stopwatch = $stopwatch;
3445
}
3546

3647
/**
@@ -60,6 +71,10 @@ public function createInstance($name, ExtendedCacheItemPoolInterface $instance)
6071
*/
6172
public function get($name)
6273
{
74+
if ($this->stopwatch) {
75+
$this->stopwatch->start(__METHOD__ . "('{$name}')");
76+
}
77+
6378
if (!array_key_exists($name, $this->cacheInstances)) {
6479
if (array_key_exists($name, $this->drivers)) {
6580
$this->createInstance($name, CacheManager::getInstance($this->drivers[ $name ][ 'type' ], $this->drivers[ $name ][ 'parameters' ]));
@@ -71,6 +86,9 @@ public function get($name)
7186
}
7287
}
7388

89+
if ($this->stopwatch) {
90+
$this->stopwatch->stop(__METHOD__ . "('{$name}')");
91+
}
7492
return $this->cacheInstances[ $name ];
7593
}
7694

0 commit comments

Comments
 (0)