Redis Python uses Redis

Source: Internet
Author: User
Tags set set install redis redis server

Python using Redis

Reference Documentation:

Http://redis.cn/clients.html#python

Https://github.com/andymccurdy/redis-py

Installing Redis
$ sudo pip install redis
A simple Redis operation

Strings string Manipulation

in [1]:Import Redisin [2]: R = Redis. Strictredis (host=' localhost ', port=6379, db=0, password=' foobared ') in [3]: R.set (' foo ', ' bar ') out[3]: Truein [4]: r.get (' foo ') out[4]: ' bar ' in [5]: r[' foo ']out[5]: ' bar ' in [ 6]: r.delete (' foo ') out[6]: 1In [7]: r.get (' foo ')      
Pipeline operation

A pipeline (pipeline) is a subclass of a base class in which Redis buffers multiple server commands in a single request. It greatly improves the ability to perform bulk commands by reducing the repeated TCP database packets between servers-clients.

>>> p = r.pipeline()        --创建一个管道>>> p.set(‘hello‘,‘redis‘)>>> p.sadd(‘faz‘,‘baz‘)>>> p.incr(‘num‘)>>> p.execute()[True, 1, 1]>>> r.get(‘hello‘)‘redis‘

The commands of a pipeline can be written together, such as:

>>> p.set(‘hello‘,‘redis‘).sadd(‘faz‘,‘baz‘).incr(‘num‘).execute()1

By default, commands executed in the pipeline guarantee the atomicity of execution, and execution pipe = r.pipeline(transaction=False) can disable this feature.

String scenario – Number of page hits

Suppose we need to record the number of clicks on a series of pages. For example, each post in the Forum has to record the number of clicks, and the number of clicks is much more than the number of replies. If you use a relational database to store clicks, there may be a lot of row-level lock contention. So, it's best to increase the number of clicks by using Redis's INCR command.

When the Redis server starts, you can read the initial value of the hits from the relational database (1237 this page was accessed 34,634 times)

>>> r.set("visit:1237:totals",34634)True

Whenever there is a page click, use INCR to increase the number of clicks.

>>> r.incr("visit:1237:totals")34635>>> r.incr("visit:1237:totals")34636

This value can be obtained directly when the page is loaded

>>> r.get ("visit:1237:totals")‘34636‘
Use hash types to save diverse objects

Application Scenarios

For example, we want to store a user information object data, the user's name, age, birthday, etc., modify the value of an item. The hash structure of Redis enables you to Update modify only one item property value as if it were a property in a database.

The Redis hash is actually a hashmap that is stored internally, and provides an interface to directly access the map member, such as:

>>> R.hset (' Users:jdoe ',' Name ',"John Doe")1L>>> R.hset (' Users:jdoe ',' Age ',25)1L>>> R.hset (' Users:jdoe ',' Birthday ',' 19910101 ')1L>>> R.hgetall (' Users:jdoe ') {' Age ':' 26 ',' Birthday ': ' 19910101 ',  ' name ':  ' John Doe '}< Span class= "Hljs-prompt" >>>> R.hkeys ( ' Users:jdoe ') [ ' Name ',  ' birthday ']>> > R.hincrby ( ' Users:jdoe ',  ' age ', 1) 26l>>> r.hgetall ( "Users: JDoe ') >>> { ' age ':  ' 26 ', Span class= "hljs-string" > ' birthday ':  ' 19910101 ',  ' name ':  ' John Doe '}            
Set Set Application scenario – social circle data

In social networking sites, each circle (circle) has its own user base. A circle can find people with common characteristics (such as a sport, games, movies, etc.). When a user joins one or more circles, the system can recommend people in the circle to the user. We define such two circles and join some of the circle members.

>>> R.sadd (' Circle:game:lol ',' User:debugo ')1>>> r.sadd ( ' Circle:game : Lol ',  ' User:leo ') 1>>> R.sadd ( ' circle:game:lol ',  ' User:guo ') 1>>> r.sadd ( circle: Soccer:intermilan ',  ' User:guo ') 1 >>> R.sadd ( ' Circle:soccer:InterMilan ',  ' User:levis ' ) 1>>> r.sadd ( circle: Soccer:intermilan ',  ' User:leo ') 1     

Get a member of a circle

>>> r.smembers(‘circle:game:lol‘)set([‘user:Guo‘, ‘user:debugo‘, ‘user:leo‘])

You can use set operations to get a common member of several circles:

>>> r.sinter(‘circle:game:lol‘, ‘circle:soccer:InterMilan‘)set([‘user:Guo‘, ‘user:leo‘])>>> r.sunion(‘circle:game:lol‘, ‘circle:soccer:InterMilan‘)set([‘user:Levis‘, ‘user:Guo‘, ‘user:debugo‘, ‘user:leo‘])

Recommended Games soccer:InterMilan :

>>> r.sdiff(‘circle:soccer:InterMilan‘, ‘circle:game:lol‘)>>> {‘user:Levis‘}

Redis Python uses 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.