0

I am planning to implement a caching system for my site that receives decent traffic. Sometimes MySQL server is overloaded that's why I'm caching MySQL results in static (cached) files. Here is what I do:

  1. First of all, I check for cache file (page specific)
  2. If it doesn't exist, I query database and I serialize the results
  3. I write results to the cache file
  4. When file exists, I get content with file_get_contents() function and unserialize the data

Now my question is, will my caching strategy improve performance or should I keep using database queries for every request?

halfer
  • 19,824
  • 17
  • 99
  • 186
Rehmat
  • 4,681
  • 3
  • 22
  • 38
  • Are you asking for help on how to test it? – Jeff Puckett May 31 '16 at 17:39
  • 2
    In general file access is _much_ slower than a database query, _except_ if your query is extremely expensive. What you might want to do instead is to cache the object you create in memory instead of a file. That way neither a query nor a file access has to be performed for many requests. And memory is surprisingly cheap as a resource... – arkascha May 31 '16 at 17:41
  • @JeffPuckettII I am asking which one will put higher load on server. – Rehmat May 31 '16 at 17:41
  • @arkascha Thank you so much for the idea, I didn't think about that. Query is based on multiple joins that's why I'm looking for a caching solution. I'll be checking your suggestion. – Rehmat May 31 '16 at 17:43

1 Answers1

1

There are many ways to speed up your web site and to lower database load. Those are much better then trying to make manual cache files. Here are some of them:

  1. Optimize database settings (so many interesting moments!)
  2. Use memory tables
  3. Add CPU\RAM to web server
  4. Use caching servers like nginx or varnish (those can put whole static pages to cache)
  5. Use special data caching in PHP - like memcached
  6. Use sockets instead of TCP\IP connection to database
  7. Use mysql-nd instead of mysql

And here is the answer to your exact question: PHP - Is it good practice to cache MYSQL queries in a txt file?

Community
  • 1
  • 1
Jehy
  • 4,729
  • 1
  • 38
  • 55
  • Thank you for your answer. I'll be checking into your suggestions. Caching MySQL results in memory doesn't seem a good solution for me as my pages are frequently updated (with user reviews, ratings etc.) so the caching is going be invalidated so quickly. – Rehmat Jun 01 '16 at 05:32
  • @rehmat some of results are updated often... But some are not. You can set cache time for each data set separately. Also, ask yourself a qustion - do your users really need realtime actual information in all cases? And anyway, that will be much better then caching on hard drive. – Jehy Jun 01 '16 at 09:28