Python redis and pythonredis

Source: Internet
Author: User
Tags rabbitmq

Python redis and pythonredis

Preface: I originally wanted to write redis and rabbitMQ, but after writing redis, I feel a little bit more. rabbitMQ remains in the next blog ~~

For download and installation of redis and rabbitMQ, refer to redis & rabbitMQ Installation

 

I. redis basic operations-1
1 import redis 2 # establish a connection 3 r = redis. redis (host = "127.0.0.1", port = 6379) 4 5 all_keys = r. keys () # output all keys, list [B 'age', B' name', B 'occupation'] 6 for k in all_keys: 7 print (k, r. get (k) 8 9 print (r. keys () 10 11 r. set ("sister", "yongli", ex = 5) # Save it to the cache and clear 12 13 print (r. get ("sister") 14 15 r. set ("father", "jingxian", nx = True) # The current set operation executes 16 print (r. get ("father") 17 18 r. set ("AA", "BB", xx = True) # print (r. get ("AA") 20 21 22 r. mset (k1 = "v1", k2 = "v2") # batch set value 23 print (r. mget ("k1", "k2") # obtain values 24, 25, and 26 in batches. set ("id", "3114007487") 27 print (r. getrange ("id", 3, 6) # obtain the subsequence (slice, starting from 0) 28 29 r. setrange ("id", 3, "AAA") # modify the string content and replace 30 print (r. getrange ("id", 0,-1) # output: B '311aaa7487' 31 32 # "3" corresponds to the ASCII code 51, binary is 0011 001133 print (r. getbit ("id", 7) 34 r. setbit ("id", 7th) # Change The 0th bits to 0, and the 0011 bits start from 0010 235: 50 corresponds to print (r. getbit ("id", 7) 36 print (r. getbit ("id", 1000) # When the number of read digits exceeds, the 37 print (r. get ("id "))

Running result:

 1 b'age' b'22' 2 b'id' b'211AAA7487' 3 b'k2' b'v2' 4 b'k1' b'v1' 5 b'name' b'abc' 6 b'occupation' b'student' 7 b'father' b'BB' 8 [b'age', b'id', b'k2', b'k1', b'name', b'occupation', b'father'] 9 b'yongli'10 b'BB'11 None12 [b'v1', b'v2']13 b'4007'14 b'311AAA7487'15 116 017 018 b'211AAA7487'
View Code

Redis basic operations-2 (for expansion)

Getbit (name, offset) 1 # obtain a value (0 or 1) bitcount (key, start = None, end = None) in the binary representation of the value corresponding to name) 1 # obtain the number of 1 in the binary representation of the value corresponding to name 2 # parameter: 3 # key, Redis name4 # start, position starting position 5 # end, bit end position strlen (name) 1 # Return the length of the byte corresponding to the name value (3 bytes for one Chinese character) incr (self, name, amount = 1) it can be used to calculate the value corresponding to PV1 # auto-increment name. If the name does not exist, name = amount is created. Otherwise, auto-increment is performed. 2 3 # parameter: 4 # name, Redis name5 # amount, auto increment (must be an integer) 6 strlen (name) 1 # Return the length of the byte corresponding to the name value (3 bytes for one Chinese character) decr (self, name, amount = 1) 1 # The value corresponding to the auto-increment name. If the name does not exist, create name = amount; otherwise, auto-subtract. 2 3 # parameter: 4 # name, Redis name5 # amount, auto-reduction (integer) append (key, value) return character length 1 # Append content 2 3 after the value corresponding to redis name # parameter: 4 key, redis name5 value, string to be appended
View Code

 

The above is the basic operation of redis. We recommend that you use an example.

Redis is a no-SQL cache database. Data is cached. So if you restart redis-server, you will find that all the previously stored data has disappeared! To solve this problem, you can add the save command after saving the data, the data will be saved to the disk, and the data will not disappear after the restart.

 

Ii. Connection Pool and computing website UV instance

1. Connection Pool

Use the connection pool to manage all connections to a redis server, avoiding the overhead of establishing and releasing connections each time.

1 import redis2  3 pool = redis.ConnectionPool(host='10.211.55.4', port=6379)4 r = redis.Redis(connection_pool=pool)

 

2. Calculate website UV instances

AboutPV, UV, IPCan look at my summary below, specific can also refer to the blog: http://playkid.blog.163.com/blog/static/56287260201361951919690/

