I've written a module that stores data in a separate table. Because the data needs to be in a table (for a performant SQL JOIN) and should only be valid momentarily, the table uses the session ID as a field.
Now, as the table grows with time, we need some kind of cleanup. However, as the table contains data that is still valid, as well as data that is invalid on the account of being associated with expired sessions, we need to clean only the data from invalid and/or expired sessions.
The basic question is: How can we iterate over the currently active sessions? We would then clean all data associated with sessions that are not part of the collection of active sessions, maybe with some kind of cron job.
Another possibility I would prefer is to clean the data whenever a session becomes invalid. Is there an event or any other way to run code when a session becomes invalid?