Skip to content

Commit 9bda36d

Browse files
committed
Fix: invalidate for Categories and Files too, and never cache for Folder
1 parent 0f5e86d commit 9bda36d

2 files changed

Lines changed: 26 additions & 6 deletions

File tree

truewiki/metadata.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ def category_callback(wtp, wiki_page, page):
6868
PAGES[page]["categories"].append(target)
6969
CATEGORIES[target].append(page)
7070

71+
# Reset the last time rendered for the category.
72+
if f"Category/{target}" in LAST_TIME_RENDERED:
73+
del LAST_TIME_RENDERED[f"Category/{target}"]
74+
7175

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

84+
# Reset the last time rendered for the file.
85+
if f"File/{target}" in LAST_TIME_RENDERED:
86+
del LAST_TIME_RENDERED[f"File/{target}"]
87+
8088

8189
def links_callback(wtp, wiki_page, page):
8290
for wikilink in wtp.wikilinks:
@@ -117,8 +125,14 @@ def template_callback(wtp, wiki_page, page):
117125
def _forget_page(page):
118126
for category in PAGES[page]["categories"]:
119127
CATEGORIES[category].remove(page)
128+
129+
if f"Category/{category}" in LAST_TIME_RENDERED:
130+
del LAST_TIME_RENDERED[f"Category/{category}"]
120131
for file in PAGES[page]["files"]:
121132
FILES[file].remove(page)
133+
134+
if f"File/{file}" in LAST_TIME_RENDERED:
135+
del LAST_TIME_RENDERED[f"File/{file}"]
122136
for link in PAGES[page]["links"]:
123137
LINKS[link].remove(page)
124138
for template in PAGES[page]["templates"]:
@@ -128,8 +142,11 @@ def _forget_page(page):
128142

129143
# Reset the last time rendered for all translations too, as
130144
# otherwise a removed translation will still show up on those pages.
131-
if f"Page/{translation}" in LAST_TIME_RENDERED:
132-
del LAST_TIME_RENDERED[f"Page/{translation}"]
145+
if not translation.startswith(("Category/", "File/", "Template/")):
146+
translation = f"Page/{translation}"
147+
148+
if translation in LAST_TIME_RENDERED:
149+
del LAST_TIME_RENDERED[translation]
133150

134151
PAGES[page]["categories"].clear()
135152
PAGES[page]["files"].clear()

truewiki/views/page.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ def view(user, page: str, if_modified_since) -> web.Response:
5252

5353
status_code = 200 if wiki_page.page_exists(page) else 404
5454
namespaced_page = page
55-
if not namespaced_page.startswith(("Category/", "File/", "Template/")):
55+
if not namespaced_page.startswith(("Category/", "File/", "Folder/", "Template/")):
5656
namespaced_page = f"Page/{namespaced_page}"
5757

58+
can_cache = status_code == 200 and not namespaced_page.startswith("Folder/")
59+
5860
if CACHE_PAGE_FOLDER:
5961
cache_filename = f"{CACHE_PAGE_FOLDER}/{namespaced_page}.html"
6062
else:
@@ -63,7 +65,7 @@ def view(user, page: str, if_modified_since) -> web.Response:
6365
response = None
6466

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

84-
if status_code == 200:
86+
# Never cache anything in the Folder/.
87+
if can_cache:
8588
metadata.LAST_TIME_RENDERED[namespaced_page] = time.time()
8689

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

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

0 commit comments

Comments
 (0)