Skip to content Skip to sidebar Skip to footer

Different File Caching Methods, Pros & Cons

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

Solution 1:

Performance tuning through cachine could be categorized into multi-layers:

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:

  1. 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.
  2. Cache the full page for two-hours using this simple Pear library PEAR::Cache_Lite.
  3. 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:

  1. Shared memory caching between servers. Check Memecache or even Membase
  2. 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.
  3. Mysql innoDB engine is slow, you may need to go for faster engine such as XtraDB
  4. 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:

  1. Front-end Performance: High Performance Web Sites, Even Faster Web Sites and High Performance JavaScript.
  2. Back-end Performance: Pro PHP Application Performance and High Performance MySQL

Solution 2:

Well caching is such a broad spectrum you really should be a bit more specific.

For example, if you're looking to lower the load on the server, you would want to cache the PHP files using APC (For example) [lowers disk reads of files].. or use memcache/redis/some other in-memory key-value store for relieving stress from your database server (application level caching).

If we're talking about the static files, there are a number of things you could do to gain network speed:

  1. Make sure the caching headers returned from the server are correct and that those files are cached in the client (for as long as you need/want). (clients get more responsive sites, you get less server load - but you'll still get hits on which you'll return a 304 not modified)

  2. If you're using Apache+mod_php... apache will start a php interpeter even for requests that are for static content (css, js). While if you place nginx before, those could be cache by the http server itself - much faster, Alternatively, go to step 3 (below)

  3. You could put Varnish infront of your entire(/both) websites for static content/semi-static content.

Another common "micro"-optimization... this usually effects bigger sites.. nothing i would worry about with your ~20K.. but if you want, is move the static files to a differnet domain such as some-university-static.com (not a subdomain).. that way cookies headers aren't sent with the static file's request, resulting in less incoming bandwidth and fast response for the user. (smaller request sent-fast it gets to the destination-faster it returns)

Home this gave you some initial pointers to look into.

Ken.

Solution 3:

For the .js and .css files, you can simply use expires HTTP headers, which will cause browsers to cache them.

As for the .php, there are several options.

You can use memcache for specific things, for example if you're loading the same list of users over and over again from the database, and you cache the result with a specific expire time, e.g. 2 hours.

Or you can use reverse proxy such as varnish to to cache away a whole static html page, generated from the .php script

Post a Comment for "Different File Caching Methods, Pros & Cons"