Skip to content

Commit

Permalink
Improve h-card caching behavior
Browse files Browse the repository at this point in the history
Removed fields will no longer be cached, and the cache lifetime is now only 5 minutes.
  • Loading branch information
fluffy-critter committed Aug 29, 2021
1 parent f68e5ce commit bd23914
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
7 changes: 4 additions & 3 deletions authl/handlers/indieauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@

# We do this instead of functools.lru_cache so that IndieAuth.handles_page
# and find_endpoint can both benefit from the same endpoint cache
_ENDPOINT_CACHE = expiringdict.ExpiringDict(max_len=128, max_age_seconds=1800)
_ENDPOINT_CACHE = expiringdict.ExpiringDict(max_len=128, max_age_seconds=300)

# And similar for retrieving user profiles
_PROFILE_CACHE = expiringdict.ExpiringDict(max_len=128, max_age_seconds=1800)
# And similar for retrieving user h-cards
_PROFILE_CACHE = expiringdict.ExpiringDict(max_len=128, max_age_seconds=300)


def find_endpoint(id_url: str,
Expand Down Expand Up @@ -189,6 +189,7 @@ def get_profile(id_url: str,
content = BeautifulSoup(request.text, 'html.parser')

if content:
profile = {}
h_cards = mf2py.Parser(doc=content).to_dict(filter_by_type="h-card")
LOGGER.debug("get_profile(%s): found %d h-cards", id_url, len(h_cards))

Expand Down
31 changes: 31 additions & 0 deletions tests/handlers/test_indieauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,3 +506,34 @@ def test_server_profile(requests_mock):
assert profile == hcard_blob

assert profile_mock.call_count == 1


def test_hcard_cache_invalidation(requests_mock):
old_profile = r"""<link rel="authorization_endpoint" href="https://cached.example/">
<div class="h-card"><a class="u-url p-name" href="https://foo.bar/old">old user</a>
<a class="p-note">a note</a></div>
"""

new_profile = r"""<link rel="authorization_endpoint" href="https://cached.example/">
<div class="h-card"><a class="u-url p-name" href="https://foo.bar/new">new user</a></div>
"""

profile_mock = requests_mock.get('http://cached.example', text=old_profile)

profile = indieauth.get_profile('http://cached.example')
assert profile['homepage'] == 'https://foo.bar/old'
assert profile['name'] == 'old user'
assert profile['bio'] == 'a note'

profile = indieauth.get_profile('http://cached.example',
content=BeautifulSoup(new_profile, 'html.parser'))
assert profile['homepage'] == 'https://foo.bar/new'
assert profile['name'] == 'new user'
assert 'bio' not in profile

profile = indieauth.get_profile('http://cached.example')
assert profile['homepage'] == 'https://foo.bar/new'
assert profile['name'] == 'new user'
assert 'bio' not in profile

assert profile_mock.call_count == 1

0 comments on commit bd23914

Please sign in to comment.