We run a Python (Django) application that temporarily stores data in Redis. Sometimes though it happens that data by the time it gets to get stored in Redis is already expired and therefore setex is called with an expiration time of <= 0.
Is there a way to make Redis silently discard such a setex command without openly complaining with:
ResponseError: invalid expire time in setex
Right now I cannot do that from the application.
Django has very good primitives for handling this situation. Just set up redis as one of your CACHES and use the cachr.set method to set an expiration n seconds from time of storage.
You can write a LUA script that does that.
Send the key name as KEYS, and value string and expiration as ARGV:
You can also call set only if expiration > 0. You can return any answer you want, regardless of the operation made.
Load you LUA script once with SCRIPT LOAD, save its SHA1 answer, and use it with EVALSHA to avoid sending/compiling the lua script for every usage.