Permalink
Browse files

Added individual logging to each Cache service in order to track the …

…results of each separately.

Began the process of updating the templates for the profiler.

Updated the profiler sidebar icon to match the profile bar icon.
  • Loading branch information...
1 parent 363eece commit 0ff002fabc4a6e789e386d4fe22dec21f6546666 @jhallbachner committed Sep 30, 2012
View
157 Collector/CacheDataCollector.php
@@ -16,86 +16,79 @@
*/
class CacheDataCollector extends DataCollector
{
-
- protected $handlerType;
- protected $handlerOptions;
-
- public function __construct($type, $options)
- {
- $this->handlerType = $type;
- $this->handlerOptions = $options;
- }
-
- public function collect(Request $request, Response $response, \Exception $exception = null)
- {
- $record = Cache::$queryRecord;
- if(!isset($record))
- $record = array();
-
- $data = array();
- foreach($record as $query => $calls)
- $data[$query] = array('calls' => count($calls), 'returns' => array_sum($calls));
-
- $info = array('calls' => Cache::$cacheCalls, 'returns' => Cache::$cacheReturns, 'record' => $data);
- $info['handlerType'] = $this->handlerType;
- $info['handlerOptions'] = $this->handlerOptions;
- $handlers = Handlers::getHandlers();
- foreach($handlers as $handler) {
- $pieces = explode('\\', $handler);
- $info['availableHandlers'][] = array_pop($pieces);
- }
- $info['availableHandlers'] = join(', ', $info['availableHandlers']);
-
- if($this->handlerType === 'MultiHandler') {
- $handlers = $info['handlerOptions']['handlers'];
- $info['handlerOptions']['handlers'] = join(', ', $handlers);
-
- foreach($handlers as $h) {
- $info['subhandlerOptions'][$h] = isset($info['handlerOptions'][$h]) ? $info['handlerOptions'][$h] : array();
- unset($info['handlerOptions'][$h]);
- }
- }
-
- $this->data = $info;
- }
-
- public function getCalls()
- {
- return $this->data['calls'];
- }
-
- public function getReturns()
- {
- return $this->data['returns'];
- }
-
- public function getRecord()
- {
- return $this->data['record'];
- }
-
- public function getHandlertype()
- {
- return $this->data['handlerType'];
- }
-
- public function getHandleroptions()
- {
- return $this->data['handlerOptions'];
- }
-
- public function getSubhandleroptions()
- {
- return $this->data['subhandlerOptions'];
- }
-
- public function gethandlers()
- {
- return $this->data['availableHandlers'];
- }
-
- public function getname()
- {
- return 'stash';
- }
+ protected $defaultCache;
+ protected $cacheNames;
+ protected $cacheOptions;
+
+ protected $loggers = array();
+
+ public function __construct($default, $caches, $options)
+ {
+ $this->defaultCache = $default;
+ $this->cacheNames = $caches;
+ $this->cacheOptions = $options;
+ }
+
+ public function addLogger($logger)
+ {
+ $this->loggers[] = $logger;
+ }
+
+ public function collect(Request $request, Response $response, \Exception $exception = null)
+ {
+ $info = array('calls' => 0, 'hits' => 0);
+ foreach($this->loggers as $logger) {
+ $name = $logger->getName();
+ $calls = $logger->getCalls();
+ $hits = $logger->getHits();
+
+ $info['calls'] += $calls;
+ $info['hits'] += $hits;
+
+ $info['caches'][$name]['options'] = $this->cacheOptions[$name];
+ $info['caches'][$name]['queries'] = $logger->getQueries();
+ $info['caches'][$name]['calls'] = $calls;
+ $info['caches'][$name]['hits'] = $hits;
+ }
+
+ $handlers = Handlers::getHandlers();
+ foreach($handlers as $handler) {
+ $pieces = explode('\\', $handler);
+ $info['availableHandlers'][] = array_pop($pieces);
+ }
+ $info['availableHandlers'] = join(', ', $info['availableHandlers']);
+ $info['default'] = $this->defaultCache;
+
+ $this->data = $info;
+ }
+
+ public function getCalls()
+ {
+ return $this->data['calls'];
+ }
+
+ public function getHits()
+ {
+ return $this->data['hits'];
+ }
+
+ public function gethandlers()
+ {
+ return $this->data['availableHandlers'];
+ }
+
+ public function getCaches()
+ {
+ return $this->data['caches'];
+ }
+
+ public function getDefault()
+ {
+ return $this->data['default'];
+ }
+
+ public function getname()
+ {
+ return 'stash';
+ }
}
View
24 DependencyInjection/TedivmStashExtension.php
@@ -27,16 +27,19 @@ public function load(array $configs, ContainerBuilder $container)
$processor = new Processor();
$config = $processor->processConfiguration(new Configuration(), $configs);
-var_dump($config);
+
$container->setAlias('cache', sprintf('stash.%s_cache', $config['default_cache']));
$caches = array();
+ $options = array();
foreach($config['caches'] as $name => $cache) {
$caches[$name] = sprintf('stash.%s_cache', $name);
+ $options[$name] = $cache;
$this->addCacheService($name, $cache, $container);
}
$container->setParameter('stash.caches', $caches);
+ $container->setParameter('stash.caches.options', $options);
$container->setParameter('stash.default_cache', $config['default_cache']);
}
@@ -53,14 +56,31 @@ protected function addCacheService($name, $cache, $container)
))
->setAbstract(false)
;
-var_dump($container);
+
+ $container
+ ->setDefinition(sprintf('stash.logger.%s_cache', $name), new DefinitionDecorator('stash.logger'))
+ ->setArguments(array(
+ $name
+ ))
+ ->setAbstract(false)
+ ;
+
$container
->setDefinition(sprintf('stash.%s_cache', $name), new DefinitionDecorator('stash.cache'))
->setArguments(array(
+ $name,
new Reference(sprintf('stash.handler.%s_cache', $name))
))
->setAbstract(false)
;
+
+ $container
+ ->getDefinition('data_collector.stash')
+ ->addMethodCall('addLogger', array(
+ new Reference(sprintf('stash.logger.%s_cache', $name))
+ ))
+ ;
+
}
public function getAlias()
View
14 Resources/config/services.yml
@@ -1,15 +1,27 @@
parameters:
- stash.cache.class: Tedivm\StashBundle\Service\Cache
+ stash.cache.class: Tedivm\StashBundle\Service\CacheService
+ stash.logger.class: Tedivm\StashBundle\Service\CacheLogger
stash.handler.class: Tedivm\Stash\Handler\HandlerInterface
stash.factory.class: Tedivm\StashBundle\Factory\HandlerFactory
services:
stash.cache:
class: %stash.cache.class%
abstract: true
+ stash.logger:
+ class: %stash.logger.class%
+ abstract: true
stash.handler:
class: %stash.handler.class%
abstract: true
synthetic: true
factory_method: createHandler
factory_class: %stash.factory.class%
+ data_collector.stash:
+ class: Tedivm\StashBundle\Collector\CacheDataCollector
+ arguments: [ %stash.default_cache%, %stash.caches%, %stash.caches.options% ]
+ tags:
+ -
+ name: data_collector
+ template: "TedivmStashBundle:Profiler:layout"
+ id: "stash"
View
BIN Resources/public/images/stash.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
96 Resources/views/Profiler/layout.html.twig
@@ -1,80 +1,54 @@
{% extends 'WebProfilerBundle:Profiler:layout.html.twig' %}
{% block toolbar %}
- {% set icon %}
- <img width="22" height="28" alt="Memory Usage" style="vertical-align: middle;" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAcCAYAAABlL09dAAAABGdBTUEAALGPC/xhBQAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9wHGAALFQCM6LQAAAFwSURBVEjH7ZW9S4JRFIefRAoaAmlpbguCiKaGIqjh1FI0SV+vH6G2BmW4RlJG0Rj08RcE0aI3bGqPioagD6IpSEISGiSiBt/ATK+91ynwLBfOOffhx+/cD6jHv40GpVQ7EDXcnxCR+7JgAKVUGhgyAJ8CAyLyWVpw2esC8GEA7gesiopt1btA0AD+AnSISKacYoAYkDMAtwKJSlYgIs/AmuEQLaXUYHHCXdKwBdxqAFlN7UkH7jQ8Hd9z2gZuROTNXZQcBg6AZgPmOxAC2gAvEHXZUC9wZAjNASPAgy2ssdiKa6DX0IEM0GJfluYfHi8txi5qeRdWE/F5wFPpHNf05pQm3Lru4GyAyakJPJ6CmO6unkqtLkdgf8DHxvomyWSKfD7vSLHWipXlOGPjo6RPjglHQo680YJTKYU17ScSnsPyzehamxxZcX55VrjH2Sx7O/t/teKxKlgzrHLgV8AvIodVwQ7iDugTkav6b/8rvgBcMlqf0kD/YAAAAABJRU5ErkJggg=="/>
- <span class="sf-toolbar-status sf-toolbar-status-green">{{ collector.calls }} call{% if collector.calls != 1 %}s{% endif %} / {{ collector.returns }} return{% if collector.returns != 1 %}s{% endif %}</span>
- {% endset %}
+ {% set icon %}
+ <img width="22" height="28" alt="Memory Usage" style="vertical-align: middle;" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAcCAYAAABlL09dAAAABGdBTUEAALGPC/xhBQAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9wHGAALFQCM6LQAAAFwSURBVEjH7ZW9S4JRFIefRAoaAmlpbguCiKaGIqjh1FI0SV+vH6G2BmW4RlJG0Rj08RcE0aI3bGqPioagD6IpSEISGiSiBt/ATK+91ynwLBfOOffhx+/cD6jHv40GpVQ7EDXcnxCR+7JgAKVUGhgyAJ8CAyLyWVpw2esC8GEA7gesiopt1btA0AD+AnSISKacYoAYkDMAtwKJSlYgIs/AmuEQLaXUYHHCXdKwBdxqAFlN7UkH7jQ8Hd9z2gZuROTNXZQcBg6AZgPmOxAC2gAvEHXZUC9wZAjNASPAgy2ssdiKa6DX0IEM0GJfluYfHi8txi5qeRdWE/F5wFPpHNf05pQm3Lru4GyAyakJPJ6CmO6unkqtLkdgf8DHxvomyWSKfD7vSLHWipXlOGPjo6RPjglHQo680YJTKYU17ScSnsPyzehamxxZcX55VrjH2Sx7O/t/teKxKlgzrHLgV8AvIodVwQ7iDugTkav6b/8rvgBcMlqf0kD/YAAAAABJRU5ErkJggg=="/>
+ <span class="sf-toolbar-status sf-toolbar-status-green">{{ collector.calls }} call{% if collector.calls != 1 %}s{% endif %} / {{ collector.hits }} hits{% if collector.hits != 1 %}s{% endif %}</span>
+ {% endset %}
{% set text %}
<div class="sf-toolbar-info-piece">
<b>Cache Handler</b>
- <span>{{ collector.handlertype }}</span>
+ <span></span>
</div>
{% endset %}
- {% include 'WebProfilerBundle:Profiler:toolbar_item.html.twig' with { 'link': profiler_url } %}
+ {% include 'WebProfilerBundle:Profiler:toolbar_item.html.twig' with { 'link': profiler_url } %}
{% endblock %}
{% block menu %}
<span class="label">
- <span class="icon"><img src="{{ asset('bundles/tedivmstash/images/stash.png') }}" alt="" /></span>
- <strong>Stash</strong>
- <span class="count">
- <span>{{ collector.calls}} / {{ collector.returns }}</span>
- </span>
+ <span class="icon"><img src="{{ asset('bundles/tedivmstash/images/stash.png') }}" alt="" /></span>
+ <strong>Stash</strong>
+ <span class="count">
+ <span>{{ collector.calls }} / {{ collector.hits }}</span>
+ </span>
</span>
{% endblock %}
{% block panel %}
- <h2>Cache Details</h2>
- <table>
- <tr>
- <th>Available Handlers</th>
- <td>{{ collector.handlers }}</td>
- </tr>
- <tr>
- <th>Cache Handler</th>
- <td>{{ collector.handlertype }}</td>
- </tr>
- {% for option, value in collector.handleroptions %}
- <tr>
- <th>{{ option }}</th>
- <td>{{ value }}</td>
- </tr>
- {% endfor %}
- {% if collector.handlertype == 'MultiHandler' %}
- {% for subhandler, options in collector.subhandleroptions %}
- <tr>
- <th colspan=2>{{ subhandler }} Options</th>
- </tr>
- {% for option, value in options %}
- <tr>
- <th>{{ option }}</th>
- <td>{{ value }}</td>
- </tr>
- {% endfor %}
- {% endfor %}
- {% endif %}
- </table>
+ <h2>Cache Information</h2>
+ <table>
+ <tr>
+ <th>Default Cache</th>
+ <td>{{ collector.default }}</td>
+ <tr>
+ <th>Available Handlers</th>
+ <td>{{ collector.handlers }}</td>
+ </tr>
+ <tr>
+ <th>Total Requests</th>
+ <td>{{ collector.calls }}</td>
+ </tr>
+ <tr>
+ <th>Total Hits</th>
+ <td>{{ collector.hits }}</td>
+ </tr>
+ </table>
+
+ <h2>Cache Services</h2>
+
+ {% for name, details in collector.caches %}
- <h2>Cache Stats</h2>
- <table>
- <tr>
- <th>Cache Calls</th>
- <td>{{ collector.calls }}</td>
- </tr>
- <tr>
- <th>Cache Returns</th>
- <td>{{ collector.returns }}</td>
- </tr>
- </table>
+ <h3>{{ name }}</h3>
- <h2>Cache Requests</h2>
- <table>
- {% for query, response in collector.record %}
- <tr>
- <th>{{ query }}</th>
- <td>{{ response.calls }} call{% if response.calls != 1 %}s{% endif %} / {{ response.returns }} return{% if response.returns != 1 %}s{% endif %}</td>
- </tr>
- {% endfor %}
- </table>
+ {% endfor %}
{% endblock %}
View
56 Service/CacheLogger.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace Tedivm\StashBundle\Service;
+
+/**
+ * Basic in-memory query
+ *
+ * @author Josh Hall-Bachner <jhallbachner@gmail.com>
+ */
+class CacheLogger
+{
+ protected $name;
+
+ protected $calls = 0;
+ protected $hits = 0;
+
+ protected $queries = array();
+
+ public function __construct($name)
+ {
+ $this->name = $name;
+ }
+
+ public function logRequest($key, $hit, $value)
+ {
+ $this->calls++;
+ if($hit) {
+ $this->hits++;
+ }
+ $this->queries[] = array(
+ 'key' => $key,
+ 'hit' => $hit,
+ 'value' => $value
+ );
+ }
+
+ public function getName()
+ {
+ return $this->name;
+ }
+
+ public function getCalls()
+ {
+ return $this->calls;
+ }
+
+ public function getHits()
+ {
+ return $this->hits;
+ }
+
+ public function getQueries()
+ {
+ return $this->queries;
+ }
+}
View
46 Service/CacheResultObject.php
@@ -0,0 +1,46 @@
+<?php
+
+namespace Tedivm\StashBundle\Service;
+use Stash\Handlers;
+
+/**
+ * Simple result-object provider for the Stash class.
+ *
+ * @author Josh Hall-Bachner <jhallbachner@gmail.com>
+ */
+class Cache
+{
+ protected $cache;
+ protected $logger;
+
+ /**
+ * Constructs the cache holder. Parameter is a Stash handler which is dynamically injected at service creation.
+ *
+ * @param StashHandler $handler
+ */
+ public function __construct($cache, $logger = null)
+ {
+ $this->cache = $cache;
+ $this->logger = $logger;
+ }
+
+ public function __call($name, $args)
+ {
+ if($name === 'get') {
+ return $this->getAndLog($args);
+ } else {
+ return call_user_func_array(array($this->cache, $name), $args);
+ }
+ }
+
+ protected function getAndLog($args)
+ {
+ $result = call_user_func_array(array($this->cache, 'get'), $args);
+ $miss = $this->cache->isMiss();
+ $key = $this->cache->getKey();
+
+ $this->logger->logRequest($key, !($miss), $result);
+
+ return $result;
+ }
+}
View
17 Service/Cache.php → Service/CacheService.php
@@ -9,18 +9,26 @@
*
* @author Josh Hall-Bachner <jhallbachner@gmail.com>
*/
-class Cache
+class CacheService
{
+ protected $name;
protected $handler;
+ protected $key;
+
+ protected $logger;
/**
* Constructs the cache holder. Parameter is a Stash handler which is dynamically injected at service creation.
*
* @param StashHandler $handler
*/
- public function __construct($handler)
+ public function __construct($name, $handler, $logger = null)
{
+ $this->name = $name;
$this->handler = $handler;
+ $this->key = '@@_' . $name . '_@@';
+
+ $this->logger = $logger;
}
/**
@@ -37,8 +45,11 @@ public function get()
if(count($args) == 1 && is_array($args[0]))
$args = $args[0];
+ array_unshift($args, $this->key);
+
$handler = (isset($this->handler)) ? $this->handler : null;
- $stash = new StashCache($handler);
+ $cache = new StashCache($handler);
+ $stash = new CacheResultObject($cache, $logger);
if(count($args) > 0)
$stash->setupKey($args);

0 comments on commit 0ff002f

Please sign in to comment.