简体   繁体   English

Java:如果集合元素对特定超时处于非活动状态,则删除它们

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

I've got a web application. 我有一个Web应用程序。 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); 喜欢: expirationList.add(userSesion, timeout); , but each time I invoke userSesion's method I have to reset timeout. ,但每次调用userSesion的方法时,我都要重置超时。

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. 然后,您可以获得具有超过9000或更大的整数的所有用户。

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. 当您遍历要删除的项的集合或队列时,请先检查时间戳。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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