[eluser]Yorick Peterse[/eluser]
After almost 6 hours of thinking about a way to get partial caching, the advanced version, working in CodeIgniter I came up with an idea. The problem was that CI's native cache system was too limited, it caches either everything or nothing at all. This is fine for normal websites, but as soon as you need dynamic content inside cached files, such as usernames, this isn't going to work.
Imagine logging in with the username "Rubber Duck", now everybody else will see a message saying "Welcome Rubber Duck" because
everything is cached, even the things that shouldn't.
Partial caching is something that fixes this problem. MP_Cache is a library that does this in a very nice way, the only prolbem is that it's really, really buggy. The second problem is that it doesn't enable the developer to include dynamic content inside cache files. For example, the following piece of code will not work with cache files :
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<p>Welcone <?php echo $username; ?></p>
</body>
</html>
If somebody logged in with the name "Rubber Duck", everybody will now see "Welcome Rubber Duck", rather than their own username. This
can be fixed by splitting the file up in seperate blocks. In this case we could split this file into three parts: header,body and footer. In this case we would only cache the header and the footer.
Again, this wasn't exactly what I wanted since I'm not going to split an relatively easy to understand template file into 3 hard to understand PHP files.
So how do we fix the problem ?
I couldn't come up with any ideas until about 30 minutes ago, and I just smacked myself in the face for thinking so complicated, the solution has been there ever since people came up with functions to replace content.
Let's take a look at the file shown earlier, using a regular cache library or method isn't going to work, but what if we replace this
Code:
<?php echo $username; ?>
with this
It may look simple, but by doing this you've already solved 50% of the problem, the other 50% can be solved by doing the following inside one of your PHP files :
Code:
// The username (or any dynamic variable)
$username = $this->session->userdata('username');
// Get the file show earlier
$file = $this->load->view('file_i_showed_earlier','',true);
// Replace {USENAME} with the dynamic username, probably faster to use preg_replace()
$results = str_replace('{USERNAME}',$username,$file);
// Output the results
echo $results;
This will result in a cached page with dynamic content. In this example I used pseudo code wrapper in curly brackets, but you could use anything as long as it isn't real PHP code (since this doesn't work).
I am aware that there's already a library out there that does it (Sparks), but it hasn't been updated for the past 2 years. Besides that it uses templates and some other stupid stuff that shouldn't be required for a cache system.
If you have any ideas on how to do this in a better way, feel free to post a comment