Skip to content

Commit

Permalink
Create separate cache entries for pd requests
Browse files Browse the repository at this point in the history
  • Loading branch information
jimchamp committed Apr 25, 2024
1 parent c354ec2 commit 2e0416c
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions openlibrary/plugins/openlibrary/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,29 @@ def get_cached_page(path: str) -> dict:

return _page

def create_base_cache_key(_modes: list[str]) -> str:
"""
Returns a cache key for a cacheable path.
Function constructs a key based on the path and langauge
code, like the following:
/collections.es
Any entries in the given _modes list will be prepended
to the key. The entries will be alphabetized, pipe
delimited, and inside of square brackets. For example:
[pd|sfw]/collections.es
...is the key for the Spanish language, print-disabled, safe
for work `/collections` page.
"""
key = f'{web.ctx.path}.{web.ctx.lang}'
if _modes:
_modes.sort()
key = f'[{"|".join(_modes)}]{key}'

return key

if web.ctx.method != 'GET':
# Don't cache non-GET requests
return handler()
Expand Down Expand Up @@ -179,9 +202,17 @@ def get_cached_page(path: str) -> dict:
# Prevent redundant cache entries when `m=view` is passed
del i['m']

# Construct cache key:
cache_key = f'{web.ctx.path}.{web.ctx.lang}'

modes = []
cookies = web.cookies()
if cookies.get('pd', ''):
modes.append('pd')

# Uncomment the following to enable safe-for-work page caching:

# if cookies.get('sfw', ''):
# modes.append('sfw')

cache_key = create_base_cache_key(modes)
if len(i):
# Construct query string in alphabetical order
query_str = '&'.join([f'{k}={v}' for k, v in sorted(i.items())])
Expand Down

0 comments on commit 2e0416c

Please sign in to comment.