Building a Template Parser Class with PHP, Part II - One step forward: defining caching methods (
Page 2 of 3 )
The first question that came to my mind when I decided to implement a caching mechanism was: is it really worth having those capabilities? The answer was a resounding yes! Let’s think about it. If we’re working with websites that don’t change their content very often, say on a daily basis, it’s highly desirable to have a caching system that simply reads the contents to be displayed from a flat file, and delivers them directly to the browser, without the need to process a template file each time the requested page has to be rendered. That sounds like common sense..
With this conclusion firmly in mind, I quickly started implementing those desirable caching capabilities inside the class. To keep things ordered, I defined two class methods for reading and retrieving data from a specific cache file, specifying a time-based cache validity policy. The first caching method, "readCache()" simply reads data from a cache file, and is defined in the following way:
function readCache($cacheFile,$expireTime){
if(file_exists($cacheFile)&&filemtime($cacheFile)>
(time()-$expireTime)){
return file_get_contents($cacheFile);
}
return false;
}
As you can appreciate, the above method takes two parameters: the cache file and time expiry, respectively, allowing us to specify a value expressed in seconds for setting the proper cache validness.
The method checks simultaneously if there is a cache file in the system and that the time expiry has not been reached. Accordingly, if the cache file exists and is valid, the contents from the file are returned. Otherwise, the method returns false. Certainly, you’ll agree with me that this is not rocket science.
Now, let’s take a look at the next method, "writeCache()", which, not surprisingly writes contents to a specified cache file:
function writeCache($cacheFile,$content){
$fp=fopen($cacheFile,'w');
fwrite($fp,$content);
fclose($fp);
}
On the opposite side, this method accepts a cache file name, and the contents to be written on it as parameters. It naturally opens the file, writes the contents (overriding any previous values) and closes the file. The method is straightforward and easy to understand.
At this point, we’re armed with two methods to read and write cache files in an efficient manner. It’s time place them into the class puzzle.
| | Discuss Building a Template Parser Class with PHP, Part II | | | | | | | In this second part, the class is expanded to add a basic caching mechanism.
Since... | | | | | | Only for aclaration, there is a typo. Where it says witeCache(), should read... | | | | | | We fixed the error, thank you for pointing it out. | | | | | | You're very welcome:-)
Thank you.
| | | | | | Thanks for the great article! As someone pointed out in the comments for Part 1 of... | | | | | | I was hoping that in Part II you might discuss a strategy to parse tags recursively,... | | | | | | This class really rocks. I will use it for sure. Thanks man.
I simply hate... | | | | | | Thank you for the comments.
Well, Reagarding your question about dealing with... | | | | | | Hey, thank you for the kind words about the class.
Regarding your opinion about... | | | | | | Thanks for the comments.
There are several ways to expand the class and add more... | | | | | | I have used Smarty exclusively for the past half dozen projects I have been involved... | | | | | | Thanks a lot for the comments about the class. If this application has been a source... | | | | | | I agree with you and with the author. Smarty has it's place for sure. I also use... | | | | | | Hey, I'm glad you liked the class and I really appreciate your words.
With no doubt... | | | | | | It would be great to see another part to this!
Way to go on I and II. | | | | | | I'll see if I can give a try writing Part III soon.
Thank you. | | | | | | Good article on using a template, but I have a question. I'm doing a similar thing,... | | | | | | Thank you for the kind words about the article. Regarding your question, the answer... | | | | | | I'm a bit confused on how this gets implemented. I see three possible PHP/HTML... | | | | | | There are several ways to generate multiple pages using a template file. With a... | | | | | | I've experienced an issue with this class that I'm not sure how to truly resolve. ... | | | | | | I think that your problem is rather easy to solve. Here's a possible solution:
In... | | | | | | hi,
I'd like to start out by saying what great articles you've made. I've been... | | | | | | Hi friend,
Thank you for the compliments. They're really important.
Regarding... | | | | | | Hmm, I think I see what you're saying. It's a different approach than I was taking... | | | | | | If you want to keep in your template file as much HTML markup as possible, just... | | | | | | Not that I condone script errors, but just curious how you would prevent error... | | | | | | Hello,
About your question, I'll try to give you a general answer, since error... | | | | | | Hey That would be nice part of the class in article 3! | | | | | | Yes, the subject might be covered on a third part. However, I think that all of the... | | | | | | Nice atricles. When I try to use te cache version there is a problem with write... | | | | | | when will be the released of your part III>>> | | | | | | based on
"With this single load.php file you'll generate all of your website pages.... | | | | | | Thank you for commenting on my PHP article. Now, when all the pages are built from... | | | | | | >>> Post your comment now! | | | | | |
|
 |