Skip to content

Commit

Permalink
Enable cache-control for requests that aren't user-dependent
Browse files Browse the repository at this point in the history
  • Loading branch information
fluffy-critter committed May 23, 2023
1 parent b0f3791 commit 4cc51fa
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
2 changes: 2 additions & 0 deletions publ/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,8 @@ def last_modified(self) -> arrow.Arrow:
@property
def authorized(self) -> bool:
""" Returns if the current user is authorized to see this entry """
if self._record.auth:
flask.g.user_dependent = True
return self._record.is_authorized(user.get_active())

def _get_markup(self, text, is_markdown, args,
Expand Down
19 changes: 17 additions & 2 deletions publ/rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@
}


def cache_control():
""" Determine the cache-control value based on page rendering """
if flask.g.get('user_dependent'):
# page had a user dependency, so don't cache it
return 'private, no-cache'

# page did not have a user dependency, so it's cacheable
timeout = config.cache.get('CACHE_DEFAULT_TIMEOUT', None)
if timeout:
return f'public, max-age={timeout}'
else:
return 'public'

def mime_type(template: Template) -> str:
""" infer the content-type from the extension """
_, ext = os.path.splitext(template.filename)
Expand Down Expand Up @@ -328,7 +341,8 @@ def render_category_path(category: str, template: typing.Optional[str]):
return 'Not modified', 304, {'ETag': f'"{etag}"'}

return rendered, {'Content-Type': mime_type(tmpl),
'ETag': f'"{etag}"'}
'ETag': f'"{etag}"',
'Cache-Control': cache_control()}


@ orm.db_session
Expand Down Expand Up @@ -518,7 +532,8 @@ def render_entry_record(record: model.Entry, category: str, template: typing.Opt

headers = {
'Content-Type': entry_obj.get('Content-Type', mime_type(tmpl)),
'ETag': etag
'ETag': etag,
'Cache-Control': cache_control()
}

return rendered, headers
Expand Down
8 changes: 8 additions & 0 deletions publ/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,13 @@ def __lt__(self, other):
@cached_property
def identity(self):
""" The federated identity name of the user """
flask.g.user_dependent = True
return self._identity

@cached_property
def humanize(self) -> str:
""" A humanized version of the identity string """
flask.g.user_dependent = True
url = self.profile.get('profile_url', self._identity)
parsed = urllib.parse.urlparse(url)
return ''.join(p for p in (
Expand All @@ -103,6 +105,7 @@ def humanize(self) -> str:
@cached_property
def name(self) -> str:
""" The readable name of the user """
flask.g.user_dependent = True
if 'name' in self.profile:
return self.profile['name']

Expand All @@ -111,21 +114,25 @@ def name(self) -> str:
@property
def profile(self) -> dict:
""" Get the user's profile """
flask.g.user_dependent = True
return self._info[0]

@cached_property
def groups(self) -> typing.Set[str]:
""" The group memberships of the user, for display purposes """
flask.g.user_dependent = True
return get_groups(self._identity, False)

@cached_property
def auth_groups(self) -> typing.Set[str]:
""" The group memberships of the user, for auth purposes """
flask.g.user_dependent = True
return get_groups(self._identity, True)

@cached_property
def is_admin(self) -> bool:
""" Returns whether this user has administrator permissions """
flask.g.user_dependent = True
return bool(config.admin_group and config.admin_group in self.auth_groups)

@cached_property
Expand Down Expand Up @@ -168,6 +175,7 @@ def last_seen(self) -> typing.Optional[arrow.Arrow]:

def token(self, lifetime: int, scope: Optional[str] = None) -> str:
""" Get a bearer token for this user """
flask.g.user_dependent = True
return tokens.get_token(self.identity, lifetime, scope)


Expand Down
5 changes: 3 additions & 2 deletions test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
'CACHE_THRESHOLD': 20
} if os.environ.get('TEST_CACHING') else {
'CACHE_TYPE': 'NullCache',
'CACHE_NO_NULL_WARNING': True
'CACHE_NO_NULL_WARNING': True,
'CACHE_DEFAULT_TIMEOUT': 720
},
'auth': {
'TEST_ENABLED': True,
Expand Down Expand Up @@ -71,7 +72,7 @@ def favicon(ext):
""" render a favicon """
logo = publ.image.get_image('images/rawr.jpg', 'tests/content')
img, _ = logo.get_rendition(format=ext, width=128, height=128, resize='fill')
return flask.redirect(img)
return flask.redirect(img), {'Cache-Control': 'public, max-age=86400'}


@app.path_alias_regex(r'(.*)/date/([0-9]+)')
Expand Down

0 comments on commit 4cc51fa

Please sign in to comment.