1

I have a main page in HTML which makes some Ajax calls (about 10) to a PHP file.

The PHP gets some data from different websites (via file_get_contents) and outputs it as JSON. The average length of the output is 4000 characters. To not overload the number of request that my server makes to the websites, I would like to cache the response of same queries for something like 30-60 minutes.

The HTML main page contains a search input, so basically anything can be written and I may need to have a lot of different cache files. In this case, which method is more efficient, database storing (MySQL) or file?

4
  • 4
    Database is faster than disk. Redis or memcache is faster than database. Commented Jun 13, 2016 at 13:53
  • 1
    If you need to implement cache duration seriously consider using Redis which has this functionality built-in rather than re-inventing the wheel. Commented Jun 13, 2016 at 14:00
  • @jszobody Reading a file would definitely be faster than querying a database assuming the disk speeds are equivalent. Especially if the server has plenty of free RAM for page cache. However, for scalability's sake, redis or memcache would be ideal. Commented Jun 13, 2016 at 14:09
  • @Devon yeah, I suppose if the OP is storing a single file for each search term, and never needs to do a partial match or query the contents of the cache data... filesystem might work well. Pretty limiting, and will obviously be a performance issue if OP ever wants to do more than that. Commented Jun 13, 2016 at 14:12

1 Answer 1

1

An important factor that hasnt been mentioned yet is the traffic on your website, caching has no use if you serve a handful of visitors an hour. Since you didnt specify it I assume you serve enough users that makes you consider caching.

In that case it will depend on your database speed, if you have a simple SELECT query and a relative small database I would not consider file/disk caching. Most likely running your query directly is faster than opening a file. If you have a complex query and a big database, we have 1 at work which is 100+ gigs big, than I would expect a file caching is better.

Memcache is fastest but it has limited storage, so best practise is to only store the most important things there that you want to have accessible on the fly.

Always add timers to your script and test the speed of multiple caching solutions against eachother.

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

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.