How to cache partial content with PHP
I recently come to a problem where i needed a cache part of the result to speed up the execution of statistics on a client page. The solution I was looking for was simple, I just needed something to partially cache the output and save it to static html-files. Looking around I found many different solution, but they were either to big (depending on a template system in use) or relied on Memcache or APC for controlling the output.
Instead I started looking at the built in function for output-buffering in PHP, which provides a simple way to store everything otherwise written to the client. When I couldn't find any existing solution which met my needs, I decided to create my own Partial PHP Cache system. The key features are:
- Simple to install, single class
- Works with minimal interruption with existing functionality
- Caches partial content into static html-files
Example implementation:
<?php $cache = new PartialCache("statistics", $userid); if ($cache->start()): // Database query.... ?> <h1>Result</h1> <?php echo $result;<?> <?php endif; $cache->render(); ?>
Everything between start and render will be cached, when rendering the result the content will either be read from the cache or if cache is missing the output will be saved for next time, when the same userid visits the page (could be changed to IP, country etc. to suit your needs). In my example the cache will also be refreshed every day based on the file age.
- Download the source code at github
- Or if you know Swedish; read a more in depth explanation on "Hur cachar man delar av sidan i php för att snabba upp sidvisningar".
Next name for a Volvo model: Optimus Prime
The original Optimus Prime transforms into a cab over truck. The truck's cab transforms into the robot mode of Prime himself, with vast strength and armed with a laser rifle.
Picture by RoninKengo
Providing a Volvo with a laser rifle might be ethically questionable, but run-time transformation, that's a feature worth exploring, isn't it? Volvo Cars, and many car manufacturers with them, are increasingly dependent on software to meet customer demands, improve safety, and reduce carbon emissions. Many employ software product lines to derive similar products from a core set of components. For example, despite what you might think, each new Volvo car ordered today is manufactured according to your preferences. As Jan Bosch explains in his book "Design and use of software architectures: adopting and evolving a product-line approach" being able to manage variations is perhaps the single most important aspect in such software product line architectures. However, with the ever growing complexity of requirements, variation patterns used in the automotive industry today are insufficient. We need to look further (perhaps to Optimus Prime?) to find suitable solutions.
In our thesis, we argue for a run-time variation mechanism as one way of meeting the increased configuration demands. Using an adaptation of the publisher-subscriber pattern, we demonstrate in a prototype compliant to AUTOSAR (a growing industry standard), how variations in software can be managed in run-time, i.e while the car is running. Moreover, we provide a number of technical insights but I won't go through all here. You're better off downloading our article and reading the complete story there.
All in all, it has been a fun and challenging (and at times boring) work to do, but we're pretty proud and satisfied with the result. While there is some more research to be done before Volvo names their next car model to Optimus Prime, I think it's safe to say that it did provide some useful inspiration during our work.
Psst! The report has diagrams and tables too