|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
mankyd Smarty Regular
Joined: 04 May 2006 Posts: 92 Location: Boston MA
|
Posted: Fri Apr 27, 2007 3:45 am Post subject: Cache Explorer |
|
|
Below is some code I've created that basically gives you a read out of what's in your cache, organized by cache_id. It's very rudimentary at the moment and there are still more useful features that can be added (such as retrieving the cache expiration date.) It also doesn't parse cache hierarchies that use sub-directories yet.
Returned is a multidimensional associative array. Every dimension of the array is organized the same:
Code: | Array(
'cache_groups' => Array(
'cache_id_1' => Array( ... ),
'cache_id_2' => Array( ... ),
'cache_id_3' => Array( ... ),
...
),
0 => 'template_1.tpl',
1 => 'template_2.tpl',
2 => 'template_3.tpl,
...
) |
'cache_groups' is itself an associative array where each key is a cache_id pointing to another array of the same structure. The numbered indexes are simply the cached templates stored at the current cache_group.
For instance, if a template named 'test.tpl' is cached with id 'part1|part2|part3' then its name might be found at $cache['cache_groups']['part1']['cache_groups']['part2']['cache_groups']['part3'][0].
I would have liked to do away with the need for the 'cache_groups' key, but there is too much risk of key collisions if someone uses numeric cache_ids. The only other option would be to loop over the directory twice which is inefficient.
Code: | function get_smarty_cache_hierarchy($cache_dir) {
$d = dir($cache_dir);
$cache = array();
while (($entry = $d->read()) !== false) {
if ($entry == '.' || $entry == '..')
continue;
//smarty cache names have three parts
//we care about the first and last part
$sections = explode('%%', $entry);
//handle templates with no cache_id
if (empty($sections[0]))
$cache[] = $sections[2];
//handle templates with a cache_id
else {
//break apart the cache_groups
$ids = explode('^', $sections[0]);
//we use a reference so that we can easily wind our way into the associative array
$cur_cache_id =& $cache;
foreach($ids as $id) {
//skip empty cache_ids
//don't know if this is ok, but it's needed in some form since the last element returned by explode will be empty
if (empty($id))
continue;
if (!isset($cur_cache_id['cache_groups'])) {
$cur_cache_id['cache_groups'] = array();
$cur_cache_id['cache_groups'][$id] = array();
}
else if (!isset($cur_cache_id['cache_groups'][$id]))
$cur_cache_id['cache_groups'][$id] = array();
//move further into our array
$cur_cache_id =& $cur_cache_id['cache_groups'][$id];
}
//append the template name to wherever we ended up
$cur_cache_id[] = $sections[2];
}
}
return $cache;
} |
As long as I was at it, I created a second function that prints out the results of the above function as a nicely formated xhtml list. (As an added challenge to myself, I did it with a stack rather than a recursive function. Yay!):
Code: | function print_smarty_cache($cache) {
$stack = array();
foreach ($cache as $key => $val) {
if ($key === 'cache_groups') {
foreach($val as $cache_id => $cache_groups)
$stack[] = array('depth' => 0, 'id' => $cache_id, 'subs' => $cache_groups);
}
else
$stack[] = array('depth' => 0, 'id' => null, 'subs' => $val);
}
$prev_depth = -1;
do {
$cur = array_shift($stack);
if ($cur['depth'] > $prev_depth)
echo str_repeat("\t", $cur['depth']*2)."<ul>\n";
else {
while ($cur['depth'] < $prev_depth ) {
echo str_repeat("\t", $prev_depth * 2)."</ul>\n";
echo str_repeat("\t", $prev_depth * 2 - 1)."</li>\n";
$prev_depth--;
}
}
echo str_repeat("\t", $cur['depth']*2+1).'<li>';
if(is_array($cur['subs'])) {
echo "<span>{$cur['id']}</span>\n";
foreach ($cur['subs'] as $key => $val) {
if ($key === 'cache_groups') {
foreach($val as $cache_id => $cache_groups) {
array_unshift($stack, array('depth' => $cur['depth'] + 1, 'id' => $cache_id, 'subs' => $cache_groups));
}
}
else
array_unshift($stack, array('depth' => $cur['depth'] + 1, 'id' => null, 'subs' => $val));
}
}
else {
echo $cur['subs'];
echo "</li>\n";
}
$prev_depth = $cur['depth'];
} while (count($stack) != 0);
while (0 < $prev_depth ) {
echo str_repeat("\t", $prev_depth * 2)."</ul>\n";
echo str_repeat("\t", $prev_depth * 2 - 1)."</li>\n";
$prev_depth--;
}
echo '</ul>';
} |
Sample output (abridged from a development version of one of my sites, http://millionnumbers.com):
Code: | # number
* 42
o number.tpl
o header.tpl
* 10
o number.tpl
o header.tpl
* 6
o number.tpl
o header.tpl
* 1
o number.tpl
o header.tpl
* 502505
o number.tpl
o header.tpl
* 696402
o number.tpl
o header.tpl
* 696401
o number.tpl
o header.tpl
# browse
* page
o 5084
+ main.tpl
o 1049
+ main.tpl
o 387
+ main.tpl
o 3562
+ main.tpl
o 6721
+ main.tpl
o 7660
+ main.tpl
o 2645
+ main.tpl
# footer.tpl
# header.tpl
# about.tpl
# index.tpl |
|
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|