PHP Stress Relief Caching with MySQL

I am writing a web service for my application and want to know how best to handle the possibly many requests that I may receive. A lot of the data probably won't change over the course of the day, but the particular script I'm writing is calling 3 MySQL queries which seems a bit overkill given that the data is likely to be the same as the last query to the script, and if it isn't the same, it doesn't really matter.

Would the performance be much better if I save the output XML / JSON to a file and then request it for a day and then overwrite it with the first request the next day? What's the best way to do this?

I know that Joomla and phpBB and other MySQL heavy applications use caching to avoid creating as many MySQL queries, so that got me thinking.

EDIT - Forgot to mention that I'm on Windows / IIS 7.0

+2


source to share


4 answers


You should take a look at memchached : access it via PHP memcache or memcached

The Memcache module provides a convenient procedural and object-oriented interface to memcached, a highly efficient caching daemon that has been specifically designed to reduce database load in dynamic web applications.

It is designed for exactly what you need and is used by many high performance web applications.



Memcached was designed to boost the speed of LiveJournal.com, a site that already makes 20M + dynamic page views per day for 1M users with a bunch of web servers and database server links. The introduction of memcached has significantly reduced database load.

Note
PHP has two (2) client libraries. More information on this can be found at serverfaul.com: memcache-vs-memcached and comparison here

+6


source


Don't get discouraged with caching until you need it. Memcache is very often a premature optimization and should be your last resort, not your first resort. Adding this kind of caching can lead to tricky consistency issues.



Databases are not slow in nature and certainly not slower than loading a bunch of cached data from flat files. But they can be slow due to misuse. For example, if one of your queries for each page writes to a MyISAM table or makes a raw query, or one of your queries is complex and complex. Attack these situations by first fixing your schema.

+8


source


The quick and dirty way would be something like this.

1. create a cachefilename for this specific url
2. check if such a file exists in the cache directory and if it is younger than n minutes

2.1 if not: 
2.2 // business logic goes here => Save output e.g. in a variable $output
2.3 save contents of $output in cachefilename
2.4 echo $output

3. if so:

3.1 $output = file_get_contents(cachefilename)
3.2 echo $output

      

It might not be as elegant as memcache or memcached, but you can use it pretty much anywhere.

+3


source


Memcached is great, but might be a little overkill for your intention. A file based caching approach like the one presented by Martin will work just fine. There are a number of ready-made file-level cache libraries for PHP.

+1


source







All Articles