Skip to content
This repository was archived by the owner on Oct 21, 2022. It is now read-only.

Commit 589db4f

Browse files
authored
Check if the github cache is valid (#204)
1 parent a0dc77d commit 589db4f

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

badge_server/main.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,14 @@ def run_check():
318318
else:
319319
details = self_comp_res
320320

321-
# Run the check if details is None or forced to populate the cache.
321+
# Run the check if details is None or forced to populate the cache or
322+
# the cache is outdated.
322323
if self_comp_res is None or force_run_check is not None:
323324
threading.Thread(target=run_check).start()
325+
elif self_comp_res is not None:
326+
timestamp = self_comp_res.get('timestamp')
327+
if not badge_utils._is_github_cache_valid(timestamp):
328+
threading.Thread(target=run_check).start()
324329

325330
badge = badge_utils._get_badge(details, badge_name)
326331
response = flask.make_response(badge)
@@ -418,9 +423,14 @@ def run_check():
418423
else:
419424
details = dependency_res
420425

421-
# Run the check if dependency_res is None or forced to populate the cache.
426+
# Run the check if dependency_res is None or forced to populate the cache
427+
# or the cache is outdated.
422428
if dependency_res is None or force_run_check is not None:
423429
threading.Thread(target=run_check).start()
430+
elif dependency_res is not None:
431+
timestamp = dependency_res.get('timestamp')
432+
if not badge_utils._is_github_cache_valid(timestamp):
433+
threading.Thread(target=run_check).start()
424434

425435
badge = badge_utils._get_badge(details, badge_name)
426436
response = flask.make_response(badge)
@@ -533,9 +543,14 @@ def run_check():
533543
else:
534544
details = google_comp_res
535545

536-
# Run the check if google_comp_res is None or forced to populate the cache.
546+
# Run the check if google_comp_res is None or forced to populate the cache
547+
# or the cache is outdated.
537548
if google_comp_res is None or force_run_check is not None:
538549
threading.Thread(target=run_check).start()
550+
elif google_comp_res is not None:
551+
timestamp = google_comp_res.get('timestamp')
552+
if not badge_utils._is_github_cache_valid(timestamp):
553+
threading.Thread(target=run_check).start()
539554

540555
badge = badge_utils._get_badge(details, badge_name)
541556
response = flask.make_response(badge)

badge_server/templates/one-badge.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<div class="col-md-12">
3131
<h1 class="title-4">Package name: {{ package_name }}
3232
</h1>
33-
{% if commit_number | length > 0 %}
33+
{% if commit_number %}
3434
<h1 class="title-4">Commit number: {{ commit_number }}
3535
</h1>
3636
{% endif %}

badge_server/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
"""Common utils methods for badge server."""
1616

17+
import datetime
1718
import enum
1819
import json
1920
import logging
@@ -45,6 +46,7 @@
4546
URL_PREFIX = 'https://img.shields.io/badge/'
4647
GITHUB_HEAD_NAME = 'github head'
4748
GITHUB_API = 'https://api.github.com/repos'
49+
GITHUB_CACHE_TIME = 1800 # seconds
4850
SVG_CONTENT_TYPE = 'image/svg+xml'
4951
EMPTY_DETAILS = 'NO DETAILS'
5052
PACKAGE_NOT_SUPPORTED = "The package is not supported by checker server."
@@ -170,3 +172,16 @@ def _calculate_commit_number(package: str) -> Optional[str]:
170172
return None
171173

172174
return None
175+
176+
177+
def _is_github_cache_valid(cache_timestamp_str):
178+
"""Return True if the cached result if calculated within last 30 mins."""
179+
cache_timestamp = datetime.datetime.strptime(
180+
cache_timestamp_str, TIMESTAMP_FORMAT)
181+
current_timestamp = datetime.datetime.now()
182+
seconds_diff = (current_timestamp - cache_timestamp).seconds
183+
184+
if seconds_diff > GITHUB_CACHE_TIME:
185+
return False
186+
else:
187+
return True

0 commit comments

Comments
 (0)