- I have two websites that will share some resources, lets say, index.php, functions.js and style.css, and these scripts will be used on almost all pages on the websites.
- I have two audiences to cater for (in terms of download speed), the users within the same network that the sites hosted on (100mb/s aprx) and external users.
I am looking for the best way to cache each kind of script (.js, .css, .php) and examples of how this would be done with their pros and cons over other methods if possible. By caching I mean locally, network and server caching.
Note: index.php is a dynamic page which should be refreshed from cache every 2 hours. It would be nice if you start your answer with .js, .css, .php or a combination so I can easily see what type of script you are talking about caching.
Thanks All!
Performance tuning through cachine could be categorized into multi-layers:
- Client-side (JS and CSS): Add an Expires or a Cache-Control Header will get it done for you. But note that there are more to do than only caching to enhance client-side performance. For detailes check Best Practices for Speeding Up Your Web Site
- Server-side: this could be on many levels web server, scripting language, database, operating system, network, etc..
Good introduction and practical code examples can be found in Chapter 9 (Performance) - Developing Large Web Applications. It will talk about Caching CSS, Javascript, Modules, Pages, Ajax and Expire headers.
If we need to keep things simple on server-side do the following:
- Install APC extension which will make PHP faster for you through the so called opcode caching. No special configuration, it will work silently for you.
- Cache the full page for two-hours using this simple Pear library PEAR::Cache_Lite.
- For each database SELECT query cache the result in APC with a TTL of 5 Min, md5 hash the SELECT statement and use it as key for APC cache. Docs
In future if you have multiple servers and the performance becomes to be crucial before then you will need to look at:
- Shared memory caching between servers. Check Memecache or even Membase
- You need a reverse proxy solution: this basically layer between your user and server server so that it will serve the HTTP requests instead of your server. You can use for that Varnish, Squid or Apache Traffic Server.
- Mysql innoDB engine is slow, you may need to go for faster engine such as XtraDB
- Then maybe you will find that rational databases are stil slow for you. Then you will go for the key-value solution such as MongoDB.
Finally as references in web application performance check:
- Front-end Performance: High Performance Web Sites, Even Faster Web Sites and High Performance JavaScript.
- Back-end Performance: Pro PHP Application Performance and High Performance MySQL