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 r.
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/r.conf:/usr/local/etc/redis/r.conf
environment:
- REDIS_REPLICATION_MODE=master
Ensure you have docker installed and run
docker-compose up
In python, the main used redis module is called redis-py
and can be installed using the follows.
pip install redis
Let's open up a new file, index.py
and go through many of
the common commands you will used with lists in r.
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:
person1Hash = {
"name": 'jane'
}
# Hash Set
result = r.hset("person1-hash", "name", "jane")
print(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.
import redis
r = redis.Redis(host='localhost', port=6379, db=0)
# Hash Set
result = r.hset("person1-hash", "name", "jane")
print(result)
We can set multiple keys using the hmset function. Below we set the name and hage of a new hash.
# Hash Multiple Set
result = r.hmset("person1-hash", {
"name": "jane",
"age": 20
})
print(result)
We can get a field on a hash using the hget
function. The example below gets the name value of our hash.
# Hash Get
result = r.hget("person1-hash", "name")
print(result)
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
result = r.hmget("person1-hash", "name", "age")
print(result)
If we want to get all keys from a hash, we can use hget
.
# Hash Get All
result = r.hget("person1-hash")
print(result)
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
result = r.hexists("person1-hash", "name")
print(result)
const result2 = await r.hexists("person1-hash", "age")
print(result2)
const result3 = await r.hexists("person2-hash", "name")
console.log(result3)
To delete a hash field we use the hdel
function. This will remove fields from our hash.
# Hash Del
result = r.hdel("person1-hash", "name")
print(result)
const result2 = await r.hdel("person1-hash", "name", "age")
print(result2)
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 r.hset("person1-hash", "money", 100)
# Hash increment by
result = r.hincrby("person1-hash", "money", 10)
print(result)
const result2 = await r.hincrby("person1-hash", "money", -20)
print(result2)
We get gey all keys for a hash using the hkeys
function.
# Hash keys
result = r.hkeys("person1-hash")
print(result)
To get the length of number of keys in a hash, we can use hlen
.
# Hash length
result = r.hlen("person1-hash")
print(result)