From eb73d39ba21a109dc25625bb77d285d9c6b44ae3 Mon Sep 17 00:00:00 2001 From: Tdxdxoz Date: Sat, 6 Nov 2021 00:33:01 +0800 Subject: [PATCH] cache home view --- app.py | 4 ++++ requirements.txt | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index ba145a3..428630b 100644 --- a/app.py +++ b/app.py @@ -6,6 +6,7 @@ from flask import (Flask, request, render_template, send_from_directory, abort, from flask_sqlalchemy import SQLAlchemy from flask_limiter import Limiter from flask_limiter.util import get_remote_address +from flask_caching import Cache from mastodon import Mastodon import random from config import C @@ -29,6 +30,8 @@ app = Flask(__name__) app.config.from_object('config.C') app.secret_key = C.session_key +cache = Cache(config={'CACHE_TYPE': 'SimpleCache'}) +cache.init_app(app) MAST_LOGIN_URL = Mastodon(api_base_url=C.mast_base_uri).auth_request_url( client_id = C.mast_client_id, @@ -203,6 +206,7 @@ def logout(): @bp.route('/') +@cache.cached(timeout=10) def home(): stories = Story.query.order_by(Story.total_like_num.desc()).all() return render_template('home.html', **locals()) diff --git a/requirements.txt b/requirements.txt index b0a714a..bf3fc19 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ Flask~=2.0.2 Flask_Limiter~=1.4 Flask_SQLAlchemy~=2.5.1 -mastodon~=0.0.1 Mastodon.py~=1.5.1 +Flask-Caching