Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have a Wordpress site that uses WP Super Cache plugin in order to make my blog posts and pages stay as HTML so that less PHP/MySQL code is executed. However, there's a featured box that can't get cached. So, I configured it manually to bypass cache and stay dynamic (code is executed on every page load).

So, most part of the entire page is executed using HTML and a specific part (a featured bar box) uses a PHP/MySQL.

The code that runs on every page load are some simple <?php $query = mysql_query("SELECT * [... rest of my code here ...]"); ?> to select some data from my database and some $fetch and <?php echo [...] ?> to print the results in the webpage, of course. But instead of everytime calling database to get this results, it would be much better to cache this results. So, I have three options:

  1. Let WP Super Cache caches my entire page. But I can't do that, because inside my box that currently isn't getting cached, the database data can change anytime (normally 1 time per day). So, I can't cache this box and we have to discart this option 1.
  2. Activate query_cache in my.cnf. However, this will only cache MySQL and don't PHP. :(
  3. Than, we have memcache (or APC, I don't know much about it's difference). After all my explanations, here's the final question: I'd like to know if installing memcache in my server is enought to get it working imediatally or I have to make any changes in my code to adapt it with memcache; I'd like to know if it will really help me with both PHP and MySQL for this specific part of code I have to cache or it's results is similar to enabling query_cache? Or is using APC rather than memcache better in my case?

Thanks,
André.

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

You should decide what exactly you need, before deciding those 3 options.

My suggestion is as following:

  1. Install memcache and the necesary php module to use with memcache. Once you install memcache, you will have an IP and PORT to connect to, for SET / GET operations

  2. Decide where exactly in your Wordpress php code you need to inject the code that displays the "featured" stuff

  3. Add a php code similar to:

    • CONNECT to memcache
    • Check for the existance of a KEY/VALUE pair, named "FEATURED STUFF"
    • If there is, GET that pair, and display the pair's value
    • If not, run the SELECT query, FETCH the results, build a string that will contain the HTML body of "FEATURED STUFF", PUT the string in memcache as a KEY/VALUE pair.
  4. The code above should not be cached with WP Super Cache. Let memcache do the cacheing for you.

If you need help with the actual implementation, let me know.


Sample PHP Code:

$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");

if ($gotten = $memcache->get('featured_stuff')) {
    echo 'CACHE HIT'</br>';
    echo $gotten;
} else {
    # Never mind - fetch it and store for next time!
    $gotten = dbfetch($query, ...);
    $memcache->set($featured_stuff,$gotten);
    echo $gotten;
}
share|improve this answer
    
Really complete answer, @MTIhai. Thank you very much. I think I got the ideia and will do as you said. This way both PHP (used to print MySQL results) and MySQL (used to give results to PHP) are cached or only MySQL is? –  André Bazaglia Apr 15 '13 at 23:54
1  
in short, MYSQL result from the query you send to the DB will be "cached" as a KEY/VALUE pair in memcache. So PHP will only need to fetch that query result once (in every 4 hours for example) and during those 4 hours PHP will not query the database, as it can get the result from memcache. –  MTIhai Apr 16 '13 at 13:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.