简体   繁体   中英

Java: remove collection elements if they are inactive for specific timeout

I've got a web application. I store user sessions in collection. I also want to has an ability to renew user session. So I need to store even inactive user connections for some period, and if they are inactive for about a day, I should remove them. Like: expirationList.add(userSesion, timeout); , but each time I invoke userSesion's method I have to reset timeout.

Question: Are there any implementations of such collection?

You could use a Map with the integer being the time since they have last been active. You could then get all Users with an Integer over 9000 or whatever.

I have implemented something similar to this by storing a timestamp on the object. Update the timestamp each time you use the object. When you iterate through the collection or queue of items to be possibly deleted, check the timestamp first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM