Using Python to manipulate Redis

Source: Internet
Author: User
Tags redis server

1. Installing Pyredis

(1) using # Easy_install Redis

(2) Direct compilation and installation

#wget HTTPS://pypi. Python. org/packages/source/R /Redis/redis-2.9.1.tar.gz

#tar xvzf redis-2.9.1.tar.gz

#CD redis-2.9.1

#python setup. py Install

2. Simple Redis operation

Redis connection instances are thread-safe and can be directly used by setting up a Redis connection instance as a global variable. If you need another Redis instance (or Redis database), you will need to recreate the Redis connection instance to get a new connection. Similarly, Python's redis does not implement the Select command.

  

3. Application Scenario-page hits

Redis Cookbook A detailed description of the classic scene. 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)

  

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

  

This value can be obtained directly when the page is loaded

  

4. Use hash type to save diverse objects

When there are many types of document objects , the contents of the document are different, (that is, "table" does not have a fixed column), you can use hash to express.

  

5. 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.

  

Get a member of a circle

  

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

  

6. Application scenario-real-time user statistics

When we need to display the current online user on the page, we can use Redis to do it. First get the current time (in Unix timestamps) divided by 60, you can create a key based on this value. Then add the user to this collection. When the maximum timeout you set is exceeded, the set is set to expire, and when you need to query the current online user, the last n-minute set is intersected together. Because Redis connection objects are thread-safe, they can be represented directly using a global variable. (Counting Online Users with Redis)

  

Using Python to manipulate 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.