Skip to content

Commit d6a27de

Browse files
[RTY-260013]: remove cache
1 parent ad443ec commit d6a27de

File tree

8 files changed

+64
-419
lines changed

8 files changed

+64
-419
lines changed

.flake8

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[flake8]
22
exclude =
33
.git,
4+
.venv-dev
45
__pycache__,
56
.poetry,
67
.venv,

README.md

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ This project is designed with:
2626
- Clean startup lifecycle (no racing configs)
2727
- Optional database dependency
2828
- Graceful degradation when MongoDB is unavailable
29-
- In-memory cache fallback
29+
3030
- QR code generation with auto folder creation
3131

3232
---
@@ -86,18 +86,17 @@ def generate_code(length=6):
8686

8787
## Tech Stack
8888

89-
| Layer | Technology |
90-
| ----------- | ----------------------- |
91-
| UI Backend | FastAPI |
92-
| API Backend | FastAPI |
93-
| Database | MongoDB (Optional) |
94-
| Cache | In-Memory (Python dict) |
95-
| Frontend | HTML, CSS, Vanilla JS |
96-
| QR Code | qrcode + Pillow |
97-
| API Server | Uvicorn |
98-
| Validation | Pydantic v2 |
99-
| Env Mgmt | python-dotenv |
100-
| Tooling | Poetry |
89+
| Layer | Technology |
90+
| ----------- | --------------------- |
91+
| UI Backend | FastAPI |
92+
| API Backend | FastAPI |
93+
| Database | MongoDB (Optional) |
94+
| Frontend | HTML, CSS, Vanilla JS |
95+
| QR Code | qrcode + Pillow |
96+
| API Server | Uvicorn |
97+
| Validation | Pydantic v2 |
98+
| Env Mgmt | python-dotenv |
99+
| Tooling | Poetry |
101100

102101
---
103102

@@ -212,7 +211,6 @@ TinyURL supports graceful offline mode.
212211
- UI loads
213212
- Short URLs are generated
214213
- QR codes are generated
215-
- Redirects work from in-memory cache
216214

217215
### What is disabled
218216

app/api/fast_api.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,21 @@
77
from fastapi.responses import HTMLResponse, JSONResponse
88
from pydantic import BaseModel, Field
99

10-
try:
10+
from typing import TYPE_CHECKING
11+
12+
if TYPE_CHECKING:
1113
from pymongo.errors import PyMongoError
12-
except ImportError:
13-
PyMongoError = Exception
14+
else:
15+
try:
16+
from pymongo.errors import PyMongoError
17+
except ImportError:
18+
19+
class PyMongoError(Exception):
20+
pass
21+
22+
1423
from app import __version__
1524
from app.db import data as db_data
16-
from app.utils.cache import get_short_from_cache, set_cache_pair
1725
from app.utils.helper import generate_code, is_valid_url, sanitize_url
1826

1927
SHORT_CODE_PATTERN = re.compile(r"^[A-Za-z0-9]{6}$")
@@ -147,9 +155,8 @@ def shorten_url(payload: ShortenRequest):
147155
)
148156

149157
if db_data.urls is None:
150-
cached_short = get_short_from_cache(original_url)
151-
short_code = cached_short or generate_code()
152-
set_cache_pair(short_code, original_url)
158+
short_code = generate_code()
159+
153160
return {
154161
"success": True,
155162
"input_url": original_url,

app/main.py

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,25 @@
1010
from fastapi.templating import Jinja2Templates
1111
from starlette.middleware.sessions import SessionMiddleware
1212

13-
try:
13+
from typing import TYPE_CHECKING
14+
15+
if TYPE_CHECKING:
1416
from pymongo.errors import PyMongoError
15-
except ImportError:
16-
PyMongoError = Exception # fallback for offline mode
17+
else:
18+
try:
19+
from pymongo.errors import PyMongoError
20+
except ImportError:
21+
22+
class PyMongoError(Exception):
23+
pass
24+
25+
26+
# fallback for offline mode
1727

1828
from app.api.fast_api import app as api_app
1929
from app.db import data as db_data
2030
from app.utils.qr import generate_qr_with_logo
21-
from app.utils.cache import (
22-
get_from_cache,
23-
get_short_from_cache,
24-
set_cache_pair,
25-
url_cache,
26-
rev_cache,
27-
)
31+
2832
from app.utils.config import load_env
2933
from app.utils.helper import (
3034
format_date,
@@ -147,22 +151,20 @@ async def create_short_url(
147151
pass
148152

149153
if not short_code:
150-
cached_short = get_short_from_cache(original_url)
151-
short_code = cached_short or generate_code()
152-
set_cache_pair(short_code, original_url)
153-
154-
if db_available(request) and db_data.urls is not None:
155-
try:
156-
db_data.urls.insert_one(
157-
{
158-
"short_code": short_code,
159-
"original_url": original_url,
160-
"created_at": datetime.datetime.utcnow(),
161-
"visit_count": 0,
162-
}
163-
)
164-
except PyMongoError:
165-
pass
154+
short_code = generate_code()
155+
156+
if db_available(request) and db_data.urls is not None:
157+
try:
158+
db_data.urls.insert_one(
159+
{
160+
"short_code": short_code,
161+
"original_url": original_url,
162+
"created_at": datetime.datetime.utcnow(),
163+
"visit_count": 0,
164+
}
165+
)
166+
except PyMongoError:
167+
pass
166168

167169
new_short_url = build_short_url(short_code, str(request.base_url))
168170
session.update(
@@ -201,16 +203,11 @@ async def delete_url(request: Request, short_code: str):
201203
except PyMongoError:
202204
return PlainTextResponse("Database connection lost.", status_code=503)
203205

204-
url_cache.pop(short_code, None)
205-
return PlainTextResponse("", status_code=204)
206+
return RedirectResponse("/recent", status_code=303)
206207

207208

208209
@app.get("/{short_code}")
209210
async def redirect_short(request: Request, short_code: str):
210-
cached_url = get_from_cache(short_code)
211-
if cached_url:
212-
return RedirectResponse(cached_url)
213-
214211
if not db_available(request) or db_data.urls is None:
215212
return PlainTextResponse("Database is not connected.", status_code=503)
216213

@@ -225,17 +222,7 @@ async def redirect_short(request: Request, short_code: str):
225222
if not doc:
226223
return PlainTextResponse("Invalid or expired short URL", status_code=404)
227224

228-
set_cache_pair(short_code, doc["original_url"])
229225
return RedirectResponse(doc["original_url"])
230226

231227

232228
app.mount("/api", api_app)
233-
234-
235-
@app.get("/_debug/cache")
236-
async def debug_cache():
237-
return {
238-
"url_cache": url_cache,
239-
"rev_cache": rev_cache,
240-
"size": {"url_cache": len(url_cache), "rev_cache": len(rev_cache)},
241-
}

0 commit comments

Comments
 (0)