Cache Rules Everything Around Me

Posted on: September 6th, 2007 by Jeremy Schoemaker

Dillsmack went to Italy for a bit so I am playing with code on my own. (expect downtime). Anyway I been playing with magpierss, wp-cache, and memcached and woah.

The Wu-Tang clan knew what they were talking about with C.R.E.A.M

  • wp-cache is the plugin for wordpress which uses file level caching. This works really good for most things but its kind of touch and go. I really am kind of puzzled why wordpress does not include this incorporate this in wordpress. It takes a HUGE load off of your database server and will keep you up if you get slash doted or dugg.
  • memcached - caching at ram level wooooot. This is pretty badass and I am having a lot of fun with it. My friend Shawn Hogan tried to turn me onto it a while ago but I just could not wrap my head around it. While I use wp-cache for most of this blog the custom plugins i wrote I use a 512mb memcache to store the results for 60 seconds. The code I wrote for them is really bad but the cool thing about memcache is it makes up for your bad code. I just cache the output of the functions and whola lightning fast..as ram!
  • Magpie RSS. Magpie rss is what I used to build my spider-bot that crawls my friends RSS feeds and all the forums to produce the marketplace feed. I memcache the results on the marketplace ever 30 seconds incase it gets hammered (and it has been getting hammered pretty good). Anyway magpie has built in caching so I dont hammer my friends rss feeds to bad.
  • EDIT:

    Here is an example of my code for the top commentors on the side bar that I wrote and the memcache that caches in information:

    PHP:
    1. function posse()
    2. {
    3. #start cache
    4. $cache_key = "posse";
    5. $memcache = new Memcache;
    6. $memcache->addServer('127.0.0.1', 11211,0,1,1,1);
    7. $output = $memcache->get($cache_key);
    8. if (!$output) {
    9.        ob_start();
    10. #end Start Cache
    11.  
    12.  
    13.         global $wpdb, $tablecomments;
    14.  
    15.         print "<div class=\"topcom_body\">";
    16.  
    17.                 $commenters = $wpdb->get_results("SELECT COUNT(comment_author) AS commentcount, comment_author, comment_author_url FROM $tablecomments
    18.                                 WHERE unix_timestamp(comment_date)>unix_timestamp(NOW())-(60*60*24*7)
    19.                                 AND comment_author_url !=''
    20.                                 AND comment_author !='ShoeMoney'
    21.                                 AND comment_author != 'ddn'
    22.                                 AND comment_approved = '1'
    23.                                 AND comment_type =''
    24.                                 GROUP BY comment_author
    25.                                 ORDER BY commentcount DESC
    26.                                 LIMIT 20;");
    27.  
    28.                 foreach($commenters as $commenter)
    29.                 {
    30.  
    31. print " <div class=\"topcom_row\"><a href=\"".$commenter->comment_author_url."\" class=\"topcom\">" .stripslashes($commenter->comment_author)." ($commenter->commentcount) </a></div>";
    32.  
    33.  
    34.         }
    35.  
    36. print "</div>";
    37.  
    38. #End of Cache Code
    39. $output = ob_get_contents();
    40. $memcache->set($cache_key, $output, false, 60); // 60 second cache
    41. } else {
    42.         echo $output;
    43.         return;
    44.         }
    45. #End of Cache Code
    46.  
    47.         }

    So for those who understand php you can see this would be a pretty huge load for the 65,000+** hits this recieves a day... but by caching this for 60 seconds you drastically lower that ;)

    Cacheing rocks!

    **hits refers to total pageloads not unique visitors. This site only gets between 10,000 and 15,000 unqiues per day

    Post written by Jeremy Schoemaker

    Hi I am Jeremy Schoemaker and ShoeMoney.com is my blog. 99% of the post here are done by me but you will see others occasionally make guest posts. This blog is fun to write but for my day job I run several online companies.

    More about Jeremy at http://www.shoemoney.com!

    Have You Read:

    61 Comments. What Say You?

    1. Instant Community
      September 17, 2007 at 7:27 am

      This is very true… cache rules after all :)

    2. Blogs for Money
      September 9, 2007 at 8:13 am

      I was going to mention you might get stale comments, but I see the cache is cleared every 60 seconds, not every 60 minutes. I guess that’s a good compromise :D

    3. Blogs for Money
      September 9, 2007 at 8:11 am

      Good idea :) I’m using BlogEngine.net which is ASP.NET, which it has built in support for “output caching” that basically does the same thing. You can specify a timeout, and a querystring parameter used to decide whether the cache is unique (eg. if you have ?postId=x you want the cache to very with x).

    4. Joeychgo
      September 9, 2007 at 12:22 am

      Me too!

    5. Aditya Kumar Singh
      September 7, 2007 at 9:51 pm

      Wp-Cache must be a default plugin for wordpress actually.

      BTW Excellent idea you gave . I would like to try on my blog too :)

    6. Ken Savage
      September 7, 2007 at 8:10 pm

      better to be over prepared and cautious than caught with your pants down and loose traffic possibilities.

    7. Modern Worker
      September 7, 2007 at 4:37 pm

      Same here, I’ll be giving memcache a go this weekend, looking forward to tracking performance results

    8. SMM Guru
      September 7, 2007 at 3:46 pm

      No one said it was better, he said it was “simpler”.

    9. Gecko Tales
      September 7, 2007 at 7:23 am

      wp-cache: You have the influence to get WP to add this feature. I might get a form letter response, but you could probably get them on the phone.

    10. jim
      September 7, 2007 at 5:40 am

      Thanks for the code, I’ve never used memcache before (my traffice levels aren’t as high as yours so never been spurred to go past wp-cache) so thanks.

    11. Seo Next
      September 7, 2007 at 5:32 am

      Wp-Cache is really great wordpress plugin , as u said for big sites this plugin is really mandetary.I hope wordpress fucture versions will have Wp-Cache as default plugin.

    12. ShoeMoney
      September 7, 2007 at 2:37 am

      You are totally wrong. uniques is no measure.

      Ever been dugg? I guess not. Get 150+ hits a second and let me know how that goes.

    13. lyricsreg
      September 7, 2007 at 2:29 am

      IMO, cache-ing is not justified for a website with less than 30k uniques. It does free some resources, but with nowadays servers, it takes a lot of traffic to to intimidate them. Smarty introduced such a cache-ing tool a long time ago, but I fail to see the the point of it, unless the server really can’t handle the requests. Just my opinion, I may be wrong

    14. TheDirectoryDude
      September 7, 2007 at 2:04 am

      Thanks for this Shoe. You’ve inspired me to give WP-Cache and magpie a shot.

      Like everyone else I can’t figure out why this isn’t included in Wordpress already.

    15. Rxbbx
      September 7, 2007 at 12:20 am

      You should take a look at SimplePie for that RSS Stuff

    16. Ken Savage
      September 6, 2007 at 10:20 pm

      dollah dollah bill y’all.

      Damn now it’s stuck in my head.

    17. CPA Affiliates
      September 6, 2007 at 10:11 pm

      thats pretty awesome man! :)

    18. Ken Savage
      September 6, 2007 at 10:03 pm

      I think the version with Mary J Blige is best.

    19. Hip Hop
      September 6, 2007 at 9:57 pm

      lol great Wu Tang reference

    20. Click Input
      September 6, 2007 at 9:22 pm

      I have Magpie running on my blog too… but not like you!

    21. Chris
      September 6, 2007 at 9:12 pm

      I had some issues with wp-cache, and I kind of am concerned about using it again because my site is updated so much I dont want my users to miss updates just because of the cache. Not sure what I can do about that..

    22. corey
      September 6, 2007 at 6:36 pm

      the logo is a background image, but there’s a huge gap to the left of the banner where he could put a big box with a javascript link.

      and this is the best blog post title of 2007. contest over.

    23. markus
      September 6, 2007 at 5:47 pm

      Haha. Love that title. Notorious S.H.O.E

    24. Word Hugger
      September 6, 2007 at 5:40 pm

      I agree with Travel Notebook. The “home” button is a few inches away, but it has just become second nature for me to click on the logo to go back to the homepage. =D

    25. David
      September 6, 2007 at 5:11 pm

      There are a million tutorials online for PHP. Just Google “PHP Tutorials” and follow along.

      If you don’t like written tutorials you can check out the videos at…
      http://www.phpvideotutorials.com/
      or
      http://www.lynda.com/ – I think it’s about $25 per month.

    26. ShoeMoney
      September 6, 2007 at 5:10 pm

      Probably not but i will see

    27. ShoeMoney
      September 6, 2007 at 5:05 pm

      just vim

    28. Paul.
      September 6, 2007 at 5:01 pm

      Do you use a program to edit your PHP coding?

    29. ShoeMoney
      September 6, 2007 at 4:58 pm

      I got in the habit of using double quotes just so I can use non literal strings. I honestly have no clue if single quotes are faster then double quotes.

    30. Gary R. Hess
      September 6, 2007 at 4:48 pm

      Shoe, is there a reason why you are using double quote marks instead of single marks in that code? I always thought single quote marks were faster.

    31. dfw
      September 6, 2007 at 4:36 pm

      Shoe get the money dollar dollar bills yall

    32. Travel Notebook
      September 6, 2007 at 4:24 pm

      Side note: Can you please make your logo clickable to the index page again? This is a standard usability thing

    33. browie
      September 6, 2007 at 4:23 pm

      That’s pretty badass. How did you get into PHP and learn it?

      I’d love to but I (kind of like you) resort to a friend for PHP help. Anyone else with ideas on how they learned?

    34. Travel Notebook
      September 6, 2007 at 4:23 pm

      Travel Permit?

    35. Travel Notebook
      September 6, 2007 at 4:23 pm

      Ive been meaning to look into memcached since you talked about it last time. I guess I will give it a go

    36. Marina @ Sufficient Thrust
      September 6, 2007 at 4:18 pm

      I installed wp-cache but it made all of my dynamic sidebar items static. Any idea what I did wrong? I’m decently tech-savvy but I admittedly didn’t look into it very far before de-activating it.

    37. Theo
      September 6, 2007 at 4:16 pm

      Did Dillsmack allow you to play with code?

    38. Modern Worker
      September 6, 2007 at 3:47 pm

      I lovvvve wp-cache, it’s drastically sped up my WP sites

    39. ShoeMoney
      September 6, 2007 at 3:45 pm

      playing with it. its entertaining… for now ;) Maybe it will last longer then mybloglog

    40. Jonathan Volk
      September 6, 2007 at 3:38 pm

      Yea. This is the install steps:

      1. Upload the folder.
      2. Click activate.
      3. Done. :P (Unless you have some permissions you need to set)

    41. eTown Landlord
      September 6, 2007 at 3:37 pm

      shoe is twittering??? I’m wondering why? Do you have a custom app in the works since they opened up their platform to developers?

    42. Grivon
      September 6, 2007 at 3:34 pm

      … “get dat money” (SHOE!) “dolla dolla bill y’alllllllllll” :p

    43. Grivon
      September 6, 2007 at 3:29 pm

      So basically you’re saying.. start using wp-cache and your site will live through moderate digging??? Any wordpress site on it’s own box basically?? I think I’ll have to start learning as well :S

    44. Dave
      September 6, 2007 at 3:29 pm

      I stop reading at “Dillsmack went to Italy for a bit”… I’m still waiting my travel permit and I already bought $2000 in tickets to go visit my family in Italy with my wife and probably I won’t be able to go :( *sigh*

    45. Trevor
      September 6, 2007 at 3:24 pm

      C.R.E.A.M brings back memories…Wu Tang Clan is off the hook…ha ha

    46. Brent Wilson
      September 6, 2007 at 3:23 pm

      Thanks a bunch.

    47. ShoeMoney
      September 6, 2007 at 3:19 pm

      sure I will edit the post with a example

    48. Brent Wilson
      September 6, 2007 at 3:10 pm

      Care to share the memcached code? A lot of wordpress users could benefit from this :)

    49. ***V***
      September 6, 2007 at 3:03 pm

      Why is Last RSS better than magpie?

    50. ***V***
      September 6, 2007 at 3:01 pm

      What other plugins do you use Paul?

      V

    51. Paul Bradish
      September 6, 2007 at 2:54 pm

      I use WP-Cache and didn’t even realize that there were other options. Though this plug in works very well for me, it’s nice to know that it isn’t the only plug in available.

    52. ShoeMoney
      September 6, 2007 at 2:47 pm

      nope… you just have to create a cache folder. The directions are pretty straight forward.

    53. katurday
      September 6, 2007 at 2:41 pm

      I also use wp-cache, and while it has issues with certain plugins with dynamic content (such as wp-postratings), it is indeed very handy to keep installed in the event of a slashdotting or getting linked from boing boing. I agree that wordpress needs to just integrate this into the main system.

    54. Word Hugger
      September 6, 2007 at 2:41 pm

      Does wp-cache require anything extra server side? I see a lot of posts on google saying they have problems installing it. I have not personally tried it myself yet, but I will bookmark this page for when I start a blog.

    55. Kenetix
      September 6, 2007 at 2:32 pm

      Very interesting, i am planning on trying MagPie.

    56. Kieran
      September 6, 2007 at 2:31 pm

      Last RSS is a good alternative to magpie if you want something simpler.

    57. KiwiPulse
      September 6, 2007 at 2:14 pm

      Since im using the wp-cache, I just can’t live without it! It makes my page faster and with the load of traffics from stumbleUpon, my blog is still on! :D

    58. Jonathan Volk
      September 6, 2007 at 1:48 pm

      Looks great. I think I’ll take a look at Wp-Cache for my own blog. Thanks for the recommends. :)

    Leave a Reply