1

I'm wondering if it's worth caching a result that is only going to change every couple of hours but is shown on every page of the website. Makes sense to me to do it but just wanting a second or third opinion.

The situation is that on each page there is 2 top 5 lists. These do change but only really need to be updated every hour for example. The result of this top 5 is found through a MySQL query. Would it be a smart idea to cache these results? What would be the best way to cache them? (Using PHP).

I've had a look through some of the questions asked here but they don't relate well enough so thought it'd be best to ask again.

Thanks.

5
  • 1
    Do you want to cache the results in plain php or you are allowed to use APC, memcached for example? Commented Jan 17, 2013 at 3:06
  • 1
    I cache the results by memcache Commented Jan 17, 2013 at 3:06
  • It's not content that needs to be secured, if that's what you're asking. Will look into the both. Commented Jan 17, 2013 at 3:07
  • 1
    No, I was asking if you're allowed to install addons/applications on your server. For example, if you can install APC or Memcache, it's better to use those engines to cache your data. If you don't have access to the hosting server and everything is "as is", you can cache the data in a php file and then read from it. Commented Jan 17, 2013 at 3:09
  • Oh ok. Will look into it. EDIT: Looks like it is not an option. Commented Jan 17, 2013 at 3:12

1 Answer 1

3

Your use case is precisely the reason that caching was developed! The most common pattern is to use a caching service, such as memcached. (see Caching with PHP to take stress away from MySQL)

However, I would encourage you to think about whether or not this is a premature optimization. Is your database unable to cope with the requests? Are all pages loading slower than you need? Further, are these database queries slow for some reason? Are you properly using indexes?

I would encourage you to think only add a cache when you need to, and to ensure you have optimized as best as possible before then.

Checkout https://stackoverflow.com/a/1600252/1058853 for a good commentary.

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

2 Comments

I'm not doing it because of slow loads or anything like that, just thought it might not be a bad idea to do it sooner rather than later etc.
So, your suggestion would be to leave it until there is a specific need for it?

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.