The approach may be:
you create middleware that does the following in process_response:
- check a cookie called online, but only if the user is authenticated.
- If the cookie does not exist,
- set a cookie called "online" with a value of "1"
- set the duration of the cookie to 10 minutes.
- update the "last_login" auth.User field for this user with the current lifetime
Now you have all registered users in your auth.User table. All users who have last_login newer than datetime.now () - interval (15 minutes) can be considered "online".
A database will be recorded for each registered user every 10 minutes. Adjust the values ββof β10β and β15β to suit your needs.
The advantage is that database entries are rare (according to your two 10/15 digital settings). And to optimize the speed, make sure that last_login is indexed, so the filter in this field, including Count, is really fast.
Hope this helps.
mawimawi
source share