Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Added cache panel #113

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Currently, the following panels have been written and are working:
* A list of superglobals
* Application settings
* SQL queries including time to execute and param bindings
* Cache requests including time
* Logging output via Yii built-in logging


Expand Down Expand Up @@ -54,6 +55,22 @@ For use [yii-debug-toolbar](/malyshev/yii-debug-toolbar/) need to specify new `r
),
```


* To enable reports about cache usage you need to change application configuration like that
```php
<?php
//...
'cache'=>array(
'class' => 'ext.yii-debug-toolbar.YiiDebugCacheProxy',
'enableProfiling' => true,
'cache' => array(
'class' => 'system.caching.CFileCache', // or other cache class
//... any other valid settings for cache
),
),
```


## TODOs and BUGS

See: [issues](https://github.com/malyshev/yii-debug-toolbar/issues)
Expand Down
245 changes: 245 additions & 0 deletions YiiDebugCacheProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
<?php
/**
* YiiDebugCacheProxy class file.
*
* @author Pavel Volyntsev <[email protected]>
*/

Yii::import('system.caching.CCache');

/**
* Class YiiDebugCacheProxy
*
* Class to profile cache requests
*
* @author Pavel Volyntsev <[email protected]>
* @version $Id$
* @package
*/
class YiiDebugCacheProxy extends CCache
{
/**
* @var boolean whether to enable profiling requests to cache.
* Defaults to false. This should be mainly enabled and used during development
* to find out the bottleneck of cache executions.
*/
public $enableProfiling = false;

/**
* Options to create cache component
* @var array
*/
public $cache;

/**
* Cache component instance
* @var \CCache
*/
protected $_cacheProxy;

/**
* Cache usage statistics
* @var array
*/
protected $_stats = array(
'get' => 0, // read requests quantity
'get_time' => 0, // read time
'mget' => 0,
'mget_time' => 0,
'set' => 0, // write requests quantity
'set_time' => 0, // write time
'add' => 0, // add requests quantity
'add_time' => 0,
'delete' => 0, // delete requests quantity
'delete_time' => 0, // delete time
'flush' => 0,
'flush_time' => 0,
'hit' => 0, // read requests when data found
'miss' => 0, // read requests when no data found
'count' => 0, // total requests
'time' => 0, // total time
);

/**
* Key for profiling logs
* @var
*/
private $_logCategory;

const LOG_CATEGORY = 'system.caching.';

public function init()
{
parent::init();
$this->_logCategory = self::LOG_CATEGORY.get_class($this);
}

/**
* @return CCache
*/
protected function getCacheProxy()
{
if (is_null($this->_cacheProxy))
{
$this->_cacheProxy = Yii::createComponent($this->cache);
$this->_cacheProxy->init();
}
return $this->_cacheProxy;
}

/**
* @see \CCache::get
* @param string $id
* @return mixed
*/
public function get($id)
{
if ($this->enableProfiling)
{
Yii::beginProfile(__METHOD__.'("'.$id.'")', $this->_logCategory.'.get');
}
$value = $this->getCacheProxy()->get($id);
++$this->_stats['get'];
++$this->_stats['hit'];
if (false===$value)
++$this->_stats['miss'];
if ($this->enableProfiling)
{
Yii::endProfile(__METHOD__.'("'.$id.'")', $this->_logCategory.'.get');
}
return $value;
}

/**
* @see \CCache::mget
* @param string[] $ids
* @return array
*/
public function mget($ids)
{
if ($this->enableProfiling)
{
Yii::beginProfile(__METHOD__.'("'.implode('","', $ids).'")', $this->_logCategory.'.mget');
}
$value = $this->getCacheProxy()->mget($ids);
++$this->_stats['mget'];
++$this->_stats['hit'];
if (empty($value))
{
++$this->_stats['miss'];
}
if ($this->enableProfiling)
{
Yii::endProfile(__METHOD__.'("'.implode('","', $ids).'")', $this->_logCategory.'.mget');
}
return $value;
}

/**
* @see \CCache::set
* @param string $id
* @param mixed $value
* @param int $expire
* @param null $dependency
* @return bool|mixed
*/
public function set($id, $value, $expire = 0, $dependency = null)
{
if ($this->enableProfiling)
{
Yii::beginProfile(__METHOD__.'("'.$id.'")', $this->_logCategory.'.set');
}
$returnValue = $this->getCacheProxy()->set($id, $value, $expire, $dependency);
++$this->_stats['set'];
if ($this->enableProfiling)
{
Yii::endProfile(__METHOD__.'("'.$id.'")', $this->_logCategory.'.set');
}
return $returnValue;
}

/**
* @see \CCache::add
* @param string $id the key identifying the value to be cached
* @param mixed $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param ICacheDependency $dependency dependency of the cached item. If the dependency changes, the item is labeled invalid.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
public function add($id,$value,$expire=0,$dependency=null)
{
if ($this->enableProfiling)
{
Yii::beginProfile(__METHOD__.'("'.$id.'")', $this->_logCategory.'.add');
}

$returnValue = $this->getCacheProxy()->add($id, $value, $expire, $dependency);

if ($this->enableProfiling)
{
++$this->_stats['add'];
Yii::endProfile(__METHOD__.'("'.$id.'")', $this->_logCategory.'.add');
}
return $returnValue;
}

/**
* @see \CCache::delete
* @param string $id
* @return bool
*/
public function delete($id)
{
if ($this->enableProfiling)
{
Yii::beginProfile(__METHOD__.'("'.$id.'")', $this->_logCategory.'.delete');
}
$value = $this->getCacheProxy()->delete($id);
++$this->_stats['delete'];
if ($this->enableProfiling)
{
Yii::endProfile(__METHOD__.'("'.$id.'")', $this->_logCategory.'.delete');
}
return $value;
}

/**
* @see \CCache::flush
* @return bool
*/
public function flush()
{
if ($this->enableProfiling)
{
Yii::beginProfile(__METHOD__.'()', $this->_logCategory.'.flush');
}
$value = $this->getCacheProxy()->flush();
++$this->_stats['flush'];
if ($this->enableProfiling)
{
Yii::endProfile(__METHOD__.'()', $this->_logCategory.'.flush');
}
return $value;
}

/**
* Returns the statistics about cache usage.
* @return array
*/
public function getStats()
{
$this->_stats['count'] = $this->_stats['get'] + $this->_stats['mget'] + $this->_stats['set'] + $this->_stats['add'] + $this->_stats['delete'] + $this->_stats['flush'];
if ($this->enableProfiling)
{
$logger = Yii::getLogger();
$this->_stats['get_time'] = array_sum($logger->getProfilingResults(null, $this->_logCategory.'.get'));
$this->_stats['mget_time'] = array_sum($logger->getProfilingResults(null, $this->_logCategory.'.mget'));
$this->_stats['set_time'] = array_sum($logger->getProfilingResults(null, $this->_logCategory.'.set'));
$this->_stats['add_time'] = array_sum($logger->getProfilingResults(null, $this->_logCategory.'.add'));
$this->_stats['delete_time'] = array_sum($logger->getProfilingResults(null, $this->_logCategory.'.delete'));
$this->_stats['flush_time'] = array_sum($logger->getProfilingResults(null, $this->_logCategory.'.flush'));
$this->_stats['time'] = $this->_stats['get_time'] + $this->_stats['mget_time'] + $this->_stats['set_time'] + $this->_stats['delete_time'] + $this->_stats['flush_time'];
}
return $this->_stats;
}
}
1 change: 1 addition & 0 deletions YiiDebugToolbarRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class YiiDebugToolbarRoute extends CLogRoute
'YiiDebugToolbarPanelViews',
'YiiDebugToolbarPanelSql',
'YiiDebugToolbarPanelLogging',
'YiiDebugToolbarPanelCache',
);

