[php] <pre><!--?php // Fake Blogging Engine include('lib/blog.php');include('template/header.php');$post_id = intval($_GET['post_id']); $cat_id = intval($_GET['cat_id']); if($post_id) { load_post($post_id); } else { list_posts(intval($_GET['cat_id'])); } include('template/footer.php'); [/php]
[php] <!--?phpfunction load_post($post_id) { $db = new mysqli('localhost','username','password','blog'); /* ... snip error checking ... */ $query = $db--->prepare('select content, title, post_date, author from posts where id=?'); $query->bind_param('i', $post_id); $query->execute(); $query->bind_result($content, $title, $post_date, $author); if(!$query->fetch()){ /* ... Snip Error handling ... */ } $query->close(); $db->close(); ?> <h1><!--?php echo $title ?--></h1> <h2>Written by <!--?php echo $author ?--> on <!--?php echo $post_date ?--></h2> <div class="blog-post"> <!--?php echo $content ?--></div> <!--?php } function list_posts($cat_id=null) { $db = new mysqli('localhost','username','password','blog'); /* ... snip error checking ... */ if(!$cat_id){ $query = $db--->prepare('select id, title, post_date, author from posts order by post_date desc limit 10'); } else { $query = $db->prepare('select id, title, post_date, author from posts where category_id=? order by post_date desc limit 10'); $query->bind_param('i',$cat_id); } $query->execute(); $query->bind_result($post_id, $title, $post_date, $author); while($query->fetch()){ ?> <h1><a href="?post_id=<?php echo $post_id; ?>"><!--?php echo $title; ?--></a></h1> <h2>Written by <!--?php echo $author; ?--> on <!--?php echo $post_date ?--></h2> <!--?php } $query--->close(); $db->close(); } [/php]
[php htmlscript="true"]Blog Title!<!-- .. Snip other header elements .. --> <h1><a href="?home">Home</a></h1> <ul class="navigation"> <!--?php $db = new mysqli('localhost','username','password','blog'); $query = $db--->prepare('select cat_name, id from categories order by cat_order asc'); $query->execute(); $query->bind_result($cat_name, $cat_id); while($query->fetch()){ ?> <li><a href="?cat_id=<?php echo $cat_id; ?>"><!--?php echo $cat_name; ?--></a></li> <!--?php } $query--->close(); $db->close(); ?></ul> <div class="blog"> [/php]
[php htmlscript="true"] <!-- END div.blog --> <ul class="footer"> <!--?php $db = new mysqli('localhost','username','password','blog'); $query = $db--->prepare('select link from footer order by footer_order asc'); $query->execute(); $query->bind_result($link); while($query->fetch()){ ?> <li><!--?php echo $link; ?--></li> <!--?php } $query--->close(); $db->close(); ?></ul> [/php]
[php] $_cache_id = array(); function cache_start($id){ $filename = cache_get_filename($id);global $_cache_id; array_push($_cache_id, $id); ob_start(); //Turn on output buffering } function cache_stop(){ global $_cache_id; $_id = array_pop($_cache_id); if($_id===null){ /*... snip error handing code...*/ } //The in-memory version of what generate_some_page produced $page_to_cache = ob_get_contents(); file_put_contents( cache_get_filename($_id), $page_to_cache);ob_end_flush(); //Write in-memory version out to the client } function cache_get_filename($id) { return sys_get_temp_dir() . '/cache/c_' . preg_replace('/[\\|&|/|^|:|?|<|>|*|]/','',$id) . '.cache'; } [/php]
[php highlight="7,9,22,24"] <!--?php // Fake Blogging Engine // Load the functions for reading blog posts include('lib/blog.php'); include('lib/cache.php');cache_start('header'); include('template/header.php'); cache_stop();// Get requested blog post, or if none specified get homepage (post_id===0) $post_id = intval($_GET['post_id']); $cat_id = intval($_GET['cat_id']); $cache_id = ($post_id? 'page'. $post_id: ($cat_id? 'category'.$cat_id:'homepage')); if($post_id) { load_post($post_id); } else { list_posts(intval($_GET['cat_id'])); } cache_start('footer'); include('template/footer.php'); cache_stop(); [/php]
The code changes were easy to implement and didn’t create much clutter. The header is now cached and our performance should skyrocket! ::Dramatic Pause:: Not so fast! The cached header is being saved, but nothing is being done with it. The caching code needs to be altered a bit further to serve up the file if it exists and isn’t too old; it’s but a simple addition to the original caching code.
[php highlight="6,7,8,9,13"] <!--?php$_cache_id = array(); function cache_start($id){ $filename = cache_get_filename($id); if(file_exists($filename)){ include($filename); return true; } global $_cache_id; array_push($_cache_id, $id); ob_start(); //Turn on output buffering return false; } function cache_stop(){ global $_cache_id; $_id = array_pop($_cache_id); if($_id===null){ /*... snip error handing code...*/ } //The in-memory version of what generate_some_page produced $page_to_cache = ob_get_contents(); file_put_contents( cache_get_filename($_id), $page_to_cache);ob_end_flush(); //Write in-memory version out to the client } function cache_get_filename($id) { return sys_get_temp_dir() . '/cache/c_' . preg_replace('/[\\|&|/|^|:|?|<|-->|*|]/','',$id) . '.cache'; } [/php]
[php highlight="7,10,22,26"] <!--?php // Fake Blogging Engine // Load the functions for reading blog posts include('lib/blog.php'); include('lib/cache.php'); // Load the header content if(!cache_start('header')){ include('template/header.php'); cache_stop(); } // Get requested blog post, or if none specified get homepage (post_id===0) $post_id = intval($_GET['post_id']); $cat_id = intval($_GET['cat_id']);if($post_id) { load_post($post_id); } else { list_posts(intval($_GET['cat_id'])); }if(!cache_start('footer')){ // Load the footer content include('template/footer.php'); cache_stop(); } ?--> [/php]
Latency | CPU Resources | Network Traffic | Total | |
---|---|---|---|---|
No Cache - 1st Req | ||||
No Cache - Subsequent | ||||
Cache - 1st Req | ||||
Cache - Subsequent |
Lorem ipsum dolor sit amet, consectetur adipiscing elit
For the past two decades, we've made it our business to help you work smarter. From commerce challenges to ERP customizations, we support the power of your big ideas by helping you work more strategically, more intuitively, and more efficiently.
2658 Scranton Road, Suite 3
Cleveland, Ohio 44113
216.369.3600
No Comments Yet
Let us know what you think