HowardZhangdqs commited on
Commit
3158d32
·
1 Parent(s): e64fb41

fix: add cache expiry for API requests in fetch_paper.py

Browse files
Files changed (1) hide show
  1. fetch_paper.py +6 -3
fetch_paper.py CHANGED
@@ -12,14 +12,16 @@ from concurrent.futures import ThreadPoolExecutor, as_completed
12
  API_URL = "https://huggingface.co/api/daily_papers"
13
 
14
  cache = {}
 
15
 
16
 
17
  def make_request(url: str):
18
  # Create a hash of the URL to use as the cache key
19
  url_hash = hashlib.md5(url.encode()).hexdigest()
 
20
 
21
- # Check if the response is already cached
22
- if url_hash in cache:
23
  print(f"Cache hit for URL: {url}")
24
  return cache[url_hash]
25
 
@@ -37,8 +39,9 @@ def make_request(url: str):
37
  response.raise_for_status()
38
  data = response.json()
39
 
40
- # Cache the response
41
  cache[url_hash] = data
 
42
 
43
  return data
44
  except requests.RequestException as e:
 
12
  API_URL = "https://huggingface.co/api/daily_papers"
13
 
14
  cache = {}
15
+ cache_expiry = {}
16
 
17
 
18
  def make_request(url: str):
19
  # Create a hash of the URL to use as the cache key
20
  url_hash = hashlib.md5(url.encode()).hexdigest()
21
+ current_time = datetime.datetime.now()
22
 
23
+ # Check if the response is already cached and not expired
24
+ if url_hash in cache and (current_time - cache_expiry[url_hash]).seconds < 3600:
25
  print(f"Cache hit for URL: {url}")
26
  return cache[url_hash]
27
 
 
39
  response.raise_for_status()
40
  data = response.json()
41
 
42
+ # Cache the response and set the expiry time
43
  cache[url_hash] = data
44
+ cache_expiry[url_hash] = current_time
45
 
46
  return data
47
  except requests.RequestException as e: