0

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?

VB_
  • 45,112
  • 42
  • 145
  • 293
  • 1
    http://code.google.com/p/guava-libraries/wiki/CachesExplained and check this http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/cache/CacheBuilder.html#expireAfterWrite%28long,%20java.util.concurrent.TimeUnit%29 –  Feb 14 '14 at 15:02
  • searching SO i found the same like above with an example: http://stackoverflow.com/questions/3802370/java-time-based-map-cache-with-expiring-keys –  Feb 14 '14 at 15:04
  • @A.J. I can't find the expireAfterWrite method in official docs http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/MapMaker.html – VB_ Feb 14 '14 at 15:09
  • @A.J. but it's present in CacheBuilder, thanks. Can I reset a timeout of the item? – VB_ Feb 14 '14 at 15:10
  • the link is in the first comment (second link in it) but you have found it. and you are welcome. –  Feb 14 '14 at 15:12
  • the timeout is reset if the item is replaced before being purged. so you don't need to reset the timeout manually. yet, the timeout here is not per item. it is for all items. so you can't say item1 has timeout 1 day and item2 has timeout 3 days. –  Feb 14 '14 at 15:17
  • A.J. as I understand I need to replace my session each time I address it. But I've got responsive UI and user can generate 10-20 requests per minute. More over, as I understand it builds a concurrent map. Seems like a bottleneck. – VB_ Feb 14 '14 at 15:27

2 Answers2

1

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.

GreySwordz
  • 368
  • 1
  • 9
  • yes, but in such case I should manage it by my own. It'll be greate if I can shift responsibility for somebody else. – VB_ Feb 14 '14 at 15:02
1

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.

Teresa Carrigan
  • 668
  • 8
  • 14
  • I've read about Guava map, with timeout. http://stackoverflow.com/questions/3802370/java-time-based-map-cache-with-expiring-keys But I can't find the `expireAfterWrite` method in official docs – VB_ Feb 14 '14 at 15:08