mmCache is a general purpose cache plugin for Wolf CMS. It is useful in caching computationally intensive operations like rendering multi-level menus, which can generate many database requests.
Another case of usage would be fetching some external content from remote server with slow connection.
For obvious reasons, fragment caching should not be applied to any content that contains forms.
These are examples of mmFragment an mmCache usage. You can copy/paste following code into your Pages, Snippets or Layouts.
The random number is cached for 20 seconds in this example.
<?php
// check if there is a cached fragment named 'samplefragment'
// if the fragment exists in cache it will be echoed
// if it doesn't exist the inner clause of 'if' statement
// will be executed
// notice the exclamation mark in if statement
if (! mmFragment::load('samplefragment') ) {
// make some output with random number
echo "Random number to be cached: <strong>" . mt_rand(0, 10000) . "</strong><br/>";
// save fragment for 20 seconds
mmFragment::save(20);
// this text will only show when fragment doesn't exist
// because we've already stored fragment
echo "<em>Cache didn't exist. Saved fragment for 20 seconds</em>";
} else { // fragment was found
// the "else" gets executed if fragment is served from cache
// you can omit this part or you can do this:
// echo "<!-- CACHED -->"
// for debugging hidden from end-user
echo "<em>Above text is served from cache it expires in: " . (mmFragment::getTimeout('samplefragment') - time()) . " seconds</em>";
}
?>
Fragments can be nested. In this example the inner number is cached for 18 seconds and the outer number is cached for 7 seconds. For detalis see the source of snippet.
<?php
if (! mmFragment::load('nestedfragmentouter') ) {
echo "Outer number: <strong>" . mt_rand(0, 10000) . "</strong><br/>";
if (! mmFragment::load('nestedfragmentinner')) {
echo "Inner number: <strong>" . mt_rand(0, 10000) . "</strong><br/>";
mmFragment::save(18); //save inner fragment
}
mmFragment::save(7); //save outer fragment
}
?>
You can cache whole page content as a fragment with key defined as the current URI. For security reasons it's highly recommended to generate md5 (or other) hash of uri. This way you avoid filename collisions / security vulnerabilities.
In your layout put this code at the very beginning:
<?php
$fragmentKey = 'wholePage/' . md5($_SERVER["REQUEST_URI"]);
// or
// $fragmentKey = md5( $this->url() ); // but this way you don't save query strings like ?id=123&p=12 etc.
if (! mmFragment::load($fragmentKey)) {
?>
// here you put your layout contents
at the end of your layout put this code:
<?php
mmFragment::save(3600); //save cache of current page for 3600 seconds
}
?>
In this example we store a variable (array) in cache for 13 seconds. If data is present in Cache it will be assigned to $data during the if-condition examining, if it's not cached we'll execute the code inside if-clause, assign $data values and save it to Cache for 13 seconds. Then $data is displayed regardless of being cached or just-generated.
<?php
// Get the instance of mmCache
$cache = mmCache::getInstance();
// if there is no cached data for 'sampledata' make something
// inside the 'if' clause
if (! $data = $cache->get('sampledata') ) {
// prepare empty array
$data = array();
// random array size
$size = mt_rand(5, 15);
// fill array with random numbers
for ($i = 0; $i < $size; $i++) {
$data[] = 'Random number: ' . mt_rand(1, 1000);
}
// store $data for 13 seconds
$cache->set('sampledata', $data, 13);
}
?>
<pre>
<?php
print_r($data);
echo "<em>This data expires in: ". ($cache->getTimeout('sampledata') - time()) . " seconds</em>";
?>
</pre>
- GPLv3 license
While I make every effort to deliver quality plugins for Wolf CMS, I do not guarantee that they are free from defects. They are provided “as is," and you use it at your own risk. I'll be happy if you notice me of any errors.
I'm not really programmer nor web developer, however I like programming PHP and JavaScript. In fact I'm an architekt.