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

@joshbetz/memcached

v1.3.0

Published

Memcached client for modern Node JS

Downloads

222

Readme

Memcached Node.js CI

There are three libraries exported from this package.

const { Memcached, Pool, HashPool } = require( '@joshbetz/memcached' );

API

The API for all three libraries is the same. It just depends what kind of connection and failover logic you need.

async ready()

Wait for the connection to be ready.

async flush()

Flush the Memcached data.

async set( key, value, ttl ): Boolean

SETs a given key and value for the specified TTL (or no TTL). Returns a Boolean to indicate whether the operation was successful.

async add( key, value, ttl ): Boolean

ADDs a given key and value for the specified TTL (or no TTL) if it doesn't already exist. Returns a Boolean to indicate whether the operation was successful.

async get( key ): string|Boolean

GETs a given key. Returns false if it does not exist.

async del( key ): Boolean

DELETEs a given key.

async ping(): Boolean

Sends the version command. Returns true if the expected response is returned.

async end()

Close the connection to Memcached.

Memcached Library

This is a simple Memcached library that connects to a Memcached server and execute commands.

Example

const opts = {
    prefix: '',
    socketTimeout: 100,
};
const memcached = new Memcached( 11211, 'localhost', opts );
await memcached.ready();
await memcached.set( 'key', 'value' );
const value = await memcached.get( 'key' );
await memcached.end();

Options

  • prefix A prefix to apply to all keys. Default: ''
  • socketTimeout The timeout to establish a connection. Default: 100.

Pool Library

This is a wrapper around our Memcached library that establishes a connection pool.

Example

const opts = {
    // Pool options
    max: 10,
    min: 2,
    acquireTimeoutMillis: 200,
    destroyTimeoutMillis: 200,
    maxWaitingClients: 2,
    idleTimeoutMillis: 30000,

    // Connection options
    prefix: '',
    socketTimeout: 100,
};
const memcached = new Pool( 11211, 'localhost', opts );
await memcached.set( 'key', 'value' );
const value = await memcached.get( 'key' );
await memcached.end();

Options

  • max The maximum number of connections in the pool. Default: 10.
  • min The minimum number of connections in the pool. Default: 2.
  • acquireTimeoutMillis The maximum amount of time to wait to create a connection. Default: 200.
  • destroyTimeoutMillis The maximum amount of time to wait to destroy a connection. Default: 200.
  • maxWaitingClients The maximum number of queued requests allowed, additional acquire calls will be callback with an err in a future cycle of the event loop. Default: 2.
  • idleTimeoutMillis The minimum amount of time that an object may sit idle in the pool before it is eligible for eviction due to idle time. Default: 30000.
  • prefix A prefix to apply to all keys. Default: ''
  • socketTimeout The timeout to establish a connection. Default: 100.

HashPool Library

This is a wrapper around our Pool library that establishes connection pools to each host and load balances queries across them. It includes automatic failover and reconnecting when hosts experience issues.

Example

const opts = {
    retry: ( retries: number ): number => {
        const exp = Math.pow( 2, retries ) * 250;

        // exponential backoff up to 30 seconds
        return Math.min( exp, 30000 );
    },

    // Pool options
    max: 10,
    min: 2,
    acquireTimeoutMillis: 200,
    destroyTimeoutMillis: 200,
    maxWaitingClients: 2,
    idleTimeoutMillis: 30000,

    // Connection options
    prefix: '',
    socketTimeout: 100,
};
const memcached = new HashPool( [ 'localhost:11211', 'localhost:11311' ], opts );
await memcached.set( 'key', 'value' );
const value = await memcached.get( 'key' );
await memcached.end();

Options

  • retry A function that takes the number of retries as a parameter and returns the time before the next retry in milliseconds.
  • max The maximum number of connections in the pool. Default: 10.
  • min The minimum number of connections in the pool. Default: 2.
  • acquireTimeoutMillis The maximum amount of time to wait to create a connection. Default: 200.
  • destroyTimeoutMillis The maximum amount of time to wait to destroy a connection. Default: 200.
  • maxWaitingClients The maximum number of queued requests allowed, additional acquire calls will be callback with an err in a future cycle of the event loop. Default: 2.
  • idleTimeoutMillis The minimum amount of time that an object may sit idle in the pool before it is eligible for eviction due to idle time. Default: 30000.
  • prefix A prefix to apply to all keys. Default: ''
  • socketTimeout The timeout to establish a connection. Default: 100.