Reputation: 4537
I'v been running my own blog system for a while, and I noticed that for most of the pages, several (5-10) same databse queries need to be performed everytime the page gets visited.
Now I'm thinking of caching data into a xml file. Here's my plan of doing this:
--- When posting/editing a blog, insert/update data into MySQL database, and generate the xml file
<?xml version="1.0" encoding="UTF-8"?>
<data>
<id>32</id>
<title>caching data with xml</title>
<date>2012-10-01</date>
<content><![CDATA[<p>blah...blah...blah...</p>]]></content>
</data>
--- When the page gets visited, check for file existence first (just in case), and then parse the xml file and format/output data.
if(file_exists("path/to/blog/32.xml")) {
$data = simplexml_load_file("path/to/blog/32.xml");
echo '<h2'>.$data->title.'</h2>';
echo '<p'>.$data->date.'</p>';
echo $data->content;
}
else {
mysql_query(...);
}
By doing this MySQL could do much less work, but I'm not quite sure if this is gonna cause any problems later, like 100 or 300 visitors are visiting the same page at the same time. Can PHP handle that? Am I doing it right with this method?
Thank in advance for any info and tips.
BTW, I'm not thinking of using those templates yet.
Upvotes: 0
Views: 518
Reputation: 9568
XAP 9.1 which should be released tomorrow includes exactly this capability.
We realized the need to cache XML data and query it using XPath so we added a new native XML API.
[disclaimer I work in GigaSpaces]
Upvotes: 0
Reputation: 2388
Generally talking caching is always good idea when possible and I suppose your xml solution wont cause much performance problems. But you can consider using Memcache instead (which could be more handy).
If you dont want to use Memcache for some reason but XML caching and you are not sure if it would harm or improve performance you could simply benchmark your SQL queries and your caching solution and then compare.
However it sounds to me like premature optimization. Does your blog really get 300 visitors at the same time or it is some imaginary case?
Upvotes: 1
Reputation: 588
I believe that memcached can do something like this automatically, I'd try to reuse what's already there instead of reinventing the wheel..
Upvotes: 0