|
1 | 1 | """News caching module for Past News application. |
2 | 2 |
|
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. |
5 | 5 | """ |
6 | 6 |
|
7 | | -from datetime import date |
| 7 | +from datetime import datetime, timedelta |
8 | 8 | from typing import Optional |
9 | 9 |
|
| 10 | +# Cache TTL in hours |
| 11 | +CACHE_TTL_HOURS = 4 |
| 12 | + |
10 | 13 |
|
11 | 14 | class NewsCache: |
12 | 15 | """ |
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. |
14 | 17 |
|
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. |
17 | 20 |
|
18 | 21 | Note: 'random' option is not cached since it should return different results. |
19 | 22 | """ |
20 | 23 |
|
21 | 24 | def __init__(self): |
22 | 25 | """Initialize empty cache.""" |
23 | 26 | self._cache = {} |
24 | | - self._cache_date = None |
| 27 | + self._cache_time = None |
25 | 28 |
|
26 | | - def _check_and_clear_old_cache(self, current_date: date) -> None: |
| 29 | + def _check_and_clear_expired_cache(self, current_time: datetime) -> None: |
27 | 30 | """ |
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. |
29 | 32 |
|
30 | 33 | Args: |
31 | | - current_date: The current date to check against |
| 34 | + current_time: The current datetime to check against |
32 | 35 | """ |
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]: |
41 | 46 | """ |
42 | 47 | Get cached article data for a specific option. |
43 | 48 |
|
44 | 49 | Args: |
45 | 50 | 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) |
47 | 52 |
|
48 | 53 | Returns: |
49 | 54 | Cached article data or None if not found |
50 | 55 |
|
51 | 56 | Note: |
52 | 57 | Returns None for 'random' option since it should not be cached. |
53 | 58 | """ |
54 | | - if current_date is None: |
55 | | - current_date = date.today() |
| 59 | + if current_time is None: |
| 60 | + current_time = datetime.now() |
56 | 61 |
|
57 | 62 | # Don't cache random options |
58 | 63 | if option == 'random': |
59 | 64 | return None |
60 | 65 |
|
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) |
63 | 68 |
|
64 | 69 | # Return cached data if available |
65 | 70 | return self._cache.get(option) |
66 | 71 |
|
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: |
68 | 73 | """ |
69 | 74 | Store article data in cache for a specific option. |
70 | 75 |
|
71 | 76 | Args: |
72 | 77 | option: Time period option ('one_week', 'two_weeks', 'one_month') |
73 | 78 | data: Article data to cache |
74 | | - current_date: The current date (default: today) |
| 79 | + current_time: The current datetime (default: now) |
75 | 80 |
|
76 | 81 | Note: |
77 | 82 | Does not cache 'random' option since it should not be cached. |
78 | 83 | """ |
79 | | - if current_date is None: |
80 | | - current_date = date.today() |
| 84 | + if current_time is None: |
| 85 | + current_time = datetime.now() |
81 | 86 |
|
82 | 87 | # Don't cache random options |
83 | 88 | if option == 'random': |
84 | 89 | return |
85 | 90 |
|
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) |
88 | 93 |
|
89 | 94 | # Store data |
90 | 95 | self._cache[option] = data |
91 | 96 |
|
92 | 97 | def clear(self) -> None: |
93 | 98 | """Clear all cached data.""" |
94 | 99 | self._cache.clear() |
95 | | - self._cache_date = None |
| 100 | + self._cache_time = None |
96 | 101 |
|
97 | | - def has(self, option: str, current_date: date = None) -> bool: |
| 102 | + def has(self, option: str, current_time: datetime = None) -> bool: |
98 | 103 | """ |
99 | 104 | Check if cache has data for a specific option. |
100 | 105 |
|
101 | 106 | Args: |
102 | 107 | option: Time period option to check |
103 | | - current_date: The current date (default: today) |
| 108 | + current_time: The current datetime (default: now) |
104 | 109 |
|
105 | 110 | Returns: |
106 | 111 | True if data is cached, False otherwise |
107 | 112 | """ |
108 | | - if current_date is None: |
109 | | - current_date = date.today() |
| 113 | + if current_time is None: |
| 114 | + current_time = datetime.now() |
110 | 115 |
|
111 | 116 | # Random is never cached |
112 | 117 | if option == 'random': |
113 | 118 | return False |
114 | 119 |
|
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) |
117 | 122 |
|
118 | 123 | return option in self._cache |
119 | 124 |
|
|
0 commit comments