PV (page view) is the page views or clicks, which is used to measure the access volume of a website or webpage user. Specifically, the PV value is the number of pages or pages that all visitors view a website within 24 hours (. PV refers to the number of page refreshes, and every page refresh, even if PV traffic is performed. Unique visitor (UV) refers to the number of unique visitors who visit a site or click different IP addresses of a webpage. Within the same day, UV records only visitors with independent IP addresses who enter the website for the first time, and does not count when they access the website again within the same day. IP addresses can be understood as the number of users accessing the website using different IP addresses within one day. No matter how many pages are accessed by the same IP address, the number of independent IP addresses is 1.
View Code

 

The Application Scenario of setbit huge stream disadvantage. When will this function be used? For ultra-large application platforms, such as Sina Weibo, how can I view users currently logged on? Of course, you will think that after a user logs on to the database, mark the user information, and count the total number of marked users. so, the current user can view and solve the problem. OK, well, you must set tags for each user login. If the number of users currently is several hundred million, you need to store hundreds of millions of tags to overhead the database; now there is an invincible and efficient way,Use binary bits to count current online usersWhat does it mean? You can see the following code:

1 import redis 2 # establish connection 3 pool = redis. connectionPool (host = '2017. 0.0.1 ', port = 6379) 4 r = redis. redis (connection_pool = pool) 5 6 r. setbit ("uv_count1", 5, 1) # Set the byte to 1 7 r for each connection. setbit ("uv_count1", 8, 1) 8 r. setbit ("uv_count1", 3,1) 9 r. setbit ("uv_count1",) # repeated 10 print ("uv_count:", r. bitcount ("uv_count1") 11 12 output: uv_count: 3
View Code

For example, if the current 500th-bit user is online, set the 500th bits to 1 (the default value is 0 ). Bitcount counts the number of 1 in the second-level system bits. It can be used with setbit and bitcount to easily solve the current number of online users.1 byte = 8 bits, so 10 m = 80 million bits, that is, 0.1 billion of online users can handle more than 10 MB of memory.This optimization is incredible !!

 

III. Basic redis hash, list, and set operations

I will directlyBasic operations such as hash, list, and setOtherwise, it will be boring ~~

Hash operation. The storage format of Hash in redis in memory is as follows:

 

1. Basic hash operations

 

List operation. The List in redis is stored in the memory according to a name corresponding to a List. :

 

2. Basic list operations

Insert a new value before or after a value in the list

R. lpush () from the left
R. rpush () from the right

 

3. set basic operations

 

4. 16 databases and ordered collections of redis

1. 16 Databases

Redis has 16 databases by default, which are in the 0 database by default and can be switched (eg: switched to database No. 15: select 15). But in python, for security considerations, the python API does not have the concept of database switching. You can specify the called database when connecting to the call, but you cannot switch it once it is connected.

Move (name, db) # move a redis value to the specified database

 

2. Ordered Set

Ordered Set: sorts each element based on the set. The sorting of elements needs to be compared based on another value. Therefore, for an ordered set, each element has two values: values and scores, which are used for sorting.

 

Basic operations on Ordered Sets

 

 

V. redis release and subscription


Application Scenario: An advertisement is displayed in the upper right corner of the logon QQ page.
Publisher: Server
Subscriber: individual user

Publishing and subscription must be on the same channel (similarRadio). Otherwise, the subscriber will not receive the message after the publisher releases it!

Redis_helper.py file (public class)

1 import redis 2 3 4 class RedisHelper (object): 5 6 def _ init _ (self): 7 self. _ conn = redis. redis (host = '2017. 0.0.1 ') 8 self. chan_sub = 'fm88. 7' # set two channels and subscribe to Channel 9 self. chan_pub = 'fm88. 7' # Release Channel 10 11 def public (self, msg): 12 self. _ conn. publish (self. chan_pub, msg) # publish message 13 returnTrue14 15 def subscribe (self): 16 pub = self. _ conn. pubsub () # generate an instance to turn on the radio 17 pub. subscribe (self. chan_sub) # twist to the 18 m = pub. parse_response () # Prepare for listening. If it is not blocked, call it again to block 19 print (m) # [B 'subscribe', B 'fm88. 7', 1] 20 return pub # return instance

Redis_sub.py

1 from redis_helper import RedisHelper2 3 obj = RedisHelper () 4 redis_sub = obj. subscribe () # Return instance 5 6 while True: 7 msg = redis_sub.parse_response () # Listen to 8 print (msg) # print if there is a message, and block if there is no message

Redis_pub.py

from redis_helper importRedisHelperobj=RedisHelper()return1=obj.public('love')print(return1)

 

Running result (client ):Supports concurrent processing of multiple clients.

[b'subscribe', b'fm88.7', 1][b'message', b'fm88.7', b'love'][b'message', b'fm88.7', b'love']

 

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.