#Node - Memcached Basics
Memcached is an in memory key-value store.
Installing Memcached
apt-get install memcached
To check the version
memcached -i
Memcached is not normally installed as a system service. You may also want to have multiple instances of Memcached running at the same time. Memcached can be configured specifically for you application but on the most basic level it is just a matter of specifying the port you want to listen on.
memcached -p 11211
Alternatively you may want to use the -vv option which shows more verbose information which can be useful for debugging.
memcached -vv -p 11211
##Sample Javascript app
memcached.js
var Memcached = require( 'memcached' )
var memcached = new Memcached("127.0.0.1:11211")
var expires = 3600
memcached.set( "foo", 'bar', expires, function( err, result ){
if( err ) console.error( err );
console.dir( result );
memcached.get( "foo", function( err, result ){
if( err ) console.error( err );
console.dir( result );
})
})
Run the memcached.js script
node memcached.js
This return bar as this is the value associated with foo. The memcached.get(key,callback)
is used to retrieve the value and the memcached.set(key,value, expires, callback)
is used to set the value.