How can I change the key name stored in Redis by Flask-limiter?
Asked
Active
Viewed 422 times
0
-
Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 15 '21 at 23:51
2 Answers
0
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

namizaru
- 646
- 3
- 5
0
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.

Andrew Brookins
- 36
- 2