Skip to content

Commit 1baa7ef

Browse files
committed
Change news cache from daily to 4-hour TTL
Instead of caching today's news for the entire day, the cache now expires after 4 hours. This allows for more frequent updates while still reducing API calls. https://claude.ai/code/session_01P77pEMoS1PHS5cEGyU2cZz
1 parent 33432ed commit 1baa7ef

4 files changed

Lines changed: 213 additions & 185 deletions

File tree

api/index.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66

77
import os
8-
from datetime import date
8+
from datetime import date, datetime
99
from flask import Flask, request, jsonify
1010

1111
from api.date_calculator import (
@@ -107,14 +107,14 @@ def get_news():
107107
'error': 'Missing required parameter: option'
108108
}), 400
109109

110-
# Get today's date for cache key
111-
today = date.today()
110+
# Get current time for cache
111+
now = datetime.now()
112112

113113
# Get cache instance
114114
cache = get_cache()
115115

116-
# Check if we have cached data for this option and today
117-
cached_data = cache.get(option, today)
116+
# Check if we have cached data for this option
117+
cached_data = cache.get(option, now)
118118
if cached_data is not None:
119119
# Return cached response
120120
return jsonify(cached_data)
@@ -170,7 +170,7 @@ def get_news():
170170
}
171171

172172
# Cache the response for non-random options
173-
cache.set(option, response_data, today)
173+
cache.set(option, response_data, now)
174174

175175
return jsonify(response_data)
176176

api/news_cache.py

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,124 @@
11
"""News caching module for Past News application.
22
3-
This module provides a simple cache that stores fetched news for a specific day.
4-
When a new day starts, old caches are automatically cleared.
3+
This module provides a simple cache that stores fetched news with a 4-hour TTL.
4+
After 4 hours, the cache is automatically cleared to fetch fresh news.
55
"""
66

7-
from datetime import date
7+
from datetime import datetime, timedelta
88
from typing import Optional
99

10+
# Cache TTL in hours
11+
CACHE_TTL_HOURS = 4
12+
1013

1114
class NewsCache:
1215
"""
13-
Simple in-memory cache for storing news articles by date and option.
16+
Simple in-memory cache for storing news articles with time-based expiration.
1417
15-
The cache stores articles for each day, with keys for 'one_week', 'two_weeks',
16-
and 'one_month'. When the date changes, old caches are automatically cleared.
18+
The cache stores articles with a 4-hour TTL. After 4 hours, the cache
19+
is automatically cleared when accessed.
1720
1821
Note: 'random' option is not cached since it should return different results.
1922
"""
2023

2124
def __init__(self):
2225
"""Initialize empty cache."""
2326
self._cache = {}
24-
self._cache_date = None
27+
self._cache_time = None
2528

26-
def _check_and_clear_old_cache(self, current_date: date) -> None:
29+
def _check_and_clear_expired_cache(self, current_time: datetime) -> None:
2730
"""
28-
Check if the date has changed and clear old cache if needed.
31+
Check if the cache has expired and clear it if needed.
2932
3033
Args:
31-
current_date: The current date to check against
34+
current_time: The current datetime to check against
3235
"""
33-
if self._cache_date is None:
34-
self._cache_date = current_date
35-
elif self._cache_date != current_date:
36-
# Date has changed, clear old cache
37-
self._cache.clear()
38-
self._cache_date = current_date
39-
40-
def get(self, option: str, current_date: date = None) -> Optional[dict]:
36+
if self._cache_time is None:
37+
self._cache_time = current_time
38+
else:
39+
elapsed = current_time - self._cache_time
40+
if elapsed >= timedelta(hours=CACHE_TTL_HOURS):
41+
# Cache has expired, clear it
42+
self._cache.clear()
43+
self._cache_time = current_time
44+
45+
def get(self, option: str, current_time: datetime = None) -> Optional[dict]:
4146
"""
4247
Get cached article data for a specific option.
4348
4449
Args:
4550
option: Time period option ('one_week', 'two_weeks', 'one_month')
46-
current_date: The current date (default: today)
51+
current_time: The current datetime (default: now)
4752
4853
Returns:
4954
Cached article data or None if not found
5055
5156
Note:
5257
Returns None for 'random' option since it should not be cached.
5358
"""
54-
if current_date is None:
55-
current_date = date.today()
59+
if current_time is None:
60+
current_time = datetime.now()
5661

