Data processing based on Python+mysql+redis cache design and Database Association

Source: Internet
Author: User

1. Add a table

CREATE TABLE Tb_signin (

ID INT,

User_name VARCHAR (10),

Signin_num INT,

Signin_time DATETIME,

Gold_coin INT

);

INSERT into Tb_signin

VALUES (1, ' Ma ', 0, NULL, 0),

(2, ' he ', 0, NULL, 0),

(3, ' Yu ', 0, NULL, 0),

(4, ' Hai ', 0, NULL, 0),

(5, ' Fang ', 0, NULL, 0);

2, Redis cache key value Design

Key value

Table name: Primary KEY value: Column list value

Or:

Table name: Primary KEY value: Column value 1: Column name 1

Example: Storing a number of people with ID 1 (assuming 5) in a redis can be done as follows:

Set (' Tb_signin_rank:1:signin_num ', 5)

Like a database, other values can be obtained by primary key

3. Data processing mode of Redis Association database:

, first determine if there is a cache (usually based on key), read from the cache if it exists, or read from the database and update the cache

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/89/A9/wKiom1gZXwHAkZyoAABDYTBtdJQ407.png-wh_500x0-wm_3 -wmp_4-s_1933037467.png "title=" Redis1.png "alt=" Wkiom1gzxwhakzyoaabdytbtdjq407.png-wh_50 "/>

Application scenario: The data real-time requirements are not high, the update is not very frequent


For example, write to Redis and then, using daemons, and so on, write to the database regularly

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/89/A7/wKioL1gZXxrDQ2YNAAA9dIkMOCY732.png-wh_500x0-wm_3 -wmp_4-s_1440030602.png "title=" Red2.png "alt=" Wkiol1gzxxrdq2ynaaa9dikmocy732.png-wh_50 "/>


For example, write to the database before updating to the cache

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/89/A7/wKioL1gZXyyy5XkAAAAy6gz0EKM820.png-wh_500x0-wm_3 -wmp_4-s_1287887528.png "title=" Red3.png "alt=" Wkiol1gzxyyy5xkaaaay6gz0ekm820.png-wh_50 "/>

####

import configparserimport sysimport redisimport mysqldb__name__ ==:     pool=redis. ConnectionPool (=,=,=)     r=redis. Redis (=pool)     config=configparser.configparser ()     :         dbcon=mysqldb.connect (=,=,=,=,=,=)     mysqldb.error,e:         ,e        sys.exit ()     :         db_cursor=dbcon.cursor ()           id  (,):              db_cursor.execute (, id)               db_cursor.execute ()          r.zincrby (,  id, )     e:          (% e)          db_ Cursor.execute ()          db_cursor.close ()            ()     id  (,):         result=r.zscore (, id)         result:             :                 db_cursor=dbcon.cursor ()                  db_cursor.execute (, id)                  result=db_cursor.fetchone () []                 r.zadd (, Id,result)              e:                 % e                 db_cursor.close ()         :              ()              result =  (Result)          (%  (Id, result))

###

Zadd: commands are used to add one or more member elements and their sub-values to an ordered set

Zscore: The command returns an ordered set of members in the key score. Returns nil if the member does not exist in the sorted collection, or if the key does not exist.


This article is from the "DBSpace" blog, so be sure to keep this source http://dbspace.blog.51cto.com/6873717/1868400

Data processing based on Python+mysql+redis cache design and Database Association

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.