<p>I want a data structure that will allow querying <em>how many items in last <strong>X</strong> minutes</em>. An item may just be a simple identifier or a more complex data structure, preferably the timestamp of the item will be in the item, rather than stored outside (as a hash or similar, wouldn't want to have problems with multiple items having same timestamp).</p>

<p>So far it seems that with LINQ I could easily filter items with timestamp greater than a given time and aggregate a count. Though I'm hesitant to try to work .NET 3.5 specific stuff into my production environment yet. Are there any other suggestions for a similar data structure?</p>

<p>The other part that I'm interested in is <em>aging</em> old data out, If I'm only going to be asking for counts of items less than 6 hours ago I would like anything older than that to be removed from my data structure because this may be a long-running program.</p>

[{'answer_id': 15316, 'author': 'dmo', 'author_id': 1807, 'author_profile': 'https://Stackoverflow.com/users/1807', 'pm_score': 2, 'selected': False, 'text': '<p>I think that an important consideration will be the frequency of querying vs. adding/removing.  If you will do frequent querying (especially if you\'ll have a large collection) a B-tree may be the way to go:</p>\n\n<p><a href="http://en.wikipedia.org/wiki/B-tree" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/B-tree</a></p>\n\n<p>You could have some thread go through and clean up this tree periodically or make it part of the search (again, depending on the usage).  Basically, you\'ll do a tree search to find the spot "x minutes ago", then count the number of children on the nodes with newer times.  If you keep the number of children under the nodes up to date, this sum can be done quickly.</p>\n'}, {'answer_id': 15904, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 3, 'selected': True, 'text': '<p>A simple linked list can be used for this.</p>\n\n<p>Basically you add new items to the end, and remove too old items from the start, it is a cheap data structure.</p>\n\n<p>example-code:</p>\n\n<pre><code>list.push_end(new_data)\nwhile list.head.age &gt;= age_limit:\n    list.pop_head()\n</code></pre>\n\n<p>If the list will be busy enough to warrant chopping off larger pieces than one at a time, then I agree with <a href="https://stackoverflow.com/users/1807/dmo">dmo</a>, use a tree structure or something similar that allows pruning on a higher level.</p>\n'}, {'answer_id': 2699852, 'author': 'ehosca', 'author_id': 199771, 'author_profile': 'https://Stackoverflow.com/users/199771', 'pm_score': 2, 'selected': False, 'text': '<p>a cache with sliding expiration will do the job ....</p>\n\n<p>stuff your items in and the cache handles the aging ....</p>\n\n<p><a href="http://www.sharedcache.com/cms/" rel="nofollow noreferrer">http://www.sharedcache.com/cms/</a></p>\n'}]
['https://Stackoverflow.com/questions/15272', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/163/']