5762
# Don't cache random options
5863
if option == 'random':
5964
return None
6065

61-
# Check and clear old cache
62-
self._check_and_clear_old_cache(current_date)
66+
# Check and clear expired cache
67+
self._check_and_clear_expired_cache(current_time)
6368

6469
# Return cached data if available
6570
return self._cache.get(option)
6671

67-
def set(self, option: str, data: dict, current_date: date = None) -> None:
72+
def set(self, option: str, data: dict, current_time: datetime = None) -> None:
6873
"""
6974
Store article data in cache for a specific option.
7075
7176
Args:
7277
option: Time period option ('one_week', 'two_weeks', 'one_month')
7378
data: Article data to cache
74-
current_date: The current date (default: today)
79+
current_time: The current datetime (default: now)
7580
7681
Note:
7782
Does not cache 'random' option since it should not be cached.
7883
"""
79-
if current_date is None:
80-
current_date = date.today()
84+
if current_time is None:
85+
current_time = datetime.now()
8186

8287
# Don't cache random options
8388
if option == 'random':
8489
return
8590

86-
# Check and clear old cache
87-
self._check_and_clear_old_cache(current_date)
91+
# Check and clear expired cache
92+
self._check_and_clear_expired_cache(current_time)
8893

8994
# Store data
9095
self._cache[option] = data
9196

9297
def clear(self) -> None:
9398
"""Clear all cached data."""
9499
self._cache.clear()
95-
self._cache_date = None
100+
self._cache_time = None
96101

97-
def has(self, option: str, current_date: date = None) -> bool:
102+
def has(self, option: str, current_time: datetime = None) -> bool:
98103
"""
99104
Check if cache has data for a specific option.
100105
101106
Args:
102107
option: Time period option to check
103-
current_date: The current date (default: today)
108+
current_time: The current datetime (default: now)
104109
105110
Returns:
106111
True if data is cached, False otherwise
107112
"""
108-
if current_date is None:
109-
current_date = date.today()
113+
if current_time is None:
114+
current_time = datetime.now()
110115

111116
# Random is never cached
112117
if option == 'random':
113118
return False
114119

115-
# Check and clear old cache
116-
self._check_and_clear_old_cache(current_date)
120+
# Check and clear expired cache
121+
self._check_and_clear_expired_cache(current_time)
117122

118123
return option in self._cache
119124

src/news_cache.py

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,124 @@
11
"""News caching module for Past News application.
22
3-
This module provides a simple cache that stores fetched news for a specific day.
4-
When a new day starts, old caches are automatically cleared.
3+
This module provides a simple cache that stores fetched news with a 4-hour TTL.
4+
After 4 hours, the cache is automatically cleared to fetch fresh news.
55
"""
66

7-
from datetime import date
7+
from datetime import datetime, timedelta
88
from typing import Optional
99

10+
# Cache TTL in hours
11+
CACHE_TTL_HOURS = 4
12+
1013

1114
class NewsCache:
1215
"""
13-
Simple in-memory cache for storing news articles by date and option.
16+
Simple in-memory cache for storing news articles with time-based expiration.
1417
15-
The cache stores articles for each day, with keys for 'one_week', 'two_weeks',
16-
and 'one_month'. When the date changes, old caches are automatically cleared.
18+
The cache stores articles with a 4-hour TTL. After 4 hours, the cache
19+
is automatically cleared when accessed.
1720
1821
Note: 'random' option is not cached since it should return different results.
1922
"""
2023

2124
def __init__(self):
2225
"""Initialize empty cache."""
2326
self._cache = {}
24-
self._cache_date = None
27+
self._cache_time = None
2528

