Redis Database Operations

Source: Internet
Author: User
Tags install redis

The first thing to do is pip install Redis

Import Redis,json
ip=' 211.149.218.16 '
port=6379
db=0
Passwd=' 123456 '
R = Redis. Redis (Host=ip,Port=port,Db=db,PASSWORD=PASSWD)#链接 Redis
R.set (' Name ',' Nhy ')#如果key一样则冲掉原来的值
R.set (' Name ', [1,2,3,4])
#r. Set (' name ', {' name ': ' hehehe '})
Name = R.get (' Name ')
Name.decode ()#转成字符串, String type, Redis contains string, bytes type, to be converted to string with decode ()
New_name = Json.loads (Name.decode ())#用json转成list
Print (name)
Print (new_name)
PrintType (new_name))
R.setex (' Nhy ',' Lingyul ',10)#设置key, value and time of failure seconds
R.mset (Name=' HH ',Age=' 18 ')#批量set值, batch setup cannot set expiration time
Print (R.keys (' *n* '))#打印所有key

#哈希类型的key
R.hset (' Lingyul ',' User ',' Lingyul ') #r. Hset (' key ', ' key ', ' value ') to get the result:
# row key value
# 1 user lingyul
print (R.hget ( ' Lingyul ', #单独获取指定的name的值
R.hgetall ( Lingyul ') #全部获取
R.delete ( ' key ') #吧整个key都删掉
R.hdel ( ' Lingyul ', ' user ') #删除指定的key
  r.set ( ' User:lingyul ',  ' hahaha ')  #如果设置的key里面有冒号, first create a folder to the left of the colon, There are key and value under the folder, and the hash type is the same     
                 

Redis Database Operations

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.