🚦 lightweight-ratelimit-django
A lightweight, decorator-based rate limiting solution for Django views, backed by Django’s cache framework and designed for accuracy, simplicity, and low overhead.
This package enforces request limits using strict TTL-based expiration, making it suitable for APIs and critical endpoints.
✨ Features
- Decorator-based rate limiting for Django views
- Supports authenticated users and anonymous IP-based limiting
- Accurate retry-after timing
- No middleware required
- Redis / Memcached compatible
- Minimal configuration
📦 Installation
pip install lightweight-ratelimit-django
⚠️ Cache Backend Requirements (IMPORTANT)
This package requires a cache backend with strict TTL enforcement. This package relies on cache key TTL to calculate accurate retry-after timing.
❌ Unsupported Backends
The following backends are not supported and will break rate limiting logic:
- LocMemCache
- Will result
AttributeError: 'LocMemCache' object has no attribute 'ttl'
- Will result
Do not use LocMemCache, even in development, since package relies on cache key TTL to calculate accurate retry-after timing.
✅ Supported Backends
- Redis (django-redis) – recommended (created with
Redisin mind) - Memcached
- Database cache (acceptable but slower)
🔧 Redis Configuration Example
# settings.py
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
}
}
🚀 Basic Usage
from lightweight_ratelimit_django import RateLimiter
from django.http import JsonResponse
from django.contrib.auth.decorators import login_required
@login_required
@RateLimiter.view_rate_limit()
def protected_api_view(request):
return JsonResponse({"status": "success"})
Defaults
- Default method:
GET - Default limit:
50/h - Default
exclude_user=False(meaning if the request.user is logged in, limit will be calculated on the user)
Parameters
-
limit- accepts the following format
{call limit}/{time span}- supported timespan options:
mfor minutehfor hourdfor day
- supported timespan options:
- accepts the following format
-
methods- list of accepted call methods:
["GET", "POST"...]
- list of accepted call methods:
-
exclude user- Boolean value if the program shall omit
request.useror not
- Boolean value if the program shall omit
Example with custom configuration
@RateLimiter.view_rate_limit(limit="10/m", methods=["POST"], exclude_user=True)
def create_resource(request):
return JsonResponse({"status": "created"})
📄 License
MIT License
Metadata
Release files for lightweight-ratelimit-django 1.0.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| lightweight_ratelimit_django-1.0.7.tar.gz | 8.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| lightweight_ratelimit_django-1.0.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.5 kB
Release files / lightweight_ratelimit_django-1.0.7.tar.gz
| Download URL | lightweight_ratelimit_django-1.0.7.tar.gz |
|---|---|
| Size | 8.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d1fd5721e3b54b7af78ab4de9c82d1163de062e4c6ebdd23c729e7bb823e9add
|
|
BLAKE2b-256 checksum How to use checksums |
dfc2d3cac2eabd17507588b84c4ca877e89b94db8d2f055ea2a0250440b1c027
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.9
|
Release files / lightweight_ratelimit_django-1.0.7-py3-none-any.whl
| Download URL | lightweight_ratelimit_django-1.0.7-py3-none-any.whl |
|---|---|
| Size | 9.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cf9e121195249fff2acc8f543e85c6863d9411058edcf63783f006b22e314290
|
|
BLAKE2b-256 checksum How to use checksums |
883e1c72d494d153e4a2e476dda36b29fc82038f3ee1d5fb940029a985cb7f75
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.9
|