Search code examples
phparchitecturesessiondelayed-job

How to implement temporarily undoable operations in PHP and JavaScript?


I would like to implement operations that is undoable in a short time after the user have done them. I.e. if a user upvotes a photo in a photo-site, he/she can undo the vote within 30 seconds. This is similar to how the voting on StackOverflow is working, you can undo your votes for a short time.

How should I implement it? I guess that I shouldn't commit the votes to the database before the delay has timed out, for performance reasons. And it's hard to do it in JavaScript because then is the vote lost if the user change website before the delay has timed out. Maybe I could do it in cookies, or maybe the best solution is to do it in PHP Sessions, but How do I commit the operation after the delay then?


Solution

  • I suggest to yes, commit the vote with a timestamp into your database. It is a clean and easy solution. If you get performance problems, then you could use an in-memory database like sqlite as secondary backend to your vote data layer.