npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

memcached-protocol-server

v0.0.4

Published

Memcached protocol server framework

Downloads

13

Readme

memcached protocol server framework

This is a memcached protocol server framework.

You can implement a server, supports memcached protocol.

So, it means you can write a KVS supports memcached protocol, very easy.

SYNOPSIS

/**
 * This is an example server to implement memcached like on-memory KVS
 * with MemcachedProtocolServer.
 */

var Server = require('../index.js').Server;

var port = process.argv[2] || 22422;

var server = new Server();
server.on('error', function (e) {
    console.log("# Socket error!!: " + e);
});
server.on('end', function () {
    console.log("# END!!");
});
server.setHandlers({
    GET: function (sock, keys) {
        var self = this;
        keys.forEach(function (key) {
            if (self.storage[key]) {
                var entry = self.storage[key];
                sock.sendValue(key, entry[0], entry[1]);
            }
        });
        sock.sendEnd();
    },
    SET: function (sock, key, flags, exptime, data, noreply) {
        var self = this;
        self.storage[key] = [flags, data];
        if (!noreply) {
            sock.sendStored();
        }
    },
    DELETE: function (sock, key, noreply) {
    if (this.storage[key]) {
        delete this.storage[key];
        if (!noreply) {
            sock.sendDeleted();
        }
    } else {
        if (!noreply) {
            sock.sendNotFound();
        }
    }
    },
    DECR: function (sock, key, value, noreply) {
        if (this.storage[key]) {
            var v = parseInt(this.storage[key][1], 10);
            this.storage[key][1] = '' + (v - value);
            if (!noreply) {
                sock.write(''+(v-value)+"\r\n");
            }
        } else {
            if (!noreply) {
                sock.sendNotFound();
            }
        }
    },
    INCR: function (sock, key, value, noreply) {
        if (this.storage[key]) {
            var v = parseInt(this.storage[key][1], 10);
            this.storage[key][1] = '' + (v + value);
            if (!noreply) {
                sock.write(''+(v+value)+"\r\n");
            }
        } else {
            if (!noreply) {
                sock.sendNotFound();
            }
        }
    },
    VERSION: function (sock) {
        sock.sendVersion("4649");
    },
    FLUSH_ALL: function (sock, delay, noreply) {
        this.storage = {};
        if (!noreply) {
            sock.sendOK();
        }
    }
});
server.storage = {};
server.listen(port);

Classes

require('memcached-protocol-server').Server

var new Server();

Create a new instance of server.

server.listen(port[, host])

Listen the port.

server.setHandlers(handlermap)

Set a command handler in Object. For more details, please see a eg/memd.js and library source code itself.

require('memcached-protocol-server').Socket

This is a client connection socket object. You don't need create this object. MemcachedProtocolServer passes this type of object to your handler.

This object provides a lot of methods to say memcached protocol. But you need to study only two methods simply, It's 'close' and 'write'. Another methods are wrapper of 'write'.

sock.close()

Close a client socket.

sock.write()

Write a data to socket.

sock.sendStat(name, value)

Send a stat line.

sock.sendTouch()

Synonym of

    sock.write("TOUCHED\r\n");

sock.sendError:()

    sock.write("ERROR\r\n");

sock.sendEnd()

    sock.write("END\r\n");

sock.sendStored()

    sock.write("STORED\r\n");

sock.sendDeleted()

    sock.write("DELETED\r\n");

sock.sendNotStored()

    sock.write("NOT_STORED\r\n");

sock.sendExists()

    sock.write("EXISTS\r\n");

sock.sendOK()

    sock.write("OK\r\n");

sock.sendNotFound()

    sock.write("NOT_FOUND\r\n");

sock.sendServerError(error)

    sock.write("SERVER_ERROR " + error + "\r\n");

sock.sendClientError(error)

    sock.write("CLIENT_ERROR " + error + "\r\n");

sock.sendValue(key, flags, data_block[, cas_unique])

It sends following command for 'get' command.

// VALUE <key> <flags> <bytes> [<cas unique>]\r\n
// <data block>\r\n

sock.sendVersion(version)

    sock.write("VERSION " + version + "\r\n");

SEE ALSO

https://github.com/memcached/memcached/blob/master/doc/protocol.txt