I m building a website. It has groups that users can join.
The difference between this and "normal" groups is that membership is temporary - when a user joins a group, he decides the length of membership: 5 days, a week, 2 weeks, etc (choices are pre-defined). Or potentially all memberships could be set to be of the same length - say a week - if that makes thing simpler.
I d like to have a running tally of the number of members of each group. The number doesn t need to be accurate up to the last second. But it can t be too out of date either - say, should be updated once a day.
The "obvious" way to calculate the number of members seems to be running a cron job, say daily, and go through every member of every group one by one. If a membership has expired, remove that member from the group and decrement the group s membership count by 1.
That approach seems very inefficient and not very scalable. With a large number of groups, it could take forever.
Can you think of a better way to do this? The membership counts do not need to be accurate to the latest second. It can be approximate and (slightly) out of date. Also if it makes a difference all memberships can be set to be of the same length, say a week.