CentOS 7
Ads

Memcached : Use it on Node.js
2016/07/18
 
This is the example to use Memcached on Node.js.
[1] Install Memcached Client Module.
[cent@dlp ~]$
npm install memcache

memcache@0.3.0 node_modules/memcache
[2] Basic usage on Node.js.
[cent@dlp ~]$
vi use_memcache.js
var memcache = require('memcache');
var client = new memcache.Client();

client.connect();

// set and get a Key
client.set('key01', 'value01');
client.get('key01', function (err, val) {
    console.log("key01.value :", val);
});

// append and get a Key
client.append('key01', ',value02');
client.get('key01', function (err, val) {
    console.log("key01.value :", val);
});

client.set('key02', 1);
client.get('key02', function (err, val) {
    console.log("key02.value :", val);
});

// increment
client.increment('key02', 100);
client.get('key02', function (err, val) {
    console.log("key02.value :", val);
});

// decrement
client.decrement('key02', 51);
client.get('key02', function (err, val) {
    console.log("key02.value :", val);
});

// delete a Key
client.delete('key03');
client.get('key03', function (err, val) {
    console.log("key03.value :", val);
});

client.close();

# run

[cent@dlp ~]$
node use_memcache.js

key01.value : value01
key01.value : value01,value02
key02.value : 1
key02.value : 101
key02.value : 50
key03.value : null
 
Tweet