Tag Archives: Cache

Cache Data with the WordPress Transients API

WP Engineer had an interesting post recently about a WordPress “Transients API” that is used for caching bits of data temporarily. I often use the Options API to cache things from external servers, such as Twitter statuses, so I don’t hit Twitter’s servers more often than necessary (which would slow down page loads). The Transients API is similar, but with the addition of an expiration field. This makes it a much easier solution, even without its other added benefit.

Also of note is that Transients are inherently sped up by caching plugins, where normal options are not. A memcached plugin, for example, would make WordPress store transient values in fast memory instead of in the database. For this reason, transients should be used to store any data that is expected to expire, or which can expire at any time.

It’s simple enough to use the API. You just call the set_transient(), get_transient() and delete_transient() functions where appropriate. You can read up on the usage over at WP Engineer or the WordPress Codex.

Preventing the Caching of Dynamic Functions in WP Super Cache

WP Super Cache is commonly used to speed up WordPress blogs and reduce server load. In essence, it stores static HTML copies of pages on your blog so they will load faster the next time they are accessed. It works fairly well, but with one caveat that may be frustrating at times: The pages don’t change until the cache expires (every 30-60 minutes or so) and the static page is updated. That makes it hard to do some things where you need to process information unique to each user, such as checking HTTP referrer headers and serving ads to visitors coming from search engines.

Fortunately, there is a way around it in some cases. If you have a function in your template, you can do something like this:

<!--mfunc function_name( 'parameter', 'another_parameter' ) -->
 <?php function_name( 'parameter', 'another_parameter' ) ?>

You take a function call and surround it by the “mfunc” comments, in which the first contains a duplicate of the function. This will (somehow) instruct WP Super Cache to allow the function to execute, even in the static cached version of the page.

Beyond Super Cache: W3 Total Cache

Donncha O Caoimh’s WP Super Cache plugin has become very popular in the WordPress community, especially with bloggers with medium-traffic blogs on shared hosting plans.

But what if you’re running on your own server, be it VPS, dedicated, or something else along those lines? What can you do to squeeze some extra performance out of your high-traffic blog?

Enter W3 Total Cache, a plugin that the infamously slow-loading blog Noupe has recently started using to combat the sluggishness that their constant social media hits cause. It can do a lot of things, including:

Continue reading →

Saving Bandwidth and Speeding Up Your Site With GZIP and Browser Caching

There are a couple of easy adjustments you can make to your web server in order to decrease page loading times, save bandwidth, and reduce load on the server. All you have to do is add a couple of code snippets to either your Apache server configuration file (httpd.conf or apache2.conf) or an .htaccess file.

Note that these require that your server have certain modules installed for this to work. You will need either mod_deflate or mod_gzip for GZIP compression and mod_expires for the browser caching trick.

Enable Browser Caching

When a web browser loads a page, it checks each item it requests (JavaScript, CSS, images, etc) against its local cache. If an item, say the stylesheet, hasn’t expired yet, then it will load the local copy instead of requesting a new one. Now if you were to instruct your server to set the expiration time for images, CSS, and JavaScript files to one month from the present, users viewing multiple pages of your site (even across multiple days) won’t tax your resources as much, as they will use the copies of your stylesheets and images that have already been downloaded.

Continue reading →

Easy PHP Caching: Speed-Up Dynamic Content

Caching dynamic content can save a lot of proceesing power, potentially saving a server from total meltdown under extremely high traffic loads. The popular WP Super Cache plugin has demonstrated this, helping blogs on small shared hosts survive the “Digg Effect” longer.

If a script’s output doesn’t change every time a page is loaded, does it need to be processed each time? Probably not. You can cache a page, or a section of a page, for an appropriate amount of time, and serve it up instead.

It’s a lot simpler to implement caching than you would think. It’s just a matter of using output buffering, a little bit of basic file I/O, and watching the server’s timestamp.

PaperMashup.com has a short article on how to apply a basic cache to your scripts.

It’s not a good idea to go away and cache your entire site, you need to think about which pages receive high traffic, and which pages make a number of database requests. Static HTML pages aren’t going to see a benefit from caching and may in fact be served slower due to PHP invoking the request to the cached version.

On WP Super Cache

Everyone’s been talking about WP Super Cache, the new WordPress plugin based off the old WP-Cache 2. By storing static copies of your posts for a predetermined amount of time (then refreshing them after they expire), it reduces server load by a huge margin, which hopefully keeps your site from going down if you get “Dugg.”

It’s a great idea, but I’m not about to install it yet. As it says on the website, “dynamic content such as that within the sidebar, will only refresh when the cached pages are refreshed.” That, of course will cause major problems with some plugins. It’s hard to explain why those plugins won’t work, but I can give you some examples.

Continue reading →