Python Simple Operation Redis

Source: Internet
Author: User
Tags install redis

8178°c
The previous article simply said the Linux installation Redis process, here is a little walkthrough with Python code, first to install Redis with PIP:
sudo pip install Redis
Then you can call it in Python and write some basics below, and write it according to the introduction of the Redis combat. The complex use of Redis is used later to learn, and the code is more intuitive:

Import Redis

Cache = Redis. Strictredis (host= ' localhost ', port=6379)

#1. Simple get and set operations
Print U ' ====set operation: '
Cache.set (' Blog:title ', U ' the5fire Technology blog ')
Print Cache.get (' Blog:title ')

#真实应用场景, batch set and get
For I in range (10):
Cache.mset ({
' Blog:post:%s:title '% i:u ' article%s title '% i,
' Blog:post:%s:content '% i:u ' article%s body '% i
})

Post_list = []
For I in range (10):
Post = Cache.mget (' blog:post:%s:title '% i, ' blog:post:%s:content '% i)
If post:
Post_list.append (POST)

For title, content in Post_list:
Print title, content

#2, hashed types of operations
Print U ' ====hashed operation: '
Cache.hset (' Blog:info ', ' title ', U ' the5fire's Technical blog ')
Cache.hset (' blog:info ', ' url ', u ' http://www.the5fire.com ')

Blog_info_title = Cache.hget (' blog:info ', ' title ')
Print Blog_info_title

Blog_info = Cache.hgetall (' Blog:info ')
Print Blog_info

#同样hashed类型的set和get也可以进行批量操作
Cache.hmset (' Blog:info ', {
' title ': ' The5fire blog ',
' URL ': ' http://www.the5fire.com ',
})
Blog_info1 = Cache.hmget (' blog:info ', ' title ', ' URL ')
Print Blog_info1

#3, lists types of operations
Print U ' ====lists operation: '
Cache.lpush (' blog:tags ', ' python ')
Cache.lpush (' blog:tags ', ' Linux ')
tags = cache.lrange (' Blog:tags ', 0, 2)
Print tags

#对应的还有rpush, Lpop,rpop, see more red pills for Redis combat

#4, sets types of operations
Print U ' ====sets operation: '
Cache.sadd (' Blog:category:python ', ' 001 ')
Cache.sadd (' Blog:category:python ', ' 002 ')
#cache. Sadd (' Blog:category:python ', ' 001 ', ' 002 ')

Print cache.smembers (' Blog:category:python ')
Cache.srem (' Blog:category:python ', ' 001 ')
Print cache.smembers (' Blog:category:python ')

Python Simple Operation Redis

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.