Skip to content

Commit

Permalink
Cache get_last_updated_at() method
Browse files Browse the repository at this point in the history
Refs #238
  • Loading branch information
jonashaag committed Jul 4, 2019
1 parent 542cf7e commit c361ba5
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions klaus/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,30 @@ def name(self):

def get_last_updated_at(self):
"""Get datetime of last commit to this repository."""
refs = []
for ref_hash in self.get_refs().values():
try:
refs.append(self[ref_hash])
except KeyError:
# Whoops. The ref points at a non-existant object
pass
refs.sort(key=lambda obj:getattr(obj, 'commit_time', float('-inf')),
reverse=True)
for ref in refs:
# Find the latest ref that has a commit_time; tags do not
# have a commit time
if hasattr(ref, "commit_time"):
return ref.commit_time
return None
def _get_last_updated_at():
refs = []
for ref_hash in self.get_refs().values():
try:
refs.append(self[ref_hash])
except KeyError:
# Whoops. The ref points at a non-existant object
pass
refs.sort(key=lambda obj:getattr(obj, 'commit_time', float('-inf')),
reverse=True)
for ref in refs:
# Find the latest ref that has a commit_time; tags do not
# have a commit time
if hasattr(ref, "commit_time"):
return ref.commit_time
return None

# Cache result to speed up repo_list.html template.
# If self.refs.keys() as changed, we should invalidate the cache.

This comment has been minimized.

Copy link
@jelmer

jelmer Jul 4, 2019

Contributor

This should be self.refs rather than self.refs.keys(); if the value of the ref changes you'd also want to refresh the commit time.

This comment has been minimized.

Copy link
@jonashaag

jonashaag Jul 4, 2019

Author Owner

Thanks that's correct

cache_key = self.refs.keys()
if cache_key != getattr(self, '_last_updated_at_cache_key', None):
self._last_updated_at_cache_retval = _get_last_updated_at()
self._last_updated_at_cache_key = cache_key
return self._last_updated_at_cache_retval

@property
def cloneurl(self):
Expand Down

0 comments on commit c361ba5

Please sign in to comment.