Im actually doing this by dividing the time into periods, lasting Pt. eg. P=floor(now()/Pt) then when making changes in data i put it into period P, then every Pt i run a cron that saves everything in period P-1 into database + invalidates it, so if the script is run twice by accident it won't make the changes again (it's not important, can be skipped).
I also made a class that's acting as list with atomic OPs, with an index that's used for actually storing the data. The idea is that you need to find data belonging to each period. For your scenario probably you can solve it using a list of objects saved as UPDATE_TIME;DATA if (update_time + X < Now) you save into database then you get ONE object from the end of the list to fill in the empty place and shrink the list by 1. eg. item #;last_active; data 1 ; 10:09 ; aaaa........ 2 ; 09:09 ; bbbb........ --> needs saving 3 ; 10:15 ; cccc........ 4 ; 10:59 ; dddd........ SAVE... 1 ; 10:09 ; aaaa........ 4->2 ; 10:59 ; dddd........ 3 ; 10:15 ; cccc........ >
