search
HomeDatabaseMysql TutorialNosql之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不存在时,返回一个空表。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.