/**
Expand Down
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,12 @@
"email": "[email protected]",
"homepage": "http://www.yiiframework.com/extension/yii-debug-toolbar/"
}
]
],
"autoload": {
"classmap": [
".",
"widgets/",
"panels/"
]
}
}
11 changes: 11 additions & 0 deletions messages/ru/yii-debug-toolbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@
'Servers' => 'Сервер',
'No SQL queries were recorded during this request or profiling the SQL is DISABLED.' => 'Запросо к базе не было, либо профилирование SQL отключено.',

// Cache
'Cache' => 'Кэш',
'Total' => 'Общее количество',
'Hit' => 'Количество чтений',
'Miss' => 'Количество промахов',
'No cache usage' => 'Кэш не используется',
'Cache requests' => 'Запросы к кэшу',
'Method' => 'Метод',
'Cache not configured' => 'Кэш не настроен',
'No cache request logged or profiling the cache is DISABLED.' => 'Обращений к кэшу нет, либо профилирование кэша отключено',

// logging
'Logging' => 'Логирование',
'{n} message|{n} messages' => '{n} сообщение|{n} сообщения|{n} сообщений',
Expand Down
11 changes: 11 additions & 0 deletions messages/simple/yii-debug-toolbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@
'Servers' => '',
'No SQL queries were recorded during this request or profiling the SQL is DISABLED.' => '',

// Cache
'Cache' => '',
'Total' => '',
'Hit' => '',
'Miss' => '',
'No cache usage' => '',
'Cache requests' => '',
'Method' => '',
'Cache not configured' => '',
'No cache request logged or profiling the cache is DISABLED.' => '',

// logging
'Logging' => '',
'{n} message|{n} messages' => '',
Expand Down
Loading