Lightweight, decorator-based rate limiting for Django views using TTL-based cache keys.
Project description
🚦 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lightweight_ratelimit_django-1.0.7.tar.gz.
File metadata
- Download URL: lightweight_ratelimit_django-1.0.7.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d1fd5721e3b54b7af78ab4de9c82d1163de062e4c6ebdd23c729e7bb823e9add
|
|
| MD5 |
a0970858c90afec7d234be69099d0b5d
|
|
| BLAKE2b-256 |
dfc2d3cac2eabd17507588b84c4ca877e89b94db8d2f055ea2a0250440b1c027
|
File details
Details for the file lightweight_ratelimit_django-1.0.7-py3-none-any.whl.
File metadata
- Download URL: lightweight_ratelimit_django-1.0.7-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf9e121195249fff2acc8f543e85c6863d9411058edcf63783f006b22e314290
|
|
| MD5 |
77a2ba149d229e83a234efe1c2a0d1ab
|
|
| BLAKE2b-256 |
883e1c72d494d153e4a2e476dda36b29fc82038f3ee1d5fb940029a985cb7f75
|