Redis tutorial (4): Hashes data type
1. Overview:
We can think of the Hashes type in Redis as a map container with String Key and String Value. So this type is very suitable for storing information about value objects. Such as Username, Password and Age, etc. If the Hash contains few fields, this type of data will also occupy very little disk space. Each Hash can store 4294967295 key-value pairs.
2. Related command list:
Command prototype | Time complexity | Command description | Return value |
HSET key field value | O(1) | Set the Field/Value pair for the specified Key. If the Key does not exist, this command will create a new Key with the Field/Value in the parameter. Yes, if the Field in the parameter already exists in the Key, the original value will be overwritten with the new value. | 1 means that the new Field has been set with a new value, 0 means that the Field already exists, and will be overwritten with the new value. Original value |
HGET key field | O(1) | Returns the associated value of the specified Field in the specified Key. | Return the associated value of the Field in the parameter. If the Key or Field in the parameter does not exist, return nil. |
HEXISTSkey field | O(1) | Determine whether the specified Field in the specified Key exists. | 1 means it exists, 0 means the Field or Key in the parameter does not exist. |
HLEN key | O(1) | Get the number of Fields contained in the Key. | Returns the number of Fields contained in Key. If Key does not exist, returns 0. |
HDEL key field [field ...] | O(N) | N in time complexity represents the field to be deleted in the parameter quantity. Delete multiple fields specified in the parameters from the Hashes Value of the specified Key. If the fields do not exist, they will be ignored. If the Key does not exist, it is treated as empty Hashes and returns 0. | The number of Fields actually deleted. |
HSETNXkey field value | O(1) | Only when the Key or Field in the parameter does not exist, set the specified Key Define the Field/Value pair, otherwise the command will not perform any operation. | 1 means that the new Field has been set with a new value, 0 means that the Key or Field already exists, and this command does not perform any operation. |
HINCRBYkey field increment | O(1) | Increase the value of the Value associated with the specified Field in the specified Key. If the Key or Field does not exist, this command will create a new Key or Field, initialize its associated Value to 0, and then specify the operation of increasing the number. The numbers supported by this command are 64-bit signed integers, that is, the increment can be negative. | Return the value after the operation |
HGETALLkey | O(N) | The N in the time complexity indicates that the Key contains Field quantity. Get all Field/Value contained in this key. The return format is a Field, a Value, and so on. | List of Field/Value |
HKEYSkey | O(N) | The N in the time complexity indicates that the Key contains Field quantity. Returns all Fields names for the specified Key. | List of Fields. |
HVALSkey | O(N) | The N in the time complexity represents the number of Fields contained in the Key. Returns all Values names of the specified Key. | List of Values. |
MGETkey field [field ...] | O(N) | The N in the time complexity represents the number of fields requested. Gets a set of Values associated with the Fields specified in the parameter. If the requested Field does not exist, its value returns nil. If the Key does not exist, the command treats it as an empty hash and therefore returns a set of nil. | Returns a set of Values associated with the requested Fields. The return order is equal to the request order of the Fields. |
HMSET key field value [field value ...] | O(N) | N in time complexity represents the Field being set quantity. Set the Field/Value pairs given in the parameters pair by pair. If one of the Fields already exists, the original value will be overwritten with the new value. If the Key does not exist, create a new Key and set the Field/Value in the parameters. |
3. Command examples:
1. HSET/HGET/HDEL/HEXISTS/HLEN/HSETNX:
#在Shell命令行启动Redis客户端程序 /> redis-cli #给键值为myhash的键设置字段为field1,值为stephen。 redis 127.0.0.1:6379> hset myhash field1 "stephen" (integer) 1 #获取键值为myhash,字段为field1的值。 redis 127.0.0.1:6379> hget myhash field1 "stephen" #myhash键中不存在field2字段,因此返回nil。 redis 127.0.0.1:6379> hget myhash field2 (nil) #给myhash关联的Hashes值添加一个新的字段field2,其值为liu。 redis 127.0.0.1:6379> hset myhash field2 "liu" (integer) 1 #获取myhash键的字段数量。 redis 127.0.0.1:6379> hlen myhash (integer) 2 #判断myhash键中是否存在字段名为field1的字段,由于存在,返回值为1。 redis 127.0.0.1:6379> hexists myhash field1 (integer) 1 #删除myhash键中字段名为field1的字段,删除成功返回1。 redis 127.0.0.1:6379> hdel myhash field1 (integer) 1 #再次删除myhash键中字段名为field1的字段,由于上一条命令已经将其删除,因为没有删除,返回0。 redis 127.0.0.1:6379> hdel myhash field1 (integer) 0 #判断myhash键中是否存在field1字段,由于上一条命令已经将其删除,因为返回0。 redis 127.0.0.1:6379> hexists myhash field1 (integer) 0 #通过hsetnx命令给myhash添加新字段field1,其值为stephen,因为该字段已经被删除,所以该命令添加成功并返回1。 redis 127.0.0.1:6379> hsetnx myhash field1 stephen (integer) 1 #由于myhash的field1字段已经通过上一条命令添加成功,因为本条命令不做任何操作后返回0。 redis 127.0.0.1:6379> hsetnx myhash field1 stephen (integer) 0
2. HINCRBY:
#删除该键,便于后面示例的测试。 redis 127.0.0.1:6379> del myhash (integer) 1 #准备测试数据,该myhash的field字段设定值1。 redis 127.0.0.1:6379> hset myhash field 5 (integer) 1 #给myhash的field字段的值加1,返回加后的结果。 redis 127.0.0.1:6379> hincrby myhash field 1 (integer) 6 #给myhash的field字段的值加-1,返回加后的结果。 redis 127.0.0.1:6379> hincrby myhash field -1 (integer) 5 #给myhash的field字段的值加-10,返回加后的结果。 redis 127.0.0.1:6379> hincrby myhash field -10 (integer) -5
3. HGETALL/HKEYS/HVALS/HMGET/HMSET:
#删除该键,便于后面示例测试。 redis 127.0.0.1:6379> del myhash (integer) 1 #为该键myhash,一次性设置多个字段,分别是field1 = "hello", field2 = "world"。 redis 127.0.0.1:6379> hmset myhash field1 "hello" field2 "world" OK #获取myhash键的多个字段,其中field3并不存在,因为在返回结果中与该字段对应的值为nil。 redis 127.0.0.1:6379> hmget myhash field1 field2 field3 1) "hello" 2) "world" 3) (nil) #返回myhash键的所有字段及其值,从结果中可以看出,他们是逐对列出的。 redis 127.0.0.1:6379> hgetall myhash 1) "field1" 2) "hello" 3) "field2" 4) "world" #仅获取myhash键中所有字段的名字。 redis 127.0.0.1:6379> hkeys myhash 1) "field1" 2) "field2" #仅获取myhash键中所有字段的值。 redis 127.0.0.1:6379> hvals myhash 1) "hello" 2) "world"
The above is the Redis tutorial (4): Hashes data type For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

Redis, as a message middleware, supports production-consumption models, can persist messages and ensure reliable delivery. Using Redis as the message middleware enables low latency, reliable and scalable messaging.

The steps to start a Redis server include: Install Redis according to the operating system. Start the Redis service via redis-server (Linux/macOS) or redis-server.exe (Windows). Use the redis-cli ping (Linux/macOS) or redis-cli.exe ping (Windows) command to check the service status. Use a Redis client, such as redis-cli, Python, or Node.js, to access the server.
