How to Use Redis Datatypes in PHP Part 2

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

This is part two in the tutorial series “How to use Redis Datatypes with PHP.” Part one of this series covered how to use GET, SET and EXISTS and examined how to use PHP to interacts with Redis elements. Part two will focus on how to use the hash commands for Redis. Hashes are used to located field and string values in Redis. In Redis, strings are the most basic type of value and are binary safe, meaning a Redis string can hold any type of data, including images.

Hash Commands for Redis

Hashes in Redis function as a map between various field and string values, making them an excellent data type to represent objects with multiple fields, such as a name and number. The hash commands for Redis are as follows:

  1. HSET — This command will set a key value on the hash. If the key does not already exist, a key holding a hash will be created.
  2. HGET — This command will return the value of a specified hash field.
  3. HDEL — This command will delete one or multiple hash field. Specified fields that don’t exist inside the hash are left out. If a key does not exist, it will be treated as an empty hash.
  4. HINCRBY — This command will increment the value of the hash object with a specified value.
  5. HGETALL — This command will get all the data in a hash.
  6. HMSET — This command will set multiple values to the specified multiple values.

An example of the HSET command in Redis

The following code will set the key “warlord” and a hashKey “name” then the value “Cao-cao”:

1
$redis->HSET("warlord", "name", "Cao-cao");

An example of the HGET command in Redis

The following code will get the value stored in hashKey “name” within the key “warlord”:

1
$redis->HGET("warlord", "name");

The results should look something like the following:

1
string(7) "Cao-cao"

An example of the HDEL command in Redis

As the description suggests, the following command will delete the hashKey “name” in the warlord key :

1
2
3
$redis->HDEL("warlord", "name");
$val=$redis->HGET("warlord", "name");
var_dump($val);

An example of the HINCRBY command in Redis

The following code will increment the value of the hashKey “star” by one (1) as specified:

1
2
3
4
$redis->HSET("warlord", "star", 3);
$redis->HINCRBY("warlord", "star", 1);
$val=$redis->HGET("warlord", "star");
var_dump($val);

As expected, the result is: string(1)"4"

An example of the HGETALL command in Redis

The following code uses the HSET command to set the values in the specified field for retrieving all the data using the HGETALL against the key “warlord”:

1
2
3
4
$redis->HSET("warlord", "country", "china");
$redis->HSET("warlord", "name", "Cao-cao");
$redis->HSET("warlord", "star", 3);
var_dump($redis->HGETALL("warlord"));

The result should look something like the following:

1
array(3) { ["name"]=> string(7) "Cao-cao" ["star"]=> string(1) "3" ["country"]=> string(5) "china" }

An example of the HMSET command in Redis

The following HMSET command will set the specified values for the key warlord:

1
2
3
4
5
6
7
8
$redis->HMSET("warlord", array(
"name" => "Nobunaga",
"country" => "Japan",
"star" => 5

);

var_dump($redis->hgetall("warlord"));

The result should look something like the following:

1
array(3) { ["name"]=> string(8) "Nobunaga" ["star"]=> string(1) "5" ["country"]=> string(5) "Japan" }

The values can now be retrieved using the HGETALL method.

Conclusion

This tutorial was part two in the series of “How to use Redis Datatypes with PHP.” This section covered how hashes work and the Redis hash commands. The six basics hash commands for Redis allow for 1) setting a key value on the hash; 2) returning the value of a specified hash field; 3) deleting one or multiple hash field; 4) incrementing the value of the hash object; 5) retrieving all the data in a hash; and 6) setting multiple values.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.