elbart / node-memcache

node.js memcached client
285 stars 64 forks source link

client.close() does not appear to work #31

Open rhythmicdevil opened 11 years ago

rhythmicdevil commented 11 years ago

Hi, I appreciate your clear documentation, its the primary reason I chose this library vs. others. I cant seem to get the close method to work though. Would you mind pointing out where I am going wrong? In the code below the close event never fires.

One other question I have is why the Client constructor requires the host and port but then I also have to set it on the client.

Thanks Steve

var memcache = require('memcache');

var client = new memcache.Client('localhost', 11211);
client.port = 11211;
client.host = 'localhost';

client.on('connect', function(){
   console.log('Connected');
});

client.on('close', function(){
    console.log('Disconnected');
});

client.on('timeout', function(){
    console.log('Timeout');
});

client.on('error', function(e){
    console.log(e);
});

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

/*

client.set('flags', 1, function(err, result){

    if(err){
        console.log(err);
    } else {
        console.log('Set: ' + result);
    }

}, 1000);

client.get('flags', function(err, result){

    if(err){
        console.log(err);
    } else {
        console.log('Get: ' + result);
    }

});

client.increment('flags', 1, function(err, result){

    if(err){
        console.log(err);
    } else {
        console.log('Increment: ' + result);
    }

});

client.get('flags', function(err, result){

    if(err){
        console.log(err);
    } else {
        console.log('Get: ' + result);
    }

});

client.delete('flags', function(err, result){

    if(err){
        console.log(err);
    } else {
        console.log('Delete: ' + result);
    }

});
*/
client.close();
ghost commented 11 years ago

You don't need to set the port or host. This will do the same: var client = new memcache.Client();

You should do a close() in the callback of connect()

rhythmicdevil commented 11 years ago

Connect does not take a callback as far as I can see.

Client.prototype.connect = function () {
    if (!this.conn) {
rhythmicdevil commented 11 years ago

It has something to do with the amount of time it takes to establish the connection to the server. The following closes the connection

var MySQL = require('mysql'),
    Memcache = require('memcache');

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

Client.on('connect', function(){
   console.log( {
        'log':'info', 
        'msg':'Memcache is connected'
    });  
});

Client.on('close', function(){
   console.log( {
        'log':'info', 
        'msg':'Memcache is disconnected'
    });
});

Client.on('timeout', function(){
   console.log( {
        'log':'info', 
        'msg':'Memcache has timedout'
    });
});

Client.on('error', function(e){
   console.log( {
        'log':'exception', 
        'msg':'Error in memcache',
        'error' : e
    }); 
});

Client.connect();

setTimeout(function(){

    Client.close();    

}, 5000);
ghost commented 11 years ago
client.on('connect', function(){
   //do stuff
   client.close()
});