A URL shortening service with a caching layer and click analytics, built with Flask and SQLite.
- Generates short codes for long URLs
- Redirects with a cache layer in front of the database (pattern identical to Redis GET/SET + TTL)
- Tracks click counts per short link
Flask · SQLite · (cache layer designed to swap in Redis directly)
pip install flask
python app.pycurl -X POST http://localhost:5002/api/shorten -H "Content-Type: application/json" \
-d '{"url":"https://example.com/some/very/long/path"}'
# -> {"short_code": "aZ3kT9", "short_url": "/r/aZ3kT9"}
curl -L http://localhost:5002/r/aZ3kT9 # redirects to the original URL
curl http://localhost:5002/api/stats/aZ3kT9 # {"code": "aZ3kT9", "clicks": 1, ...}Replace the CACHE dict + cache_get/cache_set functions with a redis.Redis() client:
r.set(code, target, ex=60) # cache_set
r.get(code) # cache_getThe rest of the app is unchanged — this is exactly why the cache logic is isolated into its own two functions.