26-
def _check_and_clear_old_cache(self, current_date: date) -> None:
29+
def _check_and_clear_expired_cache(self, current_time: datetime) -> None:
2730
"""
28-
Check if the date has changed and clear old cache if needed.
31+
Check if the cache has expired and clear it if needed.
2932
3033
Args:
31-
current_date: The current date to check against
34+
current_time: The current datetime to check against
3235
"""
33-
if self._cache_date is None:
34-
self._cache_date = current_date
35-
elif self._cache_date != current_date:
36-
# Date has changed, clear old cache
37-
self._cache.clear()
38-
self._cache_date = current_date
39-
40-
def get(self, option: str, current_date: date = None) -> Optional[dict]:
36+
if self._cache_time is None:
37+
self._cache_time = current_time
38+
else:
39+
elapsed = current_time - self._cache_time
40+
if elapsed >= timedelta(hours=CACHE_TTL_HOURS):
41+
# Cache has expired, clear it
42+
self._cache.clear()
43+
self._cache_time = current_time
44+
45+
def get(self, option: str, current_time: datetime = None) -> Optional[dict]:
4146
"""
4247
Get cached article data for a specific option.
4348
4449
Args:
4550
option: Time period option ('one_week', 'two_weeks', 'one_month')
46-
current_date: The current date (default: today)
51+
current_time: The current datetime (default: now)
4752
4853
Returns:
4954
Cached article data or None if not found
5055
5156
Note:
5257
Returns None for 'random' option since it should not be cached.
5358
"""
54-
if current_date is None:
55-
current_date = date.today()
59+
if current_time is None:
60+
current_time = datetime.now()
5661

5762
# Don't cache random options
5863
if option == 'random':
5964
return None
6065

61-
# Check and clear old cache
62-
self._check_and_clear_old_cache(current_date)
66+
# Check and clear expired cache
67+
self._check_and_clear_expired_cache(current_time)
6368

6469
# Return cached data if available
6570
return self._cache.get(option)
6671

67-
def set(self, option: str, data: dict, current_date: date = None) -> None:
72+
def set(self, option: str, data: dict, current_time: datetime = None) -> None:
6873
"""
6974
Store article data in cache for a specific option.
7075
7176
Args:
7277
option: Time period option ('one_week', 'two_weeks', 'one_month')
7378
data: Article data to cache
74-
current_date: The current date (default: today)
79+
current_time: The current datetime (default: now)
7580
7681
Note:
7782
Does not cache 'random' option since it should not be cached.
7883
"""
79-
if current_date is None:
80-
current_date = date.today()
84+
if current_time is None:
85+
current_time = datetime.now()
8186

8287
# Don't cache random options
8388
if option == 'random':
8489
return
8590

86-
# Check and clear old cache
87-
self._check_and_clear_old_cache(current_date)
91+
# Check and clear expired cache
92+
self._check_and_clear_expired_cache(current_time)
8893

8994
# Store data
9095
self._cache[option] = data
9196

9297
def clear(self) -> None:
9398
"""Clear all cached data."""
9499
self._cache.clear()
95-
self._cache_date = None
100+
self._cache_time = None
96101

97-
def has(self, option: str, current_date: date = None) -> bool:
102+
def has(self, option: str, current_time: datetime = None) -> bool:
98103
"""
99104
Check if cache has data for a specific option.
100105
101106
Args:
102107
option: Time period option to check
103-
current_date: The current date (default: today)
108+
current_time: The current datetime (default: now)
104109
105110
Returns:
106111
True if data is cached, False otherwise
107112
"""
108-
if current_date is None:
109-
current_date = date.today()
113+
if current_time is None:
114+
current_time = datetime.now()
110115

111116
# Random is never cached
112117
if option == 'random':
113118
return False
114119

115-
# Check and clear old cache
116-
self._check_and_clear_old_cache(current_date)
120+
# Check and clear expired cache
121+
self._check_and_clear_expired_cache(current_time)
117122

118123
return option in self._cache
119124

0 commit comments

Comments
 (0)