mirror of
https://codeberg.org/rimu/pyfedi
synced 2025-02-02 16:21:32 -08:00
caching on home page and posts, especially for anonymous users
the home page and post pages are the most likely to get slashdotted by mastodon.
This commit is contained in:
parent
a767a96dcd
commit
d9d69c0b8a
3 changed files with 29 additions and 5 deletions
|
@ -26,7 +26,7 @@ from app.activitypub.util import public_key, users_total, active_half_year, acti
|
|||
from app.utils import gibberish, get_setting, is_image_url, allowlist_html, render_template, \
|
||||
domain_from_url, markdown_to_html, community_membership, ap_datetime, ip_address, can_downvote, \
|
||||
can_upvote, can_create_post, awaken_dormant_instance, shorten_string, can_create_post_reply, sha256_digest, \
|
||||
community_moderators, lemmy_markdown_to_html
|
||||
community_moderators, lemmy_markdown_to_html, make_cache_key
|
||||
from sqlalchemy import desc
|
||||
import werkzeug.exceptions
|
||||
|
||||
|
@ -1509,6 +1509,7 @@ def post_ap2(post_id):
|
|||
|
||||
|
||||
@bp.route('/post/<int:post_id>', methods=['GET', 'POST'])
|
||||
@cache.cached(timeout=5, make_cache_key=make_cache_key)
|
||||
def post_ap(post_id):
|
||||
if request.method == 'GET' and is_activitypub_request():
|
||||
post = Post.query.get_or_404(post_id)
|
||||
|
|
|
@ -7,6 +7,7 @@ from time import sleep
|
|||
import flask
|
||||
import markdown2
|
||||
import requests
|
||||
from flask_caching import CachedResponse
|
||||
from sqlalchemy.sql.operators import or_, and_
|
||||
|
||||
from app import db, cache
|
||||
|
@ -28,7 +29,8 @@ from app.utils import render_template, get_setting, gibberish, request_etag_matc
|
|||
ap_datetime, ip_address, retrieve_block_list, shorten_string, markdown_to_text, user_filters_home, \
|
||||
joined_communities, moderating_communities, parse_page, theme_list, get_request, markdown_to_html, allowlist_html, \
|
||||
blocked_instances, communities_banned_from, topic_tree, recently_upvoted_posts, recently_downvoted_posts, \
|
||||
generate_image_from_video_url, blocked_users, microblog_content_to_title, menu_topics, languages_for_form
|
||||
generate_image_from_video_url, blocked_users, microblog_content_to_title, menu_topics, languages_for_form, \
|
||||
make_cache_key
|
||||
from app.models import Community, CommunityMember, Post, Site, User, utcnow, Domain, Topic, File, Instance, \
|
||||
InstanceRole, Notification, Language, community_language, PostReply
|
||||
|
||||
|
@ -36,23 +38,36 @@ from app.models import Community, CommunityMember, Post, Site, User, utcnow, Dom
|
|||
@bp.route('/', methods=['HEAD', 'GET', 'POST'])
|
||||
@bp.route('/home', methods=['GET', 'POST'])
|
||||
@bp.route('/home/<sort>', methods=['GET', 'POST'])
|
||||
@cache.cached(make_cache_key=make_cache_key)
|
||||
def index(sort=None):
|
||||
if 'application/ld+json' in request.headers.get('Accept', '') or 'application/activity+json' in request.headers.get(
|
||||
'Accept', ''):
|
||||
return activitypub_application()
|
||||
|
||||
return home_page('home', sort)
|
||||
return CachedResponse(
|
||||
response=home_page('home', sort),
|
||||
timeout=50 if current_user.is_anonymous else 5,
|
||||
)
|
||||
|
||||
|
||||
@bp.route('/popular', methods=['GET'])
|
||||
@bp.route('/popular/<sort>', methods=['GET'])
|
||||
@cache.cached(timeout=5, make_cache_key=make_cache_key)
|
||||
def popular(sort=None):
|
||||
return home_page('popular', sort)
|
||||
return CachedResponse(
|
||||
response=home_page('popular', sort),
|
||||
timeout=50 if current_user.is_anonymous else 5,
|
||||
)
|
||||
|
||||
|
||||
@bp.route('/all', methods=['GET'])
|
||||
@bp.route('/all/<sort>', methods=['GET'])
|
||||
@cache.cached(timeout=5, make_cache_key=make_cache_key)
|
||||
def all_posts(sort=None):
|
||||
return home_page('all', sort)
|
||||
return CachedResponse(
|
||||
response=home_page('all', sort),
|
||||
timeout=50 if current_user.is_anonymous else 5,
|
||||
)
|
||||
|
||||
|
||||
def home_page(type, sort):
|
||||
|
|
|
@ -166,6 +166,14 @@ def gibberish(length: int = 10) -> str:
|
|||
return "".join([random.choice(random_chars) for x in range(length)])
|
||||
|
||||
|
||||
# used by @cache.cached() for home page and post caching
|
||||
def make_cache_key(sort=None, post_id=None):
|
||||
if current_user.is_anonymous:
|
||||
return f'{request.url}_{sort}_{post_id}_anon_{request.headers.get("Accept")}_{request.headers.get("Accept-Language")}' # The Accept header differentiates between activitypub requests and everything else
|
||||
else:
|
||||
return f'{request.url}_{sort}_{post_id}_user_{current_user.id}'
|
||||
|
||||
|
||||
def is_image_url(url):
|
||||
common_image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp']
|
||||
mime_type = mime_type_using_head(url)
|
||||
|
|
Loading…
Add table
Reference in a new issue