我有一个数据库,在从updated_at
行开始30分钟后,我想删除数据库中的行。我知道如何基于这个时间找到行,但我不知道在定时间隔内实际删除它的技术。
实现这一目标的好方法是什么?是否有Rails-esque?
答案 0 :(得分:1)
Delayed Job是这种事情的常用工具。
您可以使用perform
方法创建一个简单的类来删除条目:
class DeleteTheThing
attr_accessor :thing_id
def initialize(the_thing)
@thing_id = the_thing.id
end
def perform
# Delete the thing if it should still be deleted
end
end
然后,当您决定删除对象时,请排队延迟的任务:
Delayed::Job.enqueue(
DeleteTheThing.new(the_thing),
:run_at => 30.minutes.from_now.getutc
)
答案 1 :(得分:1)
答案 2 :(得分:0)
为什么不使用memcached而不是db?然后你可以做类似
的事情 def get_data(user)
Rails.cache.fetch("cached_data-#{user.name}", :expires_in => 30.minutes) do
## create some hash here
{:name => user.name, :more_data => user.data}
end
end
数据将持续存在,直到散列未使用30分钟。
如果您喜欢Active记录,那么每5分钟左右就可以有一个cron作业,但这不能很好地扩展