Skip to content

Commit a342466

Browse files
committed
cache /convert/ and /r/ endpoints in memcache
...since GAE's edge caching based on Cache-Control doesn't seem to be very effective :/ for #1149
1 parent 04e85db commit a342466

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

convert.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
URL pattern is ``/convert/SOURCE/DEST``, where ``SOURCE`` and ``DEST`` are the
44
``LABEL`` constants from the :class:`protocol.Protocol` subclasses.
55
"""
6+
from datetime import timedelta
67
import logging
78
import re
89
from urllib.parse import quote, unquote
@@ -14,12 +15,13 @@
1415

1516
from activitypub import ActivityPub
1617
from common import (
17-
CACHE_CONTROL_VARY_ACCEPT,
18+
CACHE_CONTROL,
1819
LOCAL_DOMAINS,
1920
subdomain_wrap,
2021
SUPERDOMAIN,
2122
)
2223
from flask_app import app
24+
import memcache
2325
from models import Object, PROTOCOLS
2426
from protocol import Protocol
2527
from web import Web
@@ -28,12 +30,16 @@
2830

2931

3032
@app.get(f'/convert/<to>/<path:_>')
31-
@flask_util.headers(CACHE_CONTROL_VARY_ACCEPT)
33+
@memcache.memoize(expire=timedelta(hours=1))
34+
@flask_util.headers(CACHE_CONTROL)
3235
def convert(to, _, from_=None):
3336
"""Converts data from one protocol to another and serves it.
3437
3538
Fetches the source data if it's not already stored.
3639
40+
Note that we don't do conneg or otherwise care about the Accept header here,
41+
we always serve the "to" protocol's format.
42+
3743
Args:
3844
to (str): protocol
3945
from_ (str): protocol, only used when called by

redirect.py

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
The conneg makes these ``/r/`` URLs searchable in Mastodon:
1515
https://github.com/snarfed/bridgy-fed/issues/352
1616
"""
17+
from datetime import timedelta
1718
import logging
1819
import re
1920
import urllib.parse
@@ -27,6 +28,7 @@
2728
from activitypub import ActivityPub
2829
from common import CACHE_CONTROL_VARY_ACCEPT, CONTENT_TYPE_HTML, as2_request_type
2930
from flask_app import app
31+
import memcache
3032
from protocol import Protocol
3133
from web import Web
3234

@@ -38,6 +40,7 @@
3840

3941

4042
@app.get(r'/r/<path:to>')
43+
@memcache.memoize(expire=timedelta(hours=1), key=lambda to: (to, as2_request_type()))
4144
@flask_util.headers(CACHE_CONTROL_VARY_ACCEPT)
4245
def redir(to):
4346
"""Either redirect to a given URL or convert it to another format.

0 commit comments

Comments
 (0)