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.
Redis is an in-memory data structure storage that provides five basic data types , each type has unique properties and uses.
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>
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>
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>
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>
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>
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!