-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis-app.py
91 lines (74 loc) · 2.39 KB
/
redis-app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import psycopg2
from configparser import ConfigParser
from flask import Flask, render_template, g, abort
import redis
import time
def config(filename='config/database.ini', section='postgresql'):
# create a parser
parser = ConfigParser()
# read config file
parser.read(filename)
# get section, default to postgresql
db = {}
if parser.has_section(section):
params = parser.items(section)
for param in params:
db[param[0]] = param[1]
else:
raise Exception('Section {0} not found in the {1} file'.format(section, filename))
return db
def fetch(sql):
ttl = 10 # Time to live in seconds
try:
params = config(section='redis')
cache = redis.Redis.from_url(params['redis_url'])
result = cache.get(sql)
if result:
print('Got redis result')
return result
else:
# connect to database listed in database.ini
conn = connect()
cur = conn.cursor()
cur.execute(sql)
# fetch one row
result = cur.fetchone()
print('Closing connection to database...')
cur.close()
conn.close()
# cache result
cache.setex(sql, ttl, ''.join(result))
return result
except (Exception, psycopg2.DatabaseError) as error:
print(error)
def connect():
""" Connect to the PostgreSQL database server and return a cursor """
conn = None
try:
# read connection parameters
params = config()
# connect to the PostgreSQL server
print('Connecting to the PostgreSQL database...')
conn = psycopg2.connect(**params)
# return a conn
return conn
except (Exception, psycopg2.DatabaseError) as error:
print(error)
app = Flask(__name__)
@app.before_request
def before_request():
g.request_start_time = time.time()
g.request_time = lambda: "%.5fs" % (time.time() - g.request_start_time)
@app.route("/")
def index():
retval = ''
sql = 'SELECT slow_version();'
db_result = fetch(sql)
print('db_result', db_result)
if not db_result:
abort(500)
db_version = db_result
params = config()
return render_template('index.html', db_version=db_version, db_host=params['host'])
if __name__ == "__main__": # on running python app.py
app.run() # run the flask app