Home > Database > Redis > body text

How to use five data types of redis

下次还敢
Release: 2024-04-19 20:30:42
Original
854 people have browsed it

Redis provides five data types: String: stores a single string value. Hash table: stores key-value pairs, used for objects or maps. List: Stores an ordered sequence of elements, used in arrays or queues. Collection: stores unique elements, used for unique values ​​or labels. Sorted set: stores elements with fractions, sorted by fraction from low to high.

How to use five data types of redis

How to use Redis’s five data types

Redis is an in-memory data structure storage that provides five basic data types , each type has unique properties and uses.

1. String (String)

Purpose: Stores a single string value, used to store simple text, numbers or JSON strings.

Usage:

<code># 设置字符串值
SET my_string "Hello World"

# 获取字符串值
GET my_string</code>
Copy after login

2. Hash table (Hash)

Purpose: Store key-value pairs, use For storing objects or mappings.

Usage:

<code># 设置哈希表值
HSET my_hash field1 "value1"
HSET my_hash field2 "value2"

# 获取哈希表值
HGET my_hash field1</code>
Copy after login

3. List

Purpose: To store an ordered sequence of elements, use For storing arrays or queues.

Usage:

<code># 入队到列表
LPUSH my_list element1
LPUSH my_list element2

# 出队元素
LPOP my_list</code>
Copy after login

4. Set

Purpose: Stores unique elements for Store a unique value or label.

Usage:

<code># 添加元素到集合
SADD my_set element1
SADD my_set element2

# 检查元素是否存在
SISMEMBER my_set element1</code>
Copy after login

5. Sorted Set

Usage: Store elements with fractions , sorted by scores from small to large, used to store rankings or priority queues.

Usage:

<code># 添加元素到有序集合,并指定分数
ZADD my_sorted_set 10 element1
ZADD my_sorted_set 20 element2

# 获取分数为 10 的元素
ZRANGEBYSCORE my_sorted_set 10 10</code>
Copy after login

The above is the detailed content of How to use five data types of redis. 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