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

Commit 55fa0b2

Browse files
committed
Merge branch 'release/0.2.0'
2 parents 2ff4a22 + bba96ee commit 55fa0b2

File tree

21 files changed

+685
-81
lines changed

21 files changed

+685
-81
lines changed

README.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
1-
Mage-Cache-Viewer
2-
=================
1+
# Meanbee_CacheViewer
32

4-
View the status of Magento Caches
3+
Meanbee_CacheViewer provides interfaces for inspecting Magento cache.
54

6-
Access from System > Cache Management > View Cache Statistics.
5+
## Usage
6+
7+
### Analysing Block Cache
8+
9+
Enabled with a configuration setting in `System` » `Configuration` » `Cache Viewer`, the frontend overlay indicates
10+
which blocks were generated (red) and which were retrieved from the block_html cache (green) as well as showing the
11+
last modified time for each block and the total time taken to dispatch the request.
12+
13+
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.
14+
15+
![Cache Viewer frontend overlay](http://f.cl.ly/items/0F2D0s3c0I34210z1e3S/cache-viewer-frontend.png)
16+
17+
![Cache Viewer frontend overlay with hints disabled](http://f.cl.ly/items/0N2t3R0R2a232R3w191L/cache-viewer-frontend-nohints.png)
18+
19+
### View Cache Contents
20+
21+
The backend interface, found in `System` » `Cache Management` » `View Cache Contents` in the Administration area, allows
22+
viewing, inspecting or deleting all of the cache entries present in the Magento cache.
23+
24+
![Cache Viewer](http://up.nicksays.co.uk/image/0C1W29041R3y/release_0.1.0.png)
25+
26+
## License
27+
28+
Meanbee_CacheViewer, a Magento extension for inspecting cache contents.
29+
30+
Copyright (C) 2013, Meanbee Limited.
31+
32+
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.
33+
34+
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.
35+
36+
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

app/code/community/Meanbee/CacheViewer/Block/Adminhtml/Report.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
<?php
22
class Meanbee_CacheViewer_Block_Adminhtml_Report extends Mage_Adminhtml_Block_Template
33
{
4+
/**
5+
* @var null
6+
*/
47
public $_total_cache_size_bytes = null;
8+
9+
/**
10+
* @var array
11+
*/
512
public $_cache_items = array();
13+
14+
/**
15+
* Regular expressions used to organise tags into groups.
16+
*
17+
* @var array
18+
*/
619
public $_cache_types = array(
720
"Block HTML" => array(
821
"/BLOCK_HTML$/"
@@ -18,6 +31,9 @@ class Meanbee_CacheViewer_Block_Adminhtml_Report extends Mage_Adminhtml_Block_Te
1831
)
1932
);
2033

34+
/**
35+
*
36+
*/
2137
public function _construct()
2238
{
2339
parent::_construct();
@@ -27,6 +43,9 @@ public function _construct()
2743
}
2844
}
2945

46+
/**
47+
* @return Meanbee_CacheViewer_Model_CacheItemInfo[]
48+
*/
3049
public function getCacheItems()
3150
{
3251
if (count($this->_cache_items) == 0) {
@@ -100,7 +119,7 @@ public function getAsTimeUnits($raw_seconds)
100119
}
101120

102121
/**
103-
* @return int
122+
* @return double
104123
*/
105124
public function getUsedCacheSizeInMb()
106125
{
@@ -112,7 +131,7 @@ public function getUsedCacheSizeInMb()
112131
}
113132

114133
/**
115-
* @return int
134+
* @return double
116135
*/
117136
public function getTotalCacheSizeInMb()
118137
{
@@ -135,6 +154,9 @@ public function getCacheBackend()
135154
return get_class($this->_getCache()->getBackend());
136155
}
137156

157+
/**
158+
* @return array
159+
*/
138160
public function getCacheStatistics()
139161
{
140162
$cache_tag_sizes = array();
@@ -168,4 +190,20 @@ public function getCacheStatistics()
168190

169191
return $cache_tag_sizes;
170192
}
193+
194+
/**
195+
* @return array
196+
*/
197+
public function getCacheStatisticsGoogleChartsFormat()
198+
{
199+
$data_structure = array();
200+
201+
$data_structure[] = array('Cache Type', 'Usage');
202+
203+
foreach ($this->getCacheStatistics() as $name => $value) {
204+
$data_structure[] = array($name, $value);
205+
}
206+
207+
return $data_structure;
208+
}
171209
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
class Meanbee_CacheViewer_Block_Toolbar extends Mage_Core_Block_Template {
4+
5+
public function _toHtml() {
6+
7+
if(!Mage::getStoreConfig(Meanbee_CacheViewer_Helper_Data::XML_PATH_CACHEVIEWER_BLOCKS_VIEW_FRONTEND))
8+
return;
9+
10+
return parent::_toHtml();
11+
}
12+
13+
/**
14+
* Return the configured default cookie path.
15+
*
16+
* @return mixed
17+
*/
18+
protected function getCookiePath() {
19+
return Mage::getSingleton('core/cookie')->getPath();
20+
}
21+
22+
/**
23+
* Return the configured default cookie domain.
24+
*
25+
* @return string
26+
*/
27+
protected function getCookieDomain() {
28+
$domain = Mage::getSingleton('core/cookie')->getDomain();
29+
if (!empty($domain[0]) && ($domain[0] !== '.')) {
30+
$domain = '.'.$domain;
31+
}
32+
return $domain;
33+
}
34+
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
<?php
22

3-
class Meanbee_CacheViewer_Helper_Data extends Mage_Core_Helper_Abstract {}
3+
class Meanbee_CacheViewer_Helper_Data extends Mage_Core_Helper_Abstract {
4+
5+
const XML_PATH_CACHEVIEWER_BLOCKS_VIEW_FRONTEND = "cacheviewer/blocks/view_frontend";
6+
7+
public function isShowCacheStatusOnFrontend() {
8+
return Mage::getStoreConfigFlag(self::XML_PATH_CACHEVIEWER_BLOCKS_VIEW_FRONTEND);
9+
}
10+
11+
}

app/code/community/Meanbee/CacheViewer/Model/CacheItemInfo.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public function getModified()
5050
return new Zend_Date(parent::getModified());
5151
}
5252

53+
/**
54+
* @return Zend_Date
55+
*/
5356
public function getExpires()
5457
{
5558
return new Zend_Date(parent::getExpires());

app/code/community/Meanbee/CacheViewer/Model/DateInterval.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
*/
77
class Meanbee_CacheViewer_Model_DateInterval extends DateInterval {
88

9+
/**
10+
* @param $seconds
11+
* @return Meanbee_CacheViewer_Model_DateInterval
12+
*/
913
public static function createFromSeconds($seconds)
1014
{
1115
if ($seconds > 0) {
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
class Meanbee_CacheViewer_Model_Observer extends Mage_Core_Model_Abstract {
4+
5+
const EVENT_BEFORE = 'core_block_abstract_to_html_before';
6+
const EVENT_AFTER = 'core_block_abstract_to_html_after';
7+
8+
/** @var Meanbee_CacheViewer_Helper_Data */
9+
protected $_helper;
10+
11+
protected $_dispatch_start_time;
12+
13+
public function _construct() {
14+
parent::_construct();
15+
16+
$this->_helper = Mage::helper('cacheviewer');
17+
}
18+
19+
/**
20+
* Add cache block status
21+
* @param Varien_Event_Observer $observer
22+
* @return void
23+
*/
24+
public function addBlockCacheStatuses(Varien_Event_Observer $observer) {
25+
$event = $observer->getEvent();
26+
$block = $event->getBlock();
27+
$transportObject = $event->getTransport();
28+
29+
if (!$this->_helper->isShowCacheStatusOnFrontend()) {
30+
return;
31+
}
32+
33+
if (in_array(get_class($block), array("Mage_Page_Block_Html", "Mage_Adminhtml_Block_Page"))) {
34+
return;
35+
}
36+
37+
// Get cache status
38+
$cache = Mage::app()->getCache()->test(strtoupper($block->getCacheKey()));
39+
$lastModified = date(DATE_ATOM, time());
40+
$blockName = get_class($block);
41+
$cacheClass = "";
42+
43+
44+
// If cached, get last modified date and add cached class for css.
45+
if($cache) {
46+
$lastModified = date(DATE_ATOM, $cache);
47+
$cacheClass = " cacheviewer-block-cached";
48+
}
49+
50+
// Get transport object passed through event and wrap out hints around it.
51+
$html = $transportObject->getHtml();
52+
$html = <<<HTML
53+
<div class="cacheviewer-container clearer">
54+
<div class="cacheviewer-block{$cacheClass}">
55+
<div class="cacheviewer-hints">
56+
<span class="cacheviewer-lastmodified">{$lastModified}</span>
57+
<span class="cacheviewer-name">{$blockName}</span>
58+
</div>
59+
</div>
60+
{$html}
61+
</div>
62+
HTML;
63+
64+
$transportObject->setHtml($html);
65+
66+
return;
67+
}
68+
69+
/**
70+
* Start the timer for tracking dispatch time.
71+
* Observe: controller_action_predispatch
72+
*
73+
* @param Varien_Event_Observer $observer
74+
*/
75+
public function startTimer(Varien_Event_Observer $observer) {
76+
$this->_dispatch_start_time = microtime(true);
77+
}
78+
79+
/**
80+
* Stop the timer tracking dispatch time and set the elapsed time in a cookie.
81+
*
82+
* @param Varien_Event_Observer $observer
83+
*/
84+
public function stopTimer(Varien_Event_Observer $observer) {
85+
$dispatch_finish_time = microtime(true);
86+
87+
$elapsed_time = $dispatch_finish_time - $this->_dispatch_start_time;
88+
89+
if ($this->_helper->isShowCacheStatusOnFrontend()) {
90+
Mage::getSingleton('core/cookie')->set("cacheviewer_dispatch_time", sprintf("%.3fs", $elapsed_time), 0, null, null, null, false);
91+
}
92+
}
93+
}

app/code/community/Meanbee/CacheViewer/controllers/Adminhtml/Meanbee/CacheviewerController.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
<?php
22
class Meanbee_CacheViewer_Adminhtml_Meanbee_CacheviewerController extends Mage_Adminhtml_Controller_Action
33
{
4+
/**
5+
* Route: /admin/meanbee_cacheviewer/report/
6+
*
7+
* Show the cache report.
8+
*/
49
public function reportAction()
510
{
611
$this->loadLayout();
712
$this->getLayout()->getBlock('head')->setTitle("Meanbee CacheViewer");
813
$this->renderLayout();
914
}
1015

16+
/**
17+
* Route: /admin/meanbee_cacheviewer/view_single/
18+
* Params: cache_id
19+
*
20+
* Fetch the value of a given cache key.
21+
*/
1122
public function view_singleAction()
1223
{
1324
$cache_id = $this->getRequest()->getParam('cache_id');
@@ -17,6 +28,12 @@ public function view_singleAction()
1728
$this->getResponse()->setBody(($cache_data) ? Mage::helper('core')->escapeHtml($cache_data) : '(false/empty)');
1829
}
1930

31+
/**
32+
* Route: /admin/meanbee_cacheviewer/delete_single/
33+
* Params: cache_id
34+
*
35+
* Delete a single cache entry.
36+
*/
2037
public function delete_singleAction()
2138
{
2239
$cache_id = $this->getRequest()->getParam('cache_id');

0 commit comments

Comments
 (0)