Redis data type:
Redis supports five basic data types: string (string), hash (hash), list (listing), set (set), and Zset (ordered collection).
1.string (String): String type is the most basic data type of Redis, and a key can store 512MB maximum.
The string type is binary safe and can contain any data, such as a JPG picture or a serialized object.
Example:
>>> Import Redis
>>> r = Redis. Redis ()
>>> r.set (' name ', ' Xiemanr ')
True
>>> r.get (' name ')
B ' Xiemanr '
The key in the instance is name, and the value is string Xiemanr.
2.hash (hash):Hash is a set of key name pairs.
The hash is a string-type key and value mapping table, which is particularly useful for storing objects.
Each hash can store 2 32-1 key value pairs (more than 4 billion).
Example:
>>> Import Redis
>>> r = Redis. Redis ()
>>> r.hset (' Xiemanr ', ' age ', @)
1
>>> r.hget (' Xiemanr ', ' age ')
B ' 18 '
The name of the hash in the instance is Xiemanr,key as age, the value is 18, and the value of the key age is obtained using Hget.
3.list (list):Lists are list of strings, sorted by insertion order. You can add an element to the left or right of the list. Each list can store up to 2 32-1 elements (more than 4 billion)
Example:
>>> Import Redis
>>> r = Redis. Redis ()
>>> r.lpush (' Lang ', ' Python ')
>>> r.lpush (' Lang ', *[' Java ', ' Go ', ' PHP ', ' C + + ', ' JS ') ]
>>> r.lrange (' Lang ', 0, 3)
[b ' JS ', B ' C + + ', B ' PHP ', B ' go ']
>>>
4.set (SET):Set is a string-type unordered collection that is implemented by a hash table, so adding, deleting, and finding the complexity are all O (1).
sadd Function:Adds a string element to the set set that corresponds to the key, succeeds in returning 1, if the element has returned 0,key in the collection, there is no return error.
smembers function:Gets the value of the set collection.
Example:
>>> Import Redis
>>> r = Redis. Redis ()
>>> r.sadd (' sql ', ' MySQL ')
1
>>> r.sadd (' sql ', ' SQLite ')
1
>> > R.sadd (' sql ', ' Redis ')
1
>>> r.sadd (' sql ', ' MySQL ')
0
>>> r.smembers (' SQL ')
{b ' SQLite ', b ' MySQL ', b ' redis '}
instance, MySQL was added two times because the elements within the collection are unique, and the second inserted element is ignored to return 0.
5.zset (ordered set):Zset and set are also collections of string-type elements and do not allow duplicate members.
The difference is that each element is associated with a double type of score. Redis is the sort of small to large members of a collection through fractions.
Zset members are unique, but fractions (score) can be repeated.
zadd function:Adds an element to the collection, and the element exists in the collection, updating the corresponding score.
zrangebyscore function:Gets the value that is sorted by fractions.
Example:
>>> Import Redis
>>> r = Redis. Redis ()
>>> r.zadd (' SqlType ', ' Redis ', 0.1)
1
>>> r.zadd (' SqlType ', ' MySQL ', 0.5)
1
>>> r.zadd (' SqlType ', ' SQLite ', 0.4)
1
>>> r.zrangebyscore (' SqlType ', 0, 2)
[ B ' Redis ', b ' SQLite ', b ' MySQL ']