Cache Encodings with the JavaScript Client

What You Will Learn

How to connect to Infinispan with different data format encodings and store/retrieve data in text/plain and application/json formats.

Prerequisites

  • Node.js 22+

  • An Infinispan Server running on localhost:11222

Start an Infinispan Server with Docker or Podman:

docker run -it --rm -p 11222:11222 -e USER=admin -e PASS=password quay.io/infinispan/server:latest
Tip
You can replace docker with podman in the command above if you use Podman.

Step 1: Text/Plain Encoding

Connect with text/plain data format and store plain text values:

const textClient = await ispn.client({port: 11222, host: '127.0.0.1'}, {
    dataFormat: {
        keyType: 'text/plain',
        valueType: 'text/plain'
    },
    authentication: { ... }
});

await textClient.put('greeting', 'Hello, Infinispan!');
const val = await textClient.get('greeting');

Step 2: JSON Encoding

Connect with application/json value format and store JSON objects as strings:

const jsonClient = await ispn.client({port: 11222, host: '127.0.0.1'}, {
    dataFormat: {
        keyType: 'text/plain',
        valueType: 'application/json'
    },
    authentication: { ... }
});

const jsonValue = JSON.stringify({project: 'Infinispan', language: 'JavaScript'});
await jsonClient.put('info', jsonValue);
const retrieved = JSON.parse(await jsonClient.get('info'));

Step 3: Run the Tutorial

npm run encoding

You should see output like:

== Cache with text/plain encoding ==
get(greeting) = Hello, Infinispan!

== Cache with application/json encoding ==
get(info) = {"project":"Infinispan","language":"JavaScript"}
  project  = Infinispan
  language = JavaScript

Done.

What’s Next