Node Redis HSET and other Hash Commands

08.27.2021

Intro

Hash data types are used in many alogrithms to increase speed. They usually take more memory but improved the processing speed. This makes them an asset in the redis database which takes the same approach. In this article, we will learn how to use hashes in Redis.

Setting up Redis

For setting up Redis, I would recommend using a service for you in prod. Azure for example, has a great redis service that scales easily. However, you will want to learn redis and eventually how to scale it yourself. This will help with debugging cloud services or eventually, saving money and not using them.

We will start our intro to redis via using docker compose. Create a docker-compose.yml file and add the following.

version: "3.2"
services:
  redis:
    image: "redis:alpine"
    command: redis-server
    ports:
      - "6379:6379"
    volumes:
      - $PWD/redis-data:/var/lib/redis
      - $PWD/redis.conf:/usr/local/etc/redis/redis.conf
    environment:
      - REDIS_REPLICATION_MODE=master

Ensure you have docker installed and run

docker-compose up

Installing Redis Modules

There are two modules I see often used in nodejs. I will tend towards ioredis as it has built in support for promises and many other features in redis.

npm install ioredis

Writing the Code

Let's open up a new file, index.js and go through many of the common commands you will used with lists in redis.

Hash Set

We can create a hash and set a key using the following. The example below is similar to creating a hash/object/dictionary like so:

const person1Hash = {
  name: 'jane'
}
// Hash Set
const result = await redis.hset("person1-hash", "name", "jane")
console.log(result)

For each of the examples below, I will use the following template to run all the commands. Here is my full index file. We will just replace the commands each time.

const Redis = require("ioredis")

const redis = new Redis({})

async function main() {
  // Hash Set
  const result = await redis.hset("person1-hash", "name", "jane")
  console.log(result)
}

(async () => {
    await main()
})()

Hash Multiple Set

We can set multiple keys using the hmset function. Below we set the name and hage of a new hash.

// Hash Multiple Set
const result = await redis.hmset("person1-hash", "name", "jane", "age", 20)
console.log(result)

Hash Get

We can get a field on a hash using the hget function. The example below gets the name value of our hash.

// Hash Get
const result = await redis.hget("person1-hash", "name")
console.log(result)

Hash Multiple Get

If we want to access multiple fields from a single hash, we can use hmget function. Below we retrieve the name and age values from our hash.

// Hash Multiple Get
  const result = await redis.hmget("person1-hash", "name", "age")
  console.log(result)

Hash Get All

If we want to get all keys from a hash, we can use hget.

// Hash Get All
  const result = await redis.hget("person1-hash")
  console.log(result)

Hash Exists

If we use hexists we can check if a hash or a property on a hash exists. Here are some examples of checking fields and a hash.

// Hash Exists
const result = await redis.hexists("person1-hash", "name")
console.log(result)

const result2 = await redis.hexists("person1-hash", "age")
console.log(result2)

const result3 = await redis.hexists("person2-hash", "name")
console.log(result3)

Hash Delete

To delete a hash field we use the hdel function. This will remove fields from our hash.

// Hash Del
const result = await redis.hdel("person1-hash", "name")
console.log(result)

const result2 = await redis.hdel("person1-hash", "name", "age")
console.log(result2)

Hash Increment By

Hashes also support Increment By if the field values are numeric. For example, we can increment the money a hash has using the following.

await redis.hset("person1-hash", "money", 100)

// Hash increment by
const result = await redis.hincrby("person1-hash", "money", 10)
console.log(result)

const result2 = await redis.hincrby("person1-hash", "money", -20)
console.log(result2)

Hash Keys

We get gey all keys for a hash using the hkeys function.

// Hash keys
const result = await redis.hkeys("person1-hash")
console.log(result)

Hash Length

To get the length of number of keys in a hash, we can use hlen.

// Hash length
const result = await redis.hlen("person1-hash")
console.log(result)