I have a password recovery system that allows users to recover their password. These requests are stored in the recoveries
table. This table contains timestamps in the expiry
column that state the time of each row's creation.
I want this event to run individually for every new row in recoveries
, so that they expire exactly 10 minutes after creation.
I have this code, but I don't know how to make it run exactly 10 minutes after the creation of each individual row in recoveries
:
CREATE EVENT `delete_recoveries`
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 10 MINUTE
ON COMPLETION PRESERVE
DO BEGIN
DELETE FROM `recoveries` WHERE `expiry` < DATE_SUB(NOW(), INTERVAL 10 MINUTE);
END;
I think part of your problem here is that you're using the wrong tool for the job. What you really need is a queueing system.
Change your design just a bit. Instead of putting all the work in your database, make your application layer add things to the queue, and check it periodically for jobs to do. Most queues live in memory, so reading is really fast, and with many of them you can add a delay until an item is visible on the queue.
Alternatively, use a cron job to check your database, where you basically do something every ten minutes.