djangodjango-rest-frameworkredisdjango-redis

How to see keys set by django-ratelimit in redis server?


I am using the following

django==3.1.3
django-ratelimit==3.0.1
django-redis==4.12.1
djangorestframework==3.12.2

I am trying to set up a rate-limit to limit the number of times a POST request is called.

I have this in my settings:

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        },
        "KEY_PREFIX": "my_app"
    }
}

I have this in views.py. (A very simplified version)

@ratelimit(key='ip', rate='1/5m', method=['GET', 'POST'])
def rate_limit_func(request):
     if request.method == 'POST':
        return Response(status='200')

The rate limit is working as expected. But I cannot see any keys getting stored in the redis server

127.0.0.1:6379> ping
PONG
127.0.0.1:6379> get ip
(nil)
127.0.0.1:6379> keys *
(empty list or set)

I also tried searching the key using django shell

>>> from django.core.cache import cache
>>> 'ip' in cache
False

I am not sure if I have set this up correctly and it will work in production. Also, where are the cache values being set?


Solution

  • I was looking at the wrong place. By default, redis has databases indexed from 0 to 15. In my Django settings.py, I am using database with index 1 for my cache. I can select that database using redis-cli -n -1. So,

    āžœ redis-cli -n 1
    127.0.0.1:6379[1]> keys *
    

    This gave me the list of keys that were being stored by django-ratelimit

    This StackOverflow answer helped me to figure this out.