Python-redis Connection Management

Source: Internet
Author: User


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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.