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)

Parameters

  • limit

    • accepts the following format {call limit}/{time span}
      • supported timespan options:
        • m for minute
        • h for hour
        • d for day
  • methods

    • list of accepted call methods: ["GET", "POST"...]
  • exclude user

    • Boolean value if the program shall omit request.user or not

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.7.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.7-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for lightweight_ratelimit_django-1.0.7.tar.gz
Algorithm Hash digest
SHA256 d1fd5721e3b54b7af78ab4de9c82d1163de062e4c6ebdd23c729e7bb823e9add
MD5 a0970858c90afec7d234be69099d0b5d
BLAKE2b-256 dfc2d3cac2eabd17507588b84c4ca877e89b94db8d2f055ea2a0250440b1c027

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lightweight_ratelimit_django-1.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 cf9e121195249fff2acc8f543e85c6863d9411058edcf63783f006b22e314290
MD5 77a2ba149d229e83a234efe1c2a0d1ab
BLAKE2b-256 883e1c72d494d153e4a2e476dda36b29fc82038f3ee1d5fb940029a985cb7f75

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