skip to Main Content

How can I change the key name stored in Redis by Flask-limiter?

https://flask-limiter.readthedocs.io/en/stable/

2

Answers


  1. You can set a key prefix

    limiter = Limiter(
        app,
        key_func=get_remote_address,
        default_limits=["200 per day", "50 per hour"],
        storage_uri='redis://localhost:6379',
        key_prefix='DTM',
    )
    

    This changes the keyname from

    LIMITER/127.0.0.1/slow/1/1/day
    

    to

    LIMITER/DTM/127.0.0.1/slow/1/1/day
    
    Login or Signup to reply.
  2. To add to the response by namizaru: you can only control some parts of the key with this library.

    Given the example from namizaru:

    LIMITER/127.0.0.1/slow/1/1/day
    

    The string "LIMITER" comes from the limits library (https://github.com/alisaifee/limits), which Flask-limiter wraps. I don’t see a good way to override this.

    "127.0.0.1" is the result of the key_func parameter passed to Limiter (get_remote_address) and should return a unique string that identifies the rate-limited resource, as far as I understand. So, you can provide your own callable there, but that only changes one portion of the key.

    Meanwhile, the delimiter ("/") also comes from the limits library and does not seem overridable.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search