3 namespace Drupal\webprofiler\DataCollector;
5 use Drupal\webprofiler\DrupalDataCollectorInterface;
6 use Drupal\Core\StringTranslation\StringTranslationTrait;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpFoundation\Response;
9 use Symfony\Component\HttpKernel\DataCollector\DataCollector;
12 * Collects the used cache bins and cache CIDs.
14 class CacheDataCollector extends DataCollector implements DrupalDataCollectorInterface {
16 use StringTranslationTrait, DrupalDataCollectorTrait;
18 const WEBPROFILER_CACHE_HIT = 'bin_cids_hit';
19 const WEBPROFILER_CACHE_MISS = 'bin_cids_miss';
24 public function collect(Request $request, Response $response, \Exception $exception = NULL) {
30 public function __construct() {
31 $this->data['total'][CacheDataCollector::WEBPROFILER_CACHE_HIT] = 0;
32 $this->data['total'][CacheDataCollector::WEBPROFILER_CACHE_MISS] = 0;
33 $this->data['cache'] = [];
37 * Registers a cache get on a specific cache bin.
41 public function registerCacheHit($bin, $cache) {
42 $current = isset($this->data['cache'][$bin][$cache->cid]) ? $this->data['cache'][$bin][$cache->cid] : NULL;
46 $current->{CacheDataCollector::WEBPROFILER_CACHE_HIT} = 0;
47 $current->{CacheDataCollector::WEBPROFILER_CACHE_MISS} = 0;
48 $this->data['cache'][$bin][$cache->cid] = $current;
51 $current->{CacheDataCollector::WEBPROFILER_CACHE_HIT}++;
52 $this->data['total'][CacheDataCollector::WEBPROFILER_CACHE_HIT]++;
56 * Registers a cache get on a specific cache bin.
61 public function registerCacheMiss($bin, $cid) {
62 $current = isset($this->data['cache'][$bin][$cid]) ?
63 $this->data['cache'][$bin][$cid] : NULL;
66 $current = new \StdClass();
67 $current->{CacheDataCollector::WEBPROFILER_CACHE_HIT} = 0;
68 $current->{CacheDataCollector::WEBPROFILER_CACHE_MISS} = 0;
69 $this->data['cache'][$bin][$cid] = $current;
72 $current->{CacheDataCollector::WEBPROFILER_CACHE_MISS}++;
73 $this->data['total'][CacheDataCollector::WEBPROFILER_CACHE_MISS]++;
77 * Callback to return the total amount of requested cache CIDS.
83 public function getCacheCidsCount($type) {
84 return $this->data['total'][$type];
88 * Callback to return the total amount of hit cache CIDS.
92 public function getCacheHitsCount() {
93 return $this->getCacheCidsCount(CacheDataCollector::WEBPROFILER_CACHE_HIT);
97 * Callback to return the total amount of miss cache CIDS.
101 public function getCacheMissesCount() {
102 return $this->getCacheCidsCount(CacheDataCollector::WEBPROFILER_CACHE_MISS);
106 * Callback to return the total amount of hit cache CIDs keyed by bin.
112 public function cacheCids($type) {
114 foreach ($this->data['cache'] as $bin => $caches) {
116 foreach ($caches as $cid => $cache) {
117 $hits[$bin] += $cache->{$type};
125 * Callback to return hit cache CIDs keyed by bin.
129 public function getCacheHits() {
130 return $this->cacheCids(CacheDataCollector::WEBPROFILER_CACHE_HIT);
134 * Callback to return miss cache CIDs keyed by bin.
138 public function getCacheMisses() {
139 return $this->cacheCids(CacheDataCollector::WEBPROFILER_CACHE_MISS);
145 public function getName() {
152 public function getTitle() {
153 return $this->t('Cache');
159 public function getPanelSummary() {
160 return $this->t('Hit: @cache_hit, miss: @cache_miss', [
161 '@cache_hit' => $this->getCacheCidsCount(CacheDataCollector::WEBPROFILER_CACHE_HIT),
162 '@cache_miss' => $this->getCacheCidsCount(CacheDataCollector::WEBPROFILER_CACHE_MISS),
169 public function getIcon() {
170 return 'iVBORw0KGgoAAAANSUhEUgAAABQAAAAcCAYAAABh2p9gAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyRpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYxIDY0LjE0MDk0OSwgMjAxMC8xMi8wNy0xMDo1NzowMSAgICAgICAgIj4gPHJkZjpSREYgeG1sbn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