1

I have a site that get content from other sites with some JSON and XML API. To prevent loading problems and problems with limitations I do the following:

  1. PHP - Show the cached content with PHP, if any.
  2. PHP - If never cached content, show an empty error page and return 404. (The second time the page loads it will be fine "success 200")
  3. Ajax - If a date field does not exist in the database, or current date is earlier than the stored date, load/add content from API. Add a future date to the database. (This makes the page load fast and the Ajax caches the content AFTER the page is loaded).

I use Ajax just to run the PHP-file. I get the content with PHP.

Questions

  1. Because I cache the content AFTER it was loaded the user will see the old content. Which is the best way to show the NEW content to the user. I'm thinking automatically with Javascript reload the page or message-nag. Other prefered ways?
  2. If I use very many API:s the Ajax loadtime will be long and it's a bigger risk that some error will accur. Is there a clever way of splitting the load?

The second question is the important one.

1
  • Is there any reason why you use JavaScript? A simple, yet effective, solution is memcache. You can easily cache your data and expire it after X seconds. Commented Jul 7, 2011 at 21:21

2 Answers 2

1

Because I cache the content AFTER it was loaded the user will see the old content. Which is the best way to show the NEW content to the user. I'm thinking automatically with Javascript reload the page or message-nag. Other prefered ways?

I don't think you should reload the page via javascript, but just use Jquery's .load(). This way new content is inserted in the DOM without reloading the entire page. Maybe you highlight the newly inserted content be adding some CSS via addClass().

If I use very many API:s the Ajax loadtime will be long and it's a bigger risk that some error will accur. Is there a clever way of splitting the load?

You should not be splitting content in first place. You should try to minimize number of HTTP requests. If possible you should be doing all the API calling offline using some sort of message queue like for example beanstalkd, redis. Also cache the data inside in-memory database like for example redis. You can have a free instance of redis available thanks to http://redistogo.com. To connect to redistogo you should probably use predis

Sign up to request clarification or add additional context in comments.

Comments

1

Why not use the following structure:

AJAX load content.php

And in content.php

  • check if content is loaded. yes > check if date is new. yes > return content
  • there is content, but its older > reload content from external > return content
  • there is no content > reload content from external > return content.

And for your second question. It depends on how often the content of the api's needs to be refreshed. If its daily you could run a script at night (or when there are the littlest people active) to get all new content and then during the day present that content. This way you minimize the calls to external resources during peak hours.

If you have access to multiple servers, the clever way is splitting the load. have each server handle a part of the requests.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.