Skip to content

Commit

Permalink
Fix: invalidate for Categories and Files too, and never cache for Folder
Browse files Browse the repository at this point in the history
  • Loading branch information
TrueBrain committed Nov 14, 2020
1 parent 0f5e86d commit 9bda36d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
21 changes: 19 additions & 2 deletions truewiki/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ def category_callback(wtp, wiki_page, page):
PAGES[page]["categories"].append(target)
CATEGORIES[target].append(page)

# Reset the last time rendered for the category.
if f"Category/{target}" in LAST_TIME_RENDERED:
del LAST_TIME_RENDERED[f"Category/{target}"]


def file_callback(wtp, wiki_page, page):
for wikilink in wtp.wikilinks:
Expand All @@ -77,6 +81,10 @@ def file_callback(wtp, wiki_page, page):
PAGES[page]["files"].append(target)
FILES[target].append(page)

# Reset the last time rendered for the file.
if f"File/{target}" in LAST_TIME_RENDERED:
del LAST_TIME_RENDERED[f"File/{target}"]


def links_callback(wtp, wiki_page, page):
for wikilink in wtp.wikilinks:
Expand Down Expand Up @@ -117,8 +125,14 @@ def template_callback(wtp, wiki_page, page):
def _forget_page(page):
for category in PAGES[page]["categories"]:
CATEGORIES[category].remove(page)

if f"Category/{category}" in LAST_TIME_RENDERED:
del LAST_TIME_RENDERED[f"Category/{category}"]
for file in PAGES[page]["files"]:
FILES[file].remove(page)

if f"File/{file}" in LAST_TIME_RENDERED:
del LAST_TIME_RENDERED[f"File/{file}"]
for link in PAGES[page]["links"]:
LINKS[link].remove(page)
for template in PAGES[page]["templates"]:
Expand All @@ -128,8 +142,11 @@ def _forget_page(page):

# Reset the last time rendered for all translations too, as
# otherwise a removed translation will still show up on those pages.
if f"Page/{translation}" in LAST_TIME_RENDERED:
del LAST_TIME_RENDERED[f"Page/{translation}"]
if not translation.startswith(("Category/", "File/", "Template/")):
translation = f"Page/{translation}"

if translation in LAST_TIME_RENDERED:
del LAST_TIME_RENDERED[translation]

PAGES[page]["categories"].clear()
PAGES[page]["files"].clear()
Expand Down
11 changes: 7 additions & 4 deletions truewiki/views/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ def view(user, page: str, if_modified_since) -> web.Response:

status_code = 200 if wiki_page.page_exists(page) else 404
namespaced_page = page
if not namespaced_page.startswith(("Category/", "File/", "Template/")):
if not namespaced_page.startswith(("Category/", "File/", "Folder/", "Template/")):
namespaced_page = f"Page/{namespaced_page}"

can_cache = status_code == 200 and not namespaced_page.startswith("Folder/")

if CACHE_PAGE_FOLDER:
cache_filename = f"{CACHE_PAGE_FOLDER}/{namespaced_page}.html"
else:
Expand All @@ -63,7 +65,7 @@ def view(user, page: str, if_modified_since) -> web.Response:
response = None

# Check as we might have this page already on cache.
if status_code == 200 and namespaced_page in metadata.LAST_TIME_RENDERED:
if can_cache and namespaced_page in metadata.LAST_TIME_RENDERED:
if (
if_modified_since is not None
and metadata.LAST_TIME_RENDERED[namespaced_page] <= if_modified_since.timestamp()
Expand All @@ -81,7 +83,8 @@ def view(user, page: str, if_modified_since) -> web.Response:
if response is None:
body = _view(wiki_page, user, page)

if status_code == 200:
# Never cache anything in the Folder/.
if can_cache:
metadata.LAST_TIME_RENDERED[namespaced_page] = time.time()

if not user and cache_filename:
Expand All @@ -93,7 +96,7 @@ def view(user, page: str, if_modified_since) -> web.Response:
response = web.Response(body=body, content_type="text/html", status=status_code)

# Inform the browser under which rules it can cache this page.
if status_code == 200:
if can_cache:
response.last_modified = metadata.LAST_TIME_RENDERED[namespaced_page]
response.headers["Vary"] = "Accept-Encoding, Cookie"
response.headers["Cache-Control"] = "private, must-revalidate, max-age=0"
Expand Down

0 comments on commit 9bda36d

Please sign in to comment.