Home >Database >Redis >Does the redis database have data types? what type

Does the redis database have data types? what type

尚
Original
2019-07-01 15:16:342751browse

Does the redis database have data types? what type

Redis supports five data types: string (string), hash (hash), list (list), set (set) and zset (sorted set: ordered set) .

Introduction:

1. String:

String is the most basic type of redis. You can understand it as exactly the same type as Memcached. One key corresponds to one value.

The string type is binary safe. This means that the string of redis can contain any data. For example, jpg images or serialized objects.

The string type is the most basic data type of Redis. The string type value can store up to 512MB.

Example:

redis 127.0.0.1:6379> SET name "runoob"
OK
redis 127.0.0.1:6379> GET name
"runoob"

2. hash

Redis hash is a collection of key-value (key=>value) pairs.

Redis hash is a mapping table of string type fields and values. Hash is particularly suitable for storing objects.

Example:

redis 127.0.0.1:6379> DEL runoobredis 127.0.0.1:6379> HMSET myhash field1 "Hello" field2 "World"
"OK"
redis 127.0.0.1:6379> HGET myhash field1
"Hello"
redis 127.0.0.1:6379> HGET myhash field2
"World"

3, list

Redis list is a simple list of strings, sorted in insertion order. You can add an element to the head (left) or tail (right) of the list.

Example:

redis 127.0.0.1:6379> DEL runoob
redis 127.0.0.1:6379> lpush runoob redis
(integer) 1
redis 127.0.0.1:6379> lpush runoob mongodb
(integer) 2
redis 127.0.0.1:6379> lpush runoob rabitmq
(integer) 3
redis 127.0.0.1:6379> lrange runoob 0 10
1) "rabitmq"
2) "mongodb"
3) "redis"
redis 127.0.0.1:6379>

A list can store up to 232 - 1 elements (4294967295, each list can store more than 4 billion).

4. Set (set)

Redis’ Set is an unordered collection of string type.

Sets are implemented through hash tables, so the complexity of adding, deleting, and searching is O(1).

sadd command

Adds a string element to the set set corresponding to key, returning 1 if successful, 0 if the element is already in the set, and an error if the set corresponding to key does not exist.

sadd key member

Example:

redis 127.0.0.1:6379> DEL runoob
redis 127.0.0.1:6379> sadd runoob redis
(integer) 1
redis 127.0.0.1:6379> sadd runoob mongodb
(integer) 1
redis 127.0.0.1:6379> sadd runoob rabitmq
(integer) 1
redis 127.0.0.1:6379> sadd runoob rabitmq
(integer) 0
redis 127.0.0.1:6379> smembers runoob
1) "redis"
2) "rabitmq"
3) "mongodb"

5. zset (sorted set: ordered set)

Redis zset, like set, is also a collection of string type elements, and duplicates are not allowed member.

The difference is that each element is associated with a double type score. Redis uses scores to sort the members of the collection from small to large.

The members of zset are unique, but the scores can be repeated.

zadd command

Add elements to the collection, and update the corresponding score if the element exists in the collection

Instance:

redis 127.0.0.1:6379> DEL runoob
redis 127.0.0.1:6379> zadd runoob 0 redis
(integer) 1
redis 127.0.0.1:6379> zadd runoob 0 mongodb
(integer) 1
redis 127.0.0.1:6379> zadd runoob 0 rabitmq
(integer) 1
redis 127.0.0.1:6379> zadd runoob 0 rabitmq
(integer) 0
redis 127.0.0.1:6379> > ZRANGEBYSCORE runoob 0 1000
1) "mongodb"
2) "rabitmq"
3) "redis"

More Redis related For knowledge, please visit the Redis usage tutorial column!

The above is the detailed content of Does the redis database have data types? what type. For more information, please follow other related articles on the PHP Chinese website!

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
Previous article:When to use redisNext article:When to use redis