|
32 | 32 | increment_visit_cache, |
33 | 33 | url_cache, |
34 | 34 | remove_cache_key, |
| 35 | + rev_cache, |
35 | 36 | ) |
36 | 37 | from app.utils.config import DOMAIN, MAX_RECENT_URLS, CACHE_PURGE_TOKEN |
37 | 38 | from app.utils.helper import generate_code, is_valid_url, sanitize_url, format_date |
@@ -161,25 +162,27 @@ def cache_list_ui(): |
161 | 162 |
|
162 | 163 |
|
163 | 164 | @ui_router.delete("/cache/purge", response_class=PlainTextResponse) |
164 | | -def cache_purge_ui(cache_token: str = Header(..., alias="Cache-Token")): |
| 165 | +def cache_purge_ui(x_cache_token: str = Header(..., alias="X-Cache-Token")): |
165 | 166 | """ |
166 | 167 | Force delete everything from cache (secured by header) |
167 | 168 | """ |
168 | | - if cache_token != CACHE_PURGE_TOKEN: |
| 169 | + if x_cache_token != CACHE_PURGE_TOKEN: |
169 | 170 | raise HTTPException(status_code=401, detail="Unauthorized") |
170 | 171 |
|
171 | | - clear_cache() |
| 172 | + if not url_cache and not rev_cache: |
| 173 | + return "No URLs in cache" |
172 | 174 |
|
| 175 | + clear_cache() |
173 | 176 | return "cleared ALL" |
174 | 177 |
|
175 | 178 |
|
176 | 179 | @ui_router.patch("/cache/remove") |
177 | 180 | def cache_remove_one_ui( |
178 | 181 | key: str = Query(..., description="short_code OR original_url"), |
179 | | - cache_token: str = Header(..., alias="Cache-Token"), |
| 182 | + x_cache_token: str = Header(..., alias="X-Cache-Token"), |
180 | 183 | ): |
181 | 184 | # 🔐 Header security |
182 | | - if cache_token != CACHE_PURGE_TOKEN: |
| 185 | + if x_cache_token != CACHE_PURGE_TOKEN: |
183 | 186 | raise HTTPException(status_code=401, detail="Unauthorized") |
184 | 187 |
|
185 | 188 | removed = remove_cache_key(key) |
@@ -228,29 +231,44 @@ def delete_recent_api(short_code: str): |
228 | 231 | recent = get_recent_from_cache(MAX_RECENT_URLS) or [] |
229 | 232 | removed_from_cache = False |
230 | 233 |
|
| 234 | + # Try removing from cache (memory only) |
231 | 235 | for i, item in enumerate(recent): |
232 | 236 | code = item.get("short_code") or item.get("code") |
233 | 237 | if code == short_code: |
234 | 238 | recent.pop(i) |
235 | 239 | removed_from_cache = True |
236 | 240 | break |
237 | 241 |
|
238 | | - db_deleted = False |
239 | 242 | db_available = db.is_connected() |
| 243 | + db_deleted = False |
240 | 244 |
|
| 245 | + # If DB available → rely ONLY on DB |
241 | 246 | if db_available: |
242 | | - db_deleted = bool(db.delete_by_short_code(short_code)) |
| 247 | + db_deleted = db.delete_by_short_code(short_code) |
| 248 | + |
| 249 | + if not db_deleted: |
| 250 | + raise HTTPException( |
| 251 | + status_code=404, detail=f"short_code '{short_code}' not found" |
| 252 | + ) |
| 253 | + |
| 254 | + return { |
| 255 | + "status": "deleted", |
| 256 | + "short_code": short_code, |
| 257 | + "db_deleted": True, |
| 258 | + "db_available": True, |
| 259 | + } |
243 | 260 |
|
244 | | - if not removed_from_cache and not db_deleted: |
| 261 | + # If DB NOT available → rely on cache only |
| 262 | + if not removed_from_cache: |
245 | 263 | raise HTTPException( |
246 | 264 | status_code=404, detail=f"short_code '{short_code}' not found" |
247 | 265 | ) |
248 | 266 |
|
249 | 267 | return { |
250 | 268 | "status": "deleted", |
251 | 269 | "short_code": short_code, |
252 | | - "db_deleted": db_deleted, |
253 | | - "db_available": db_available, |
| 270 | + "db_deleted": False, |
| 271 | + "db_available": False, |
254 | 272 | } |
255 | 273 |
|
256 | 274 |
|
|
0 commit comments