tags:

views:

84

answers:

3

I have a PHP application that calls web services APIs to get some objects before rendering a web page that incorporates those objects. In some cases these APIs are really slow (seconds) and that is not acceptable from a user experience point of view. Two things I know I can do...

  1. Use ajax and make these calls in the background
  2. Time out the call and degrade gracefully if it is taking too long

Neither is ideal, so I was thinking about using memcache (the PHP extension for memcached) to cache the object that I get from the 3rd party web service. The objects will be loaded many times by different users loading the same page, so this seems to make sense.

The objects are relatively small (~1k).

Does this sound like a reasonable approach? I know memcached was originally designed to alleviate database load, so I'm wondering whether there is a gotcha somewhere that I'm not seeing.

Thanks.

A: 

This approach may not work for you in your situation, but you might use cron jobs to call a PHP script that loads the required information then caches it to a more speedy data source (XML or Database).

This may not work if the information is updated really often or if there is a lot of different data that needs to be loaded, but it is an option. I've used this approach for other tasks that take a lot of time to complete and have found it to be a reasonable solution.

Joe Mills
+5  A: 

This is a perfectly legitimate use of memcache. It is not only for database load reduction, it is for caching and object storage in general. :)

Also note, PHP has two interfaces for memcached. Confusingly, they are named "memcache" and "memcached". Read these to pick between the two:

philfreo
+2  A: 

I'd highly recommend memcache for this situation as it will:

  1. Reduce DNS calls.
  2. Reduce page latency.
  3. Reduce bandwidth usage.

Your only real task is to determine how often the data you are dealing with will be changing. This will help you to optimize your expiry time for the cache key(s).

cballou