Memcached Test

Warning: Hard Core Dorkage!

Here’s a little simple memcached test you can have to test memcached on your system. I am working on an app that, while the server load is not huge, the datasets that you are presenting to a visitor are larger than normal for a web app, and so I ultimately plan to break them into subsets and only load the subset the user is focusing on and cache the rest.  Hopefully most users will confine their activities to items from the same subset so we won’t be thrashing too much. Anyway we’re a long way from that day. I just installed the memcached and tested it with telnet, and I got it all hooked into the whole PHP Apache tangle.  So now we’re going to cache some data and see if it works.

<?php
//memcached simple test
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
$key = md5('42data');  //something unique
for ($k=0; $k<5; $k++) {
$data = $memcache->get($key);
    if ($data == NULL) {
        $data = array();
        //generate an array of random shit
        echo "expensive query";
        for ($i=0; $i<100; $i++) {
            for ($j=0; $j<10; $j++) {
                $data[$i][$j] = 42;  //who cares
            }
        }
        $memcache->set($key,$data,0,3600);
    } else {
        echo "cached";
    }
}

And of course you can test to see if memcached is loaded with

$memcache_enabled = extension_loaded("memcache");

There, don’t say I never gave you anything.

9 comments to Memcached Test

  • Easy, einfach, facile!! what more can I say???!!!,, Am Joking!!! Thank you for sharing those lines!

  • I don’t have any experience with memcached whatsoever, but I searched on Google and found this:

    “Memcache::connect() [function.Memcache-connect]: Can’t connect to localhost:11211, Connection refused (111)in…”

    So for some reason, it can’t connect to the server. I don’t know how memcached works, so I can’t help any further at the moment.

  • admin

    Ummm…. bike trader, I didn’t spam this comment because its clearly not auto-spam. Belaboring that you have no experience with memcached whatsoever is unnecessary because that’s pretty obvious. Thanks for the visit, but I can’t help wondering if it’s because I have the Top Commentators plugin. I wasn’t asking for help. Memcached connects just fine for any authorized IP on my server.

  • The beauty of memcached is the simplicity. Bringing boxes onto the grid, removing them, adding memory, adjusting hashing schemes, serialization schemes, etc – It’s all clear because there is so little magic to it. Additionally, in our experience, java’s memory management (GC schemes, etc) is explicitly poor for mixed use caching. Again, memcached having it’s ‘close to the metal’ slab allocator makes understanding what’s going on dead simple.

  • Adi

    Thanks for sharing your code. Works Great and is very simple.

  • Bike trader, you need to add a server before you can connect to it.

  • salut,Offre un rétablissement site s’avère à être tout simplement génial merci bcp pour ce post. J’apprecie vraiment infiniment m’a vraiment aidé, je recommanderai site aux gens que je connais. a bientot.

  • This is my first time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the leisure here! Keep up the excellent work.

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna, tincidunt vitae molestie nec, molestie at mi. Nulla nulla lorem, suscipit in posuere in, interdum non magna.