Basic useful commands in ioRedis

• 2 min read

I’m learning Redis using ioRedis javascript driver. Here the list of useful command I’ve learned.

The connection. I’m using Upstash distribuited database service, here the connection:

const Redis = require('ioredis')
const redis = new Redis(process.env.REDIS_DB_CONNECTION_URL)

where REDIS_DB_CONNECTION_URL is the connection string provided by Upstash.

To disconnect it:

redis.disconnect()

To set a new, or update an existing record by key item:a:

redis.set('item:a', 'Some content')

To set/update with an expiring time, that means it’ll be deleted automatically:

redis.set('item:a', 'Some content', 'EX', 60) // 60 seconds

To get a record:

const item = await redis.get('item:a')
// "Some content"

To delete a record:

redis.del('item:a')

To increment a value of an existing or new record by key:

redis.incr('count:a')

To increment by a specific value:

redis.incrby('count:a', 10)

To get a list of records using a wildcard operator:

const list = await redis.keys('item:*')

So far, so good.