符合TTL的MySQL记录 [英] MySQL record that would subject to TTL
问题描述
是否有可能在MySQL数据库中创建一条记录,该记录将受 TTL (生存时间)选项的限制.
Is it possible, to create a record in MySQL database, that would subject to TTL (Time to live) option.
我想做一个简单的密码恢复功能,我需要存储一个激活密钥,该激活密钥仅在数据库中存储3600秒,然后在那之后自动删除?我知道还有很多其他方法可以实现此目的,但是它们不像 TTL 功能那样简单.
I want to make a simple password recovery feature and I need to store an activation key, that would be stored in database for only 3600 seconds and then be deleted automatically after that time? I know there are bunch of other ways to achieve this but they are not as straight forward as an idea of TTL functionality.
我想MySQL没有这样的功能,但是我只是想也许我缺少了什么?
I guess that MySQL doesn't have such functionality but I just thought that maybe I'm missing something and there is?
推荐答案
我刚刚发现MySQL 5.1+具有事件调度程序. MySQL Event Scheduler 管理事件的调度和执行-根据计划运行的任务.
I just found out that MySQL 5.1+ has event scheduler. The MySQL Event Scheduler manages the scheduling and execution of events - tasks that run according to schedule.
存储的例程需要MySQL数据库中的事件表.该表是在MySQL安装过程中创建的.
Stored routines require the event table in the MySQL database. This table is created during the MySQL installation procedure.
使用该语法的语法是:
CREATE EVENT
ClearUserActivationCodes
ON SCHEDULE EVERY 1 DAY
DO
BEGIN
DELETE FROM
user_activation_code
WHERE code_time_stamp < NOW()
END
这非常有用,完全可以满足我无需使用cron作业即可自动清除表的需求.
It's quite useful and fully satisfies my needs for automatically clearing tables without using cron jobs.
这篇关于符合TTL的MySQL记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!