implement cache

main
lub 2 months ago
parent 3c41d4d750
commit 16c777218c

@ -1,5 +1,6 @@
import requests import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from time import time
from http.server import BaseHTTPRequestHandler, HTTPServer from http.server import BaseHTTPRequestHandler, HTTPServer
def scrape(): def scrape():
@ -53,15 +54,25 @@ def scrape():
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self): def do_GET(self):
current_time = time()
if cache['time'] < current_time - 30:
html = templateHtml.replace('--body--', scrape())
cache['output'] = html.encode('utf-8')
cache['time'] = current_time
self.send_response(200) self.send_response(200)
self.end_headers() self.end_headers()
html = templateHtml.replace('--body--', scrape()) self.wfile.write(cache['output'])
self.wfile.write(html.encode('utf-8'))
with open('template.html', 'r') as templateFile: with open('template.html', 'r') as templateFile:
templateHtml = templateFile.read() templateHtml = templateFile.read()
cache = {
"time": 0,
"output": None
}
httpd = HTTPServer(('', 8000), SimpleHTTPRequestHandler) httpd = HTTPServer(('', 8000), SimpleHTTPRequestHandler)
httpd.serve_forever() httpd.serve_forever()

Loading…
Cancel
Save