Redis Data types
Redis supports five types of data:
String (String)
Hash (hashed)
List (lists)
Set (SET)
Zset (sorted set: Ordered set)
String
Key:name
Value:runoob
128.127.0.0.1:6379> SET name "Runoob"
Ok
127.0.0.1:6379> GET Name
"Runoob"
127.0.0.1:6379> TYPE Name
String
Hash (that is, dictionary type)
127.0.0.1:6379> hmset myhash field1 "Hello" Field2 "World"
Ok
127.0.0.1:6379> Hget Myhash field1
"Hello"
127.0.0.1:6379> Hget Myhash Field2
"World"
127.0.0.1:6379> TYPE Myhash
Hash
List (lists)
The Redis list is a simple list of strings, sorted by insertion order. You can add an element to the head of the list (to the left) or to the tail (to the right).
127.0.0.1:6379> Lpush Runoob Redis
(integer) 1
127.0.0.1:6379> Lpush Runoob MongoDB
(integer) 2
127.0.0.1:6379> Lpush Runoob RABITMQ
(integer) 3
127.0.0.1:6379> Lpush Runoob Memcas
(integer) 4
127.0.0.1:6379> lrange Runoob 0 10
1) "Memcas"
2) "RABITMQ"
3) "MongoDB"
4) "Redis"
The list can store up to 232-1 elements (4294967295, each of which can store more than 4 billion).
Python Connect to Redis
import redis
#String operation
r = redis.Redis (host = ‘192.168.1.48’, port = 6379)
print (r.get ("name"). decode ())
r.set (‘mystring’, ‘good good study, day day up!’)
print (r.get ("mystring"))
The #Redis Mset command is used to set one or more key-value pairs at the same time.
r.mset (name1 = "ling", name2 = "ajing", name3 = "shang")
print (r.mget ("name1", "name2", "name3"))
print (r.type ("name1"))
Results:
Python-redis Connection Management