Nosql之Redis: hash数据类型及操作命令
hash类型 ? 是一个string类型的field和value的映射表. ? 它的添加,删除操作都是0(1)(平均). ? 适合用于存储对象. 1: hset? 描述: 设置哈希表key中域field的值设为value,如里key不存在,则先创建.存在则覆盖. 返回: 如果field是哈希表中新创建的,则返回1.已经
hash类型
? 是一个string类型的field和value的映射表.
? 它的添加,删除操作都是0(1)(平均).
? 适合用于存储对象.
1: hset?
描述: 设置哈希表key中域field的值设为value,如里key不存在,则先创建.存在则覆盖.
返回: 如果field是哈希表中新创建的,则返回1.已经存在且旧值已被新值覆盖,刚返回0.
操作如下:
redis 127.0.0.1:6379> hset uinfo_1 name tw
(integer) 1
redis 127.0.0.1:6379> hget uinfo_1 name
“tw”
redis 127.0.0.1:6379> hset uinfo_1 name tw2
(integer) 0
redis 127.0.0.1:6379> hget uinfo_1 name
“tw2″
2:hget
描述:返回哈希表KEY中给定域field的值.
返回: 如果存在返回指定field的值,不存在时返回nil.
操作如下:
redis 127.0.0.1:6379> hget user_1 name
(nil)
redis 127.0.0.1:6379> hget uinfo_1 name
“tw2″
3:hsetnx
描述:将哈希表key中的域field的值设置为value,当且仅当域field不存在。若域field已经存在,该操作无效。如果key不存在,一个新哈希表被创建并执行HSETNX命令.
返回值: 1表示新的Field被设置了新值,0表示Key或Field已经存在,该命令没有进行任何操作。
操作如下:
先查看key中所有字段及值
redis 127.0.0.1:6379> hgetall uinfo_1
1) “name”
2) “tw2″
3) “age”
4) “19″
redis 127.0.0.1:6379> hsetnx uinfo_1 sex 1
(integer) 1
redis 127.0.0.1:6379> hsetnx uinfo_1 sex 0
(integer) 0
redis 127.0.0.1:6379> hgetall uinfo_1
1) “name”
2) “tw2″
3) “age”
4) “19″
5) “sex”
6) “1″
redis 127.0.0.1:6379> hsetnx uinfo_2 name tw2
(integer) 1
redis 127.0.0.1:6379> hgetall uinfo_2
1) “name”
2) “tw2″
?
4: hmset
描述: 同时交多个field-value设置到哈希表中.此命令会覆盖哈希表中已存在的域。如果key不存在,一个空哈希表被创建并执行hmset操作。
返回值:执行成功返回ok,当key不是哈希类型时,返回一个错误.
操作如下:
redis 127.0.0.1:6379> set title_1 val_1
OK
redis 127.0.0.1:6379> hmset title_1 val_2
(error) ERR wrong number of arguments for ‘hmset’ command
redis 127.0.0.1:6379> hmset uinfo_1 name aaa age 30
OK
redis 127.0.0.1:6379> hgetall uinfo_1
1) “name”
2) “aaa”
3) “age”
4) “30″
5) “sex”
6) “1″
5:hmget
描述: 返回哈希表key中,一个或多个给定域的值。如果给定的域不存在于哈希表,那么返回一个nil值。
返回值:返回查询域的值.
操作如下:
redis 127.0.0.1:6379> hmget uinfo_1 name age
1) “aaa”
2) “30″
6:hgetall
描述:返回哈希表key中,所有的域和值。在返回值里,紧跟每个域(field name)之后是域的值
(value),所以返回值的长度是哈希表大小的两倍。
返回值:以列表形式返回哈希表中域和值.如果key不存,返回空列表.
操作:见前面hsetnx.
7:hdel
描述:删除哈希表key中的一个或多个指定域,不存在的域将被忽略。
返回值:被成功移除的域的数量,不包括被忽略的域。
操作如下:
redis 127.0.0.1:6379> hgetall uinfo_1
1) “name”
2) “aaa”
3) “age”
4) “30″
5) “sex”
6) “1″
redis 127.0.0.1:6379> hdel uinfo_1 name age
(integer) 2
redis 127.0.0.1:6379> hgetall uinfo_1
1) “sex”
2) “1″
8: hexists
描述:查看哈希表key中,给定域field是否存在
返回值:存在返回1,不存在返回0。
操作如下:
redis 127.0.0.1:6379> hexists uinfo_1 name
(integer) 0
redis 127.0.0.1:6379> hexists uinfo_1 sex
(integer) 1
9: hincrby
描述:为哈希表key中的域field的值加上增量increment。增量也可以为负数,相当于对给定域进
行减法操作。
返回值:执行HINCRBY命令之后,哈希表key中域field的值。
操作如下:
redis 127.0.0.1:6379> hgetall uinfo_1
1) “sex”
2) “1″
redis 127.0.0.1:6379> hincrby uinfo_1 sex 20
(integer) 21
redis 127.0.0.1:6379> hincrby uinfo_1 sex -10
(integer) 11
10:hlen
描述:返回哈希表key中域的数量。
返回值:哈希表中域的数量。当key不存在时,返回0。
11: hkeys
描述:返回哈希表key中的所有域。
返回值:一个包含哈希表中所有域的表。当key不存在时,返回一个空表。
12,hvals
描述:返回哈希表key中的所有值。
返回值:一个包含哈希表中所有值的表。当key不存在时,返回一个空表。

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

Redis uses hash tables to store data and supports data structures such as strings, lists, hash tables, collections and ordered collections. Redis persists data through snapshots (RDB) and append write-only (AOF) mechanisms. Redis uses master-slave replication to improve data availability. Redis uses a single-threaded event loop to handle connections and commands to ensure data atomicity and consistency. Redis sets the expiration time for the key and uses the lazy delete mechanism to delete the expiration key.

Steps to solve the problem that redis-server cannot find: Check the installation to make sure Redis is installed correctly; set the environment variables REDIS_HOST and REDIS_PORT; start the Redis server redis-server; check whether the server is running redis-cli ping.

Redis cluster is a distributed deployment model that allows horizontal expansion of Redis instances, and is implemented through inter-node communication, hash slot division key space, node election, master-slave replication and command redirection: inter-node communication: virtual network communication is realized through cluster bus. Hash slot: divides the key space into hash slots to determine the node responsible for the key. Node election: At least three master nodes are required, and only one active master node is ensured through the election mechanism. Master-slave replication: The master node is responsible for writing requests, and the slave node is responsible for reading requests and data replication. Command redirection: The client connects to the node responsible for the key, and the node redirects incorrect requests. Troubleshooting: fault detection, marking off line and re-

To view the Redis version number, you can use the following three methods: (1) enter the INFO command, (2) start the server with the --version option, and (3) view the configuration file.

To view all keys in Redis, there are three ways: use the KEYS command to return all keys that match the specified pattern; use the SCAN command to iterate over the keys and return a set of keys; use the INFO command to get the total number of keys.

Redis Ordered Sets (ZSets) are used to store ordered elements and sort by associated scores. The steps to use ZSet include: 1. Create a ZSet; 2. Add a member; 3. Get a member score; 4. Get a ranking; 5. Get a member in the ranking range; 6. Delete a member; 7. Get the number of elements; 8. Get the number of members in the score range.

Redis uses five strategies to ensure the uniqueness of keys: 1. Namespace separation; 2. HASH data structure; 3. SET data structure; 4. Special characters of string keys; 5. Lua script verification. The choice of specific strategies depends on data organization, performance, and scalability requirements.
