Previously wrote an article about 12306 information query page, re-organize today, I use Flask+redis, because data a hundred thousand of is placed in MySQL, even if the index, my VPS is a bit tight.
Redis can do storage and cache, because these data is not very important, lost a big deal to import again, but we care more about the query efficiency, so redis do storage is a good choice.
First, the data in the file is imported into the Redis,raw file in the following format, the file name is Result.txt
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/5A/DE/wKiom1T-lCWyHd9tAALJTU3Ifdo032.jpg "title=" Idtxt.png "alt=" wkiom1t-lcwyhd9taaljtu3ifdo032.jpg "/> The code to import Redis is as follows:
# -*- coding: utf-8 -*-import redisimport timestart = time.clock () Redisclient = redis. Strictredis (host= ' localhost ', port=6379,db=0,password= ' [email protected] ') f = open (' Result.txt ', ' R ') While true: line = f.readline () if not line: break lines = Line.replace ("\ n", ""). Replace ("\ r \ n", ""). Split ("----") redisclient.hset (Lines[0], "id", Lines[0]) redisclient.hset (lines[0], "name", Lines[1]) Redisclient.hset (lines[0], "password", lines[2]) redisclient.hset (lines[0], "email", lines[ 3]) redisclient.hset (lines[0], "phone", Lines[4]) f.close () End = time.clock () print "used:", end -start
13w data less than 1.5 ...
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/5A/D9/wKioL1T-ldLz6pslAAA2ygAPZBU249.jpg "title=" Time.png "alt=" Wkiol1t-ldlz6pslaaa2ygapzbu249.jpg "/>
Here my key is ID is * * * good, the other is a hash of the data structure stored in the form. One of the advantages of Redis is the ability to support rich data structures. Sign in to Redis, execute keys * Look at
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/5A/D9/wKioL1T-maejadpaAAI6Mo5TBYQ192.jpg "title=" Idext.png "alt=" Wkiol1t-maejadpaaai6mo5tbyq192.jpg "/>
OK, the data has, then started on the flask, the code is as follows:
# -*- coding: utf-8 -*-from flask import flask, request, render_ Template, jsonify, jsonimport redisapp = flask (__name__) @app. Route ('/api/<id > ', methods=[' GET ']) Def scan (ID): redis_client = redis. Strictredis (host= ' 127.0.0.1 ', port=6379,db=0,password= ' [email protected] ') Get_key = redis_client.keys (ID) if not get_key : json_result = {' id ' : None} return json.dumps (Json_ Result,ensure_ascii=false) else: get_key = redis_client.hmget (ID, ' name ', ' password ', ' email ', ' phone ') json_result = {' ID ' : str (ID), ' name ' : get_key[0], ' Pass ' : get_key[1], ' Email ': get_key[2], ' phone ': get_key[3] &Nbsp; } return json.dumps ( Json_result,ensure_ascii=false) @app. Route ('/') Def index (): return render_ Template (' index.html ') if __name__ == ' __main__ ': app.run (host= ' 0.0.0.0 ', port = 8080, debug=true)
We want to query which user will pass parameters to the Flask encapsulated API, this is to interface to the parameter.
What is an interface? See the instructions below to understand.
Enter the API address in the browser, just pass a non-existent parameter, that is, the * * number.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/5A/DE/wKiom1T-mguCQjmgAABMA5WP51M516.jpg "title=" Api.png "alt=" Wkiom1t-mgucqjmgaabma5wp51m516.jpg "/>
Interface return value is NULL
If the argument is present, there will be a correct return value.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/5A/D9/wKioL1T-m62TPgd0AACHmQSPC0o936.jpg "title=" Id2.png "alt=" Wkiol1t-m62tpgd0aachmqspc0o936.jpg "/>
Finally, the front page shows that I'm using Ajax plugins for Bootstrap and jquery. The code is as follows:
<! Doctype html>OK, start webserver and look at the page query situation:
Enter a * * * number, if present, the page will return
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/5A/DA/wKioL1T-ne-zfR2TAAE9s85kjds349.jpg "title=" Id321.png "alt=" Wkiol1t-ne-zfr2taae9s85kjds349.jpg "/>
If it does not exist, JS will alert a bullet box.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/5A/DA/wKioL1T-nqzw3v8JAAFOWfEgv_4047.jpg "title=" Id312.png "alt=" Wkiol1t-nqzw3v8jaafowfegv_4047.jpg "/>
is not also relatively simple, my friend Hadron want to know how to build, so share ...
This article from "Lao Xu's Private Food" blog, declined to reprint!
Flask+redis Building a Query page