Node.js Memcached Client

1.0.5 · active · verified Tue Apr 21

memcache-client is a high-performance Node.js client designed for interacting with Memcached servers, focusing on efficient ASCII protocol parsing through direct Node.js Buffer APIs. Originally developed and heavily used at WalmartLabs for powering the walmart.com e-commerce platform, it offers robust features crucial for large-scale applications. These include optional data compression, automatic reconnection on network errors, support for arbitrary Memcached commands, and the ability to store various data types like Buffer, string, numeric, and JSON. The library provides a flexible API supporting both traditional Node.js callbacks and modern Promise-based operations, alongside features like fire-and-forget requests, multiple connections, and TLS support. It is currently at version 1.0.5 and appears to be in a maintenance or stable release state, with a focus on reliability and performance in demanding environments.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates creating a MemcacheClient, setting and retrieving single keys using Promises, concurrently setting multiple keys, and performing a typed multi-key retrieval. It also shows an example of using a callback for deletion.

import { MemcacheClient, MultiRetrievalResponse } from 'memcache-client';
import assert from 'node:assert';

const server = 'localhost:11211';

// Create a client with default settings (maxConnections = 1)
const client = new MemcacheClient({ server });

async function runMemcacheExample() {
  try {
    // Set a key-value pair using Promises
    const setResult = await client.set('myKey', 'myValue');
    assert.deepEqual(setResult, ['STORED']);
    console.log('Set "myKey":', setResult);

    // Get the value of 'myKey' using Promises, with type assertion
    const getData = await client.get<string>('myKey');
    assert.equal(getData?.value, 'myValue');
    console.log('Got "myKey":', getData?.value);

    // Set multiple keys concurrently
    await Promise.all([
      client.set('key1', 'data1'),
      client.set('key2', 'data2')
    ]);
    console.log('Set "key1" and "key2".');

    // Get multiple keys with correct TypeScript typing
    const multiResults = await client.get<MultiRetrievalResponse<string>>(['key1', 'key2']);
    assert.equal(multiResults['key1'].value, 'data1');
    assert.equal(multiResults['key2'].value, 'data2');
    console.log('Got multiple keys:', multiResults);

    // Example with callback for 'delete' (optional pattern)
    client.delete('myKey', (err, result) => {
      if (err) {
        console.error('Delete error:', err);
      } else {
        assert.deepEqual(result, ['DELETED']);
        console.log('Deleted "myKey":', result);
      }
    });
  } catch (error) {
    console.error('An error occurred:', error);
  } finally {
    // It's good practice to close connections if not needed anymore, though auto-reconnect handles many cases
    // client.disconnect(); // (Assuming a disconnect method exists or client handles it internally)
  }
}

runMemcacheExample();

view raw JSON →