1

I'm currently working on a site that makes several calls to big name online sellers like eBay and Amazon to fetch prices for certain items. The issue is, currently it takes a few seconds (as far as I can tell, this time is from making the calls) to load the results, which I'd like to be more instant (~10 seconds is too much in my opinion).

I've already cached other information that I need to fetch, but that information is static. Is there a way that I can cache the prices but update them only when needed? The code is in Python and I store info in a mySQL database.

I was thinking of somehow using chron or something along that lines to update it every so often, but it would be nice if there was a simpler and less intense approach to this problem.

Thanks!

Parker
  • 8,539
  • 10
  • 69
  • 98

4 Answers4

3

You can use memcache to do the caching. The first request will be slow, but the remaining requests should be instant. You'll want a cron job to keep it up to date though. More caching info here: Good examples of python-memcache (memcached) being used in Python?

Community
  • 1
  • 1
Matt Williamson
  • 39,165
  • 10
  • 64
  • 72
  • 2
    You don't really need a cronjob. A simpler solution is probably setting an expiration, and then calling the store if you can't find it in memcache. – Matthew Flaschen Nov 17 '10 at 20:53
  • Thanks Matt! (Both of you!) Is there a way to call it in the background, so the user doesn't have to wait? – Parker Nov 17 '10 at 21:11
  • You want to queue it up and check in the background instead of doing a cron job? Depending on what platform you are on, you may want to try Celery (http://ask.github.com/celery/getting-started/introduction.html) or make some custom threading – Matt Williamson Nov 17 '10 at 21:36
  • @Matthew Flaschen, the only issue is we'd probably only want to delete the memcache record if the price has changed, not just after some period of time. – Matt Williamson Nov 17 '10 at 21:39
  • 1
    There's no way to know whether the price has changed without checking. Expiration is one way of ensuring that you check. – Matthew Flaschen Nov 17 '10 at 21:41
  • Yes your right, but we don't want to invalidate the cache key if it has not changed. – Matt Williamson Nov 18 '10 at 22:36
2

Have you thought about displaying the cached data, then updating the prices via an ajax callback? You could notify the user if the price changed with a SO type notification bar or similar.

This way the users get results immediately, and updated prices when they are available.

Edit

You can use jquery:

Assume you have a script names getPrices.php that returns a json array of the id of the item, and it's price.

No error handling etc here, just to give you an idea

 My necklace: <div id='1'> $123.50 </div><br>
 My bracelet: <div id='1'> $13.50 </div><br>
 ...

 <script>
 $(document).ready(function() {
   $.ajax({ url: "getPrices.php", context: document.body, success: function(data){
      for (var price in data)
      {
          $(price.id).html(price.price);
      }
    }}));
 </script>
Byron Whitlock
  • 52,691
  • 28
  • 123
  • 168
2

You need to handle the following in your application:

  1. get the price
  2. determine if the price has changed
  3. cache the price information

For step 1, you need to consider how often the item prices will change. I would go with your instinct to set a Cron job for a process which will check for new prices on the items (or on sets of items) at set intervals. This is trivial at small scale, but when you have tens of thousands of items the architecture of this system will become very important.

To avoid delays in page load, try to get the information ahead of time as much as possible. I don't know your use case, but it'd be good to prefetch the information as much as possible and avoid having the user wait for an asynchronous JavaScript call to complete.

For step 2, if it's a new item or if the price has changed, update the information in your caching system.

For step 3, you need to determine the best caching system for your needs. As others have suggested, memcached is a possible solution. There are a variety of "NoSQL" databases you could check out, or even cache the results in MySQL.

calvinf
  • 3,754
  • 3
  • 28
  • 41
0

How are you getting the price? If you are scrapping the data from the normal HTML page using a tool such as BeautifulSoup, that may be slowing down the round-trip time. In this case, it might help to compute a fast checksum (such as MD5) from the page to see if it has changed, before parsing it. If you are using a API which gives a short XML version of the price, this is probably not an issue.

  • Hey Andy! I used to use BS, but I switched over to lxml after hearing about the speed issues. The site is BlueDevilBooks.com , and it doesn;t show that many sites, so I can;t imagine that parsing could really take up that much time, right? I'll double check cProfile again though to see what the issue is. And thank you for the checksum idea! – Parker Nov 19 '10 at 01:46