Skip to content

Instantly share code, notes, and snippets.

@leommoore
Last active October 13, 2015 04:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leommoore/4143066 to your computer and use it in GitHub Desktop.
Save leommoore/4143066 to your computer and use it in GitHub Desktop.
Node - Memcached Basics

#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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment