Examples of Memcached with Node.js, Python and more

Node.js

Additional Documentation

var Memcached = require('memcached');  
var memcached = new Memcached('127.0.0.1:11211');  
var lifetime = 86400; //24hrs  
// Add first entry to cache
memcached.add('firstKey', 'First', lifetime, function( err, result ){  
    if( err ) console.error( err );
    console.dir("add firstKey: " + result );
});
memcached.get('firstKey', function( err, result ){  
    if( err ) console.error( err );
    console.dir("Value for firstKey: " + result );
});
// Change value
memcached.set('firstKey', 'First Updated', lifetime, function( err, result ){  
    if( err ) console.error( err );
    console.dir("Set firstKey: " + result );
});
memcached.get('firstKey', function( err, result ){  
    if( err ) console.error( err );
    console.dir("New value for firstKey: " + result );
});
// Add second entry to cache
memcached.add('secondKey', 'Second', lifetime, function( err, result ){  
    if( err ) console.error( err );
    console.dir("add secondKey: "+ result );
});
memcached.get('secondKey', function( err, result ){  
    if( err ) console.error( err );
    console.dir("Value for secondKey: " + result );
});
// Remove a single entry
memcached.del('firstKey', function( err, result ){  
    if( err ) console.error( err );
    console.dir("del firstKey: " + result );
});
// Remove all entries
memcached.flush(function( err, result ){  
    if( err ) console.error( err );
    console.dir("flush: " + result );
});

Source Code on GitHub: https://github.com/justinbellamy/MemcachedExampleNodeJS

Python

Implemented using the pylibmc library. Additional Documentation

import pylibmc  
memcached = pylibmc.Client(['127.0.0.1'], binary=True, behaviors={"tcp_nodelay": True})

#Using mapping interface
memcached["First"] = "First Value"  
print memcached["First"]  
del memcached["First"]


#Using classic style
if memcached.add("Second", "Second Value"):  
    print memcached.get("Second")

if memcached.prepend("Second", "This is the... "):  
    print memcached.get("Second")

if memcached.append("Second", "!"):  
    print memcached.get("Second")

if memcached.replace("Second", "Second Value Replaced"):  
    print memcached.get("Second")

if memcached.add("Second", "A Different Value"):  
    print memcached.get("Second")
else:  
    print "Couldn't add to key:Second, a value already exists."
memcached.delete("Second")

#Using increments and decrements
if memcached.set("3", "10"):  
    print memcached.get("3")
    memcached.incr("3")
    print memcached.get("3")
    memcached.decr("3")
    print memcached.get("3")

#Print a "deleted" key. Should return the constant None
memcached.delete("3")  
print memcached.get("3")

#Attempt to replace a key that doesn't exist & handle the error gracefully
try:  
    memcached.replace("Fourth", "Fourth Value")
except pylibmc.Error as e:  
    print "Can't replace something that doesn't exist! - " + str(e)

#Clear everything
memcached.flush_all()  

Source Code on GitHub: https://github.com/justinbellamy/MemcachedExamplePython

PHP

Additional Documentation

<?php  
$cache = new Memcached();
$cache->addServer("localhost", 11211);
$cache->setOption(Memcached::OPT_BINARY_PROTOCOL, true);

// Add first entry to cache
if ($cache->add("firstKey", "First")) {  
    echo("Value for 'firstKey': " . $cache->get("firstKey") . "\n");
}

// Change value
if ($cache->set("firstKey", "First Updated")){  
    echo("New value for 'firstKey': " . $cache->get("firstKey") . "\n");
}

// Add second entry to cache
if ($cache->add("secondKey", "Second")){  
    echo("Value for 'secondKey': " . $cache->get("secondKey") . "\n");
}

// Try to get something that doesn't exist
if ($cache->get("thirdKey")){  
    echo("Value for 'thirdKey': " . $cache->get("thirdKey") . "\n");
} else {
    echo("'thirdKey' doesn't exist \n");
}

// Get both entries at same time
$map = $cache->getMulti(array("firstKey", "secondKey"));
if ($map) {  
    echo("Value for 'firstKey': " . $map["firstKey"] . ", and value for 'secondKey': " . $map["secondKey"] . "\n");
}

// Remove a single entry
if ($cache->delete("firstKey")){  
    echo("Removed first entry from cache.\n");
}

// Remove all entries
if ($cache->flush()){  
    echo("Cleared entire cache.\n");
}

Source Code on GitHub: https://github.com/justinbellamy/MemcachedExamplePHP