Skip to main content

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'

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 Redis in 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)

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lightweight_ratelimit_django-1.0.6.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lightweight_ratelimit_django-1.0.6-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file lightweight_ratelimit_django-1.0.6.tar.gz.

File metadata

File hashes

Hashes for lightweight_ratelimit_django-1.0.6.tar.gz
Algorithm Hash digest
SHA256 a0145a34fb09921ce909d9ae892dd1e25952c044786baef300087ae9705886ec
MD5 22202cf60fb1c339a5a82a1ad009f851
BLAKE2b-256 d4deb56645d021b32e1538b5e9379f314407b97e0252833531750f060a8a949e

See more details on using hashes here.

File details

Details for the file lightweight_ratelimit_django-1.0.6-py3-none-any.whl.

File metadata

File hashes

Hashes for lightweight_ratelimit_django-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 8b7ea0cafb3705e383771e7f20de20904aadc07095948ab10c114a2544bb5774
MD5 7eb62308651a06fa81a18cb6f3f899f8
BLAKE2b-256 1ec3d98a33f00b06fb8a6f76904ceaf8b3283bed22790efc05a4d3a4f2d7d555

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page