Skip to content

Commit d7f5a0d

Browse files
[RTY-260025]: feat(api): enhance cache purge and delete_recent_api for improved DB handling and response
1 parent a84ffa4 commit d7f5a0d

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

app/routes.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
increment_visit_cache,
3333
url_cache,
3434
remove_cache_key,
35+
rev_cache,
3536
)
3637
from app.utils.config import DOMAIN, MAX_RECENT_URLS, CACHE_PURGE_TOKEN
3738
from app.utils.helper import generate_code, is_valid_url, sanitize_url, format_date
@@ -161,25 +162,27 @@ def cache_list_ui():
161162

162163

163164
@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")):
165166
"""
166167
Force delete everything from cache (secured by header)
167168
"""
168-
if cache_token != CACHE_PURGE_TOKEN:
169+
if x_cache_token != CACHE_PURGE_TOKEN:
169170
raise HTTPException(status_code=401, detail="Unauthorized")
170171

171-
clear_cache()
172+
if not url_cache and not rev_cache:
173+
return "No URLs in cache"
172174

175+
clear_cache()
173176
return "cleared ALL"
174177

175178

176179
@ui_router.patch("/cache/remove")
177180
def cache_remove_one_ui(
178181
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"),
180183
):
181184
# 🔐 Header security
182-
if cache_token != CACHE_PURGE_TOKEN:
185+
if x_cache_token != CACHE_PURGE_TOKEN:
183186
raise HTTPException(status_code=401, detail="Unauthorized")
184187

185188
removed = remove_cache_key(key)
@@ -228,29 +231,44 @@ def delete_recent_api(short_code: str):
228231
recent = get_recent_from_cache(MAX_RECENT_URLS) or []
229232
removed_from_cache = False
230233

234+
# Try removing from cache (memory only)
231235
for i, item in enumerate(recent):
232236
code = item.get("short_code") or item.get("code")
233237
if code == short_code:
234238
recent.pop(i)
235239
removed_from_cache = True
236240
break
237241

238-
db_deleted = False
239242
db_available = db.is_connected()
243+
db_deleted = False
240244

245+
# If DB available → rely ONLY on DB
241246
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+
}
243260

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:
245263
raise HTTPException(
246264
status_code=404, detail=f"short_code '{short_code}' not found"
247265
)
248266

249267
return {
250268
"status": "deleted",
251269
"short_code": short_code,
252-
"db_deleted": db_deleted,
253-
"db_available": db_available,
270+
"db_deleted": False,
271+
"db_available": False,
254272
}
255273

256274

app/utils/db.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,7 @@ def connect_db(max_retries: int = 1) -> bool:
4040
Returns:
4141
True if connection successful, False otherwise
4242
"""
43-
global \
44-
client, \
45-
db, \
46-
collection, \
47-
connection_state, \
48-
last_connection_attempt, \
49-
connection_error
43+
global client, db, collection, connection_state, last_connection_attempt, connection_error
5044

5145
if not MONGO_INSTALLED:
5246
logger.error("PyMongo is not installed")

0 commit comments

Comments
 (0)