Skip to content
This repository has been archived by the owner on Jul 25, 2023. It is now read-only.

Commit

Permalink
Merge branch 'release/0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
punkstar committed Oct 27, 2013
2 parents 2ff4a22 + bba96ee commit 55fa0b2
Show file tree
Hide file tree
Showing 21 changed files with 685 additions and 81 deletions.
38 changes: 34 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
Mage-Cache-Viewer
=================
# Meanbee_CacheViewer

View the status of Magento Caches
Meanbee_CacheViewer provides interfaces for inspecting Magento cache.

Access from System > Cache Management > View Cache Statistics.
## Usage

### Analysing Block Cache

Enabled with a configuration setting in `System` » `Configuration` » `Cache Viewer`, the frontend overlay indicates
which blocks were generated (red) and which were retrieved from the block_html cache (green) as well as showing the
last modified time for each block and the total time taken to dispatch the request.

This will become enabled for the frontend and the admin area and is controlled by the control bar added at the bottom of the viewport.

![Cache Viewer frontend overlay](http://f.cl.ly/items/0F2D0s3c0I34210z1e3S/cache-viewer-frontend.png)

![Cache Viewer frontend overlay with hints disabled](http://f.cl.ly/items/0N2t3R0R2a232R3w191L/cache-viewer-frontend-nohints.png)

### View Cache Contents

The backend interface, found in `System` » `Cache Management` » `View Cache Contents` in the Administration area, allows
viewing, inspecting or deleting all of the cache entries present in the Magento cache.

![Cache Viewer](http://up.nicksays.co.uk/image/0C1W29041R3y/release_0.1.0.png)

## License

Meanbee_CacheViewer, a Magento extension for inspecting cache contents.

Copyright (C) 2013, Meanbee Limited.

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
42 changes: 40 additions & 2 deletions app/code/community/Meanbee/CacheViewer/Block/Adminhtml/Report.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
<?php
class Meanbee_CacheViewer_Block_Adminhtml_Report extends Mage_Adminhtml_Block_Template
{
/**
* @var null
*/
public $_total_cache_size_bytes = null;

/**
* @var array
*/
public $_cache_items = array();

/**
* Regular expressions used to organise tags into groups.
*
* @var array
*/
public $_cache_types = array(
"Block HTML" => array(
"/BLOCK_HTML$/"
Expand All @@ -18,6 +31,9 @@ class Meanbee_CacheViewer_Block_Adminhtml_Report extends Mage_Adminhtml_Block_Te
)
);

/**
*
*/
public function _construct()
{
parent::_construct();
Expand All @@ -27,6 +43,9 @@ public function _construct()
}
}

/**
* @return Meanbee_CacheViewer_Model_CacheItemInfo[]
*/
public function getCacheItems()
{
if (count($this->_cache_items) == 0) {
Expand Down Expand Up @@ -100,7 +119,7 @@ public function getAsTimeUnits($raw_seconds)
}

/**
* @return int
* @return double
*/
public function getUsedCacheSizeInMb()
{
Expand All @@ -112,7 +131,7 @@ public function getUsedCacheSizeInMb()
}

/**
* @return int
* @return double
*/
public function getTotalCacheSizeInMb()
{
Expand All @@ -135,6 +154,9 @@ public function getCacheBackend()
return get_class($this->_getCache()->getBackend());
}

/**
* @return array
*/
public function getCacheStatistics()
{
$cache_tag_sizes = array();
Expand Down Expand Up @@ -168,4 +190,20 @@ public function getCacheStatistics()

return $cache_tag_sizes;
}

/**
* @return array
*/
public function getCacheStatisticsGoogleChartsFormat()
{
$data_structure = array();

$data_structure[] = array('Cache Type', 'Usage');

foreach ($this->getCacheStatistics() as $name => $value) {
$data_structure[] = array($name, $value);
}

return $data_structure;
}
}
34 changes: 34 additions & 0 deletions app/code/community/Meanbee/CacheViewer/Block/Toolbar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

class Meanbee_CacheViewer_Block_Toolbar extends Mage_Core_Block_Template {

public function _toHtml() {

if(!Mage::getStoreConfig(Meanbee_CacheViewer_Helper_Data::XML_PATH_CACHEVIEWER_BLOCKS_VIEW_FRONTEND))
return;

return parent::_toHtml();
}

/**
* Return the configured default cookie path.
*
* @return mixed
*/
protected function getCookiePath() {
return Mage::getSingleton('core/cookie')->getPath();
}

/**
* Return the configured default cookie domain.
*
* @return string
*/
protected function getCookieDomain() {
$domain = Mage::getSingleton('core/cookie')->getDomain();
if (!empty($domain[0]) && ($domain[0] !== '.')) {
$domain = '.'.$domain;
}
return $domain;
}
}
10 changes: 9 additions & 1 deletion app/code/community/Meanbee/CacheViewer/Helper/Data.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
<?php

class Meanbee_CacheViewer_Helper_Data extends Mage_Core_Helper_Abstract {}
class Meanbee_CacheViewer_Helper_Data extends Mage_Core_Helper_Abstract {

const XML_PATH_CACHEVIEWER_BLOCKS_VIEW_FRONTEND = "cacheviewer/blocks/view_frontend";

public function isShowCacheStatusOnFrontend() {
return Mage::getStoreConfigFlag(self::XML_PATH_CACHEVIEWER_BLOCKS_VIEW_FRONTEND);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public function getModified()
return new Zend_Date(parent::getModified());
}

/**
* @return Zend_Date
*/
public function getExpires()
{
return new Zend_Date(parent::getExpires());
Expand Down
4 changes: 4 additions & 0 deletions app/code/community/Meanbee/CacheViewer/Model/DateInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
*/
class Meanbee_CacheViewer_Model_DateInterval extends DateInterval {

/**
* @param $seconds
* @return Meanbee_CacheViewer_Model_DateInterval
*/
public static function createFromSeconds($seconds)
{
if ($seconds > 0) {
Expand Down
93 changes: 93 additions & 0 deletions app/code/community/Meanbee/CacheViewer/Model/Observer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

class Meanbee_CacheViewer_Model_Observer extends Mage_Core_Model_Abstract {

const EVENT_BEFORE = 'core_block_abstract_to_html_before';
const EVENT_AFTER = 'core_block_abstract_to_html_after';

/** @var Meanbee_CacheViewer_Helper_Data */
protected $_helper;

protected $_dispatch_start_time;

public function _construct() {
parent::_construct();

$this->_helper = Mage::helper('cacheviewer');
}

/**
* Add cache block status
* @param Varien_Event_Observer $observer
* @return void
*/
public function addBlockCacheStatuses(Varien_Event_Observer $observer) {
$event = $observer->getEvent();
$block = $event->getBlock();
$transportObject = $event->getTransport();

if (!$this->_helper->isShowCacheStatusOnFrontend()) {
return;
}

if (in_array(get_class($block), array("Mage_Page_Block_Html", "Mage_Adminhtml_Block_Page"))) {
return;
}

// Get cache status
$cache = Mage::app()->getCache()->test(strtoupper($block->getCacheKey()));
$lastModified = date(DATE_ATOM, time());
$blockName = get_class($block);
$cacheClass = "";


// If cached, get last modified date and add cached class for css.
if($cache) {
$lastModified = date(DATE_ATOM, $cache);
$cacheClass = " cacheviewer-block-cached";
}

// Get transport object passed through event and wrap out hints around it.
$html = $transportObject->getHtml();
$html = <<<HTML
<div class="cacheviewer-container clearer">
<div class="cacheviewer-block{$cacheClass}">
<div class="cacheviewer-hints">
<span class="cacheviewer-lastmodified">{$lastModified}</span>
<span class="cacheviewer-name">{$blockName}</span>
</div>
</div>
{$html}
</div>
HTML;

$transportObject->setHtml($html);

return;
}

/**
* Start the timer for tracking dispatch time.
* Observe: controller_action_predispatch
*
* @param Varien_Event_Observer $observer
*/
public function startTimer(Varien_Event_Observer $observer) {
$this->_dispatch_start_time = microtime(true);
}

/**
* Stop the timer tracking dispatch time and set the elapsed time in a cookie.
*
* @param Varien_Event_Observer $observer
*/
public function stopTimer(Varien_Event_Observer $observer) {
$dispatch_finish_time = microtime(true);

$elapsed_time = $dispatch_finish_time - $this->_dispatch_start_time;

if ($this->_helper->isShowCacheStatusOnFrontend()) {
Mage::getSingleton('core/cookie')->set("cacheviewer_dispatch_time", sprintf("%.3fs", $elapsed_time), 0, null, null, null, false);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
<?php
class Meanbee_CacheViewer_Adminhtml_Meanbee_CacheviewerController extends Mage_Adminhtml_Controller_Action
{
/**
* Route: /admin/meanbee_cacheviewer/report/
*
* Show the cache report.
*/
public function reportAction()
{
$this->loadLayout();
$this->getLayout()->getBlock('head')->setTitle("Meanbee CacheViewer");
$this->renderLayout();
}

/**
* Route: /admin/meanbee_cacheviewer/view_single/
* Params: cache_id
*
* Fetch the value of a given cache key.
*/
public function view_singleAction()
{
$cache_id = $this->getRequest()->getParam('cache_id');
Expand All @@ -17,6 +28,12 @@ public function view_singleAction()
$this->getResponse()->setBody(($cache_data) ? Mage::helper('core')->escapeHtml($cache_data) : '(false/empty)');
}

/**
* Route: /admin/meanbee_cacheviewer/delete_single/
* Params: cache_id
*
* Delete a single cache entry.
*/
public function delete_singleAction()
{
$cache_id = $this->getRequest()->getParam('cache_id');
Expand Down
Loading

0 comments on commit 55fa0b2

Please sign in to comment.