Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Provided the memory footprint is tolerable, I would suggest a <code>Hashset&lt;string&gt;</code> for the strings, and a queue to store a <code>Tuple&lt;DateTime, String&gt;</code>. Something like:</p> <pre><code>Hashset&lt;string&gt; Strings = new HashSet&lt;string&gt;(); Queue&lt;Tuple&lt;DateTime, String&gt;&gt; Expirations = new Queue&lt;Tuple&lt;DateTime, String&gt;&gt;(); </code></pre> <p>Now, when a string comes in:</p> <pre><code>if (Strings.Add(s)) { // string is new. process it. // and add it to the expiration queue Expirations.Enqueue(new Tuple&lt;DateTime, String&gt;(DateTime.Now + ExpireTime, s)); } </code></pre> <p>And, somewhere you'll have to check for the expirations. Perhaps every time you get a new string, you do this:</p> <pre><code>while (Expirations.Count &gt; 0 &amp;&amp; Expirations.Peek().Item1 &lt; DateTime.Now) { var e = Expirations.Dequeue(); Strings.Remove(e.Item2); } </code></pre> <p>It'd be hard to beat the performance of <code>Hashset</code> here. Granted, you're storing the strings, but that's going to be the only way to guarantee no false positives.</p> <p>You might also consider using a time stamp other than <code>DateTime.Now</code>. What I typically do is start a <code>Stopwatch</code> when the program starts, and then use the <code>ElapsedMilliseconds</code> value. That avoids potential problems that occur during Daylight Saving Time changes, when the system automatically updates the clock (using NTP), or when the user changes the date/time.</p> <p>Whether the above solution works for you is going to depend on whether you can stand the memory hit of storing the strings.</p> <p>Added after "Additional information" was posted:</p> <p>If this will be accessed by multiple threads, I'd suggest using <code>ConcurrentDictionary</code> rather than <code>Hashset</code>, and <code>BlockingCollection</code> rather than <code>Queue</code>. Or, you could use <code>lock</code> to synchronize access to the non-concurrent data structures.</p> <p>If it's true that 99% of the strings will not be duplicate, then you'll almost certainly need an expiration queue that can remove things from the dictionary.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload