Home > Database > Redis > body text

Detailed explanation of Redis commands: key, string and hash

WBOY
Release: 2023-06-21 09:21:14
Original
1440 people have browsed it

Redis is a common high-performance key-value storage database. It supports multiple data types, such as string, hash, list, set, and sorted set, and provides various commands to operate on these data types.

In this article, we will take an in-depth look at the three most commonly used Redis data types: key, string, and hash, and introduce their common commands.

  1. key

Redis key is a string type and can contain any data. In Redis, keys are unique, and commands can be used to obtain, delete, and update keys.

The following are some common key commands:

  • SET key value: Set the value of key to value.
  • GET key: Get the value of key.
  • DEL key: Delete key.
  • EXISTS key: Check whether the key exists.
  • KEYS pattern: Get the key list matching pattern.

Example:

> SET name "John"
OK
> GET name
"John"
> DEL name
(integer) 1
> EXISTS name
(integer) 0
> SET age 30
OK
> KEYS *
1) "age"
Copy after login
  1. string

string is one of the most basic data types in Redis. It can contain any data, including binary data. The maximum length of string is 512MB.

The following are some common string commands:

  • SET key value: Set the value of key to value.
  • GET key: Get the value of key.
  • APPEND key value: Append value to the end of the key value.
  • STRLEN key: Get the length of the key value.
  • INCR key: Add 1 to the value of key.
  • DECR key: Decrease the value of key by 1.

Example:

> SET name "John"
OK
> GET name
"John"
> APPEND name " Doe"
(integer) 8
> GET name
"John Doe"
> STRLEN name
(integer) 8
> INCR age
(integer) 31
> DECR age
(integer) 30
Copy after login
  1. hash

hash is a special data type in Redis, which represents an associative array, in which each Each key is mapped to a value. Each hash can contain multiple key-value pairs. The advantage of hashing is that it makes it easier to store and retrieve complex data structures.

The following are some common hash commands:

  • HSET key field value: Set the value of the field in the key to value.
  • HGET key field: Get the value of the field in key.
  • HDEL key field [field ...]: Delete the field in the key.
  • HEXISTS key field: Check whether the field exists in the key.
  • HKEYS key: Get all fields in the key.

Example:

> HSET person name "John"
(integer) 1
> HSET person age 30
(integer) 1
> HGET person name
"John"
> HDEL person age
(integer) 1
> HEXISTS person age
(integer) 0
> HKEYS person
1) "name"
Copy after login

Summary

In this article, we took an in-depth look at the three most commonly used data types in Redis: key, string and hash, and introduces their common commands. Of course, Redis also supports several other data types, such as lists, sets, and sorted sets, each of which has its own specific uses.

If you are looking for a high-performance data storage solution, Redis may be a good choice, especially if you need to deal with complex data structures or need to use caching. Hope this article helps you!

The above is the detailed content of Detailed explanation of Redis commands: key, string and hash. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!