![]() |
PHP caching system - optimization idea
I was thinking about an effective caching system to reduce server load and DB connections and eventually makes the server traffic bandwidth bigger + faster response. I will be using an easy way to explain my idea.
Such system can be applied only on heavy traffic websites that have hundreds and thousands of page views every minute and same output to all clients not personalized or dynamic. Blogs and news websites are the most to benefit from such system. We will make easy calculations to compare before & after and how much this could reduce the load on the server and increase its traffic bandwidth and eventually servers that could cost thousands of dollars. We will be using PHP script + MySQL + Apache ------------------------- Before: Pageviews: 100/minute Average DB queries / page: 5 -------------------------------- DB queries on 100 visit: 5 x 100 = 500 How does the system work: The system functionality is very simple, its main functionality is to create static pages (html) that were already compiled and don't require any PHP rendering nor SQL connections anymore, now every time a client request a page the HTML page will load. Okay that's great but what if i needed to edit or delete a page... and applying this system on all my website plug ins could take ages to finish. I already thought about this and the system is global, in a meaning it will cache the whole page (from top to bottom) and not part of it so you don't have to make it internal but global and add it to your page headers. Editing the pages is very easy also you will understand it below: Steps:
After: Pageviews: 100/minute Average DB queries / page: 5 -------------------------------- So all SQL queries in 1 minute are simply 5 SQL connections reduced by 100 times, PHP rendering by almost 50% (since we are still loading the caching system on each request) and the response time has greatly increased especially on peak times. The response time has greatly increased since the system has nothing to compile anymore. This can seriously speed up your website like hell + reduce expenses + increase traffic bandwidth. Of course it's not as simple as that and lot of things should be taken into consideration but this is the base idea of the system. This is one of the things I'd like to develop. =) |
[mouse]
Interesting idea. Already proposed many times, but not ideal. I will post a detailed analysis as soon as I get a keyboard. [/mouse] |
this photo explian the caching with php
http://img691.imageshack.us/img691/2108/withcache.png As you can see, the MySQL database and Templates aren't touched, the web server just sends back the contents of a plain .html file to the browser. The request is completed in a fraction of the time, the user gets their page faster, and your server has less load on it... Everyone's Happy. |
I created the caching system last night and currently in use online and its' working very smoothly.
Find the codes below: http://www.copypastecode.com/20949/ Code:
class cache{Quote:
|
Cool script.
Other than that, use something lighter and faster than MD5. MD5 will obviously slow down your script. --- How about a PHP optimization competition? I can prepare one if there will be enough participants. The competition will involve a PHP script written in a bad way that must be optimized in order for it to run faster. So basically, we measure the time taken by each script to run and the person with the fastest script will be the winner. |
Great class im using it, its impressively amazing, reduced the load time dramatically
|
Quote:
However, optimizing an application doesn't stop on the development level. Apache could be tweaked to easily double and triple performance. - Enable "Worker" mode instead of "Prefork" on apache. - Enable Gzip or mod_deflate to compress output content even though this will increase CPU load but will reduce your page size dramatically and eventually make the page load way faster since it's compressed on the server side > sent to client > uncompressed by the client browser and displayed. - Add PHP accelerator like APC along side with the PHP based caching system. - Disable many options from PHP like "magic_quotes" for example. - Use a PHP caching system and make sure it's the very first thing to load on the page, this will reduce PHP rendering to very low levels and totally stop any SQL connections. Refer to the first post in this thread. Or simply go with advanced caching systems such as Memcached (More complicated than regular classes but does wonders, many popular websites such as twitter use it). Tweaking your server and optimizing your application could make your machine handle times more traffic and serve your client at higher speed. |
Quote:
However, this class is made for mass page caching and works great for news and blog websites, but it breaks when you have client interaction where results are supposed to be different for each client and an SQL caching system is required. Therefore i thought about developing a more advanced class where it caches SQL results and pops them up if query encryption matches instead of placing an SQL request each time. for example: // Simple SQL query Code:
select * from users where id = 32- Encrypt it, add client session or id to it "32541_00000ad589174863012860b04aae75cd" let's say and check if it exists in the cache. If it does just load it, if it doesn't: Proceed with the SQL query, get the result > serialize it > cache it. The difference in such caching system and the above is that leaving room for dynamic queries every minute have way more effect than the above system for a simple reason: Each client results of custom queries unlike a news website where a news-load query almost never changes. Therefore the cache should be cleared upon modification (update,delete) only. When the record is updated you simple clear the cache of 32541_* Will post the class when developed. |
| All times are GMT +1. The time now is 01:19 PM. |
Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger