Giter Club home page Giter Club logo

node-memcache's Introduction

node.js memcached client

A pure-JavaScript memcached library for node.

Note: this library is a derivative work of the parent project to support binary values stored in memcache (the original would mangle them with utf8 conversions). It has 2 significant behavoral changes.

  • get returns a Buffer object instead of a String object
  • all errors return proper Javascript Error objects instead of strings

Tests

To run the test suite, first insall expresso, then run make test.

If you have node-jscoverage you can also make test-cov for coverage, but that's pretty nerdy.

Usage

Create a Client object to start working. Host and port can be passed to the constructor or set afterwards. They have sensible defaults.

var Memcache = require('memcache');

var mc = new Memcache(port, host);
mc.port = 11211;
mc.host = 'localhost';

The Client object emits 4 important events - connect, close, timeout and error.

mc.on('connect', function(){
	// no arguments - we've connected
});

mc.on('close', function(){
	// no arguments - connection has been closed
});

mc.on('timeout', function(){
	// no arguments - socket timed out
});

mc.on('error', function(e){
	// there was an error - exception is 1st argument
});

// connect to the memcache server after subscribing to some or all of these events
client.connect()

After connecting, you can start to make requests.

mc.get('key', function(error, result){
	// all of the callbacks have two arguments.
	// 'result' may contain things which aren't great, but
	// aren't really errors, like 'NOT_STORED'
});

mc.set('key', 'value', function(error, result){
	// lifetime is optional. the default is
	// to never expire (0)
}, lifetime);

mc.delete('key', function(error, result){
	// delete a key from cache.
});

mc.version(function(error, result)){
	// grab the server version
});

There are all the commands you would expect.

// all of the different "store" operations
// (lifetime & flags are both optional)
mc.set(key, value, callback, lifetime, flags);
mc.add(key, value, callback, lifetime, flags);
mc.replace(key, value, callback, lifetime, flags);
mc.append(key, value, callback, lifetime, flags);
mc.prepend(key, value, callback, lifetime, flags);
mc.cas(key, value, unique, callback, lifetime, flags);

// increment and decrement (named differently to the server commands - for now!)
// (value is optional, defaults to 1)
mc.increment('key', value, callback);
mc.decrement('key', value, callback);

// statistics. the success argument to the callback
// is a key=>value object
mc.stats(callback);
mc.stats('settings', callback);
mc.stats('items', callback);
mc.stats('mongeese', callback);

Once you're done, close the connection.

mc.close();

There might be bugs. I'd like to know about them.

I bet you also want to read the memcached protocol doc. It's exciting! It also explains possible error messages.

node-memcache's People

Contributors

elbart avatar iamcal avatar kschzt avatar akaspin avatar lackac avatar ddopson avatar jespern avatar

Stargazers

 avatar  avatar bluejack avatar wyhelother avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.