Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Have you considered using <code>System.Web.Caching</code> instead of having to roll your own ?</p> <p><a href="http://www.hanselman.com/blog/UsingTheASPNETCacheOutsideOfASPNET.aspx" rel="nofollow noreferrer">http://www.hanselman.com/blog/UsingTheASPNETCacheOutsideOfASPNET.aspx</a></p> <p><strong>EDIT</strong></p> <p>Well the above should not add THAT much of an overhead to the system but have a look at this.</p> <p><strong>A few health warnings on the code below.</strong></p> <ul> <li>It's incomplete... see the <code>throw new NotImplementedException()</code>s at the bottom. I'll try and come back to it in a while as it's an interesting puzzle.</li> <li>You may want to cange the way expiration is done &amp; have overrides on the Add Methods to supply different values to the constructed value</li> <li>I've only tested it the bare minimum in a console app. see test code</li> <li>It also needs a bit of work around the TKey &amp; TValue Collections as they'll blindly return the entirety of the inner dictionary's collections without any expiration checking... if you don't need particularly granular expiration. You could add a system.timer to the class which periodically walked the entire collection and removed expired entries.</li> <li>If you look at the Definition for the BCL Dictionary you'll see it implements a hell of a lot of other interfaces to so depending on your requirements you may want to implement these as well. <code>IDictionary&lt;TKey, TValue&gt;, ICollection&lt;KeyValuePair&lt;TKey, TValue&gt;&gt;, IEnumerable&lt;KeyValuePair&lt;TKey, TValue&gt;&gt;, IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback</code></li> </ul> <p><strong>Test Code</strong></p> <pre><code>TimeSpan t = new TimeSpan(0,0,5); //5 Second Expiry ExpiringDictionary&lt;int, string&gt; dictionary = new ExpiringDictionary&lt;int,string&gt;(t); dictionary.Add(1, "Alice"); dictionary.Add(2, "Bob"); dictionary.Add(3, "Charlie"); //dictionary.Add(1, "Alice"); //&lt;&lt;this will throw a exception as normal... System.Threading.Thread.Sleep(6000); dictionary.Add(1, "Alice"); //&lt;&lt; this however should work fine as 6 seconds have passed </code></pre> <p><strong>Implementation</strong></p> <pre><code>public class ExpiringDictionary&lt;TKey, TValue&gt; : IDictionary&lt;TKey, TValue&gt; { private class ExpiringValueHolder&lt;T&gt; { public T Value { get; set; } public DateTime Expiry { get; private set; } public ExpiringValueHolder(T value, TimeSpan expiresAfter) { Value = value; Expiry = DateTime.Now.Add(expiresAfter); } public override string ToString() { return Value.ToString(); } public override int GetHashCode() { return Value.GetHashCode(); } }; private Dictionary&lt;TKey, ExpiringValueHolder&lt;TValue&gt;&gt; innerDictionary; private TimeSpan expiryTimeSpan; private void DestoryExpiredItems(TKey key) { if (innerDictionary.ContainsKey(key)) { var value = innerDictionary[key]; if (value.Expiry &lt; System.DateTime.Now) { //Expired, nuke it in the background and continue innerDictionary.Remove(key); } } } public ExpiringDictionary(TimeSpan expiresAfter) { expiryTimeSpan = expiresAfter; innerDictionary = new Dictionary&lt;TKey, ExpiringValueHolder&lt;TValue&gt;&gt;(); } public void Add(TKey key, TValue value) { DestoryExpiredItems(key); innerDictionary.Add(key, new ExpiringValueHolder&lt;TValue&gt;(value, expiryTimeSpan)); } public bool ContainsKey(TKey key) { DestoryExpiredItems(key); return innerDictionary.ContainsKey(key); } public bool Remove(TKey key) { DestoryExpiredItems(key); return innerDictionary.Remove(key); } public ICollection&lt;TKey&gt; Keys { get { return innerDictionary.Keys; } } public bool TryGetValue(TKey key, out TValue value) { bool returnval = false; DestoryExpiredItems(key); if (innerDictionary.ContainsKey(key)) { value = innerDictionary[key].Value; returnval = true; } else { value = default(TValue);} return returnval; } public ICollection&lt;TValue&gt; Values { get { return innerDictionary.Values.Select(vals =&gt; vals.Value).ToList(); } } public TValue this[TKey key] { get { DestoryExpiredItems(key); return innerDictionary[key].Value; } set { DestoryExpiredItems(key); innerDictionary[key] = new ExpiringValueHolder&lt;TValue&gt;(value, expiryTimeSpan); } } public void Add(KeyValuePair&lt;TKey, TValue&gt; item) { DestoryExpiredItems(item.Key); innerDictionary.Add(item.Key, new ExpiringValueHolder&lt;TValue&gt;(item.Value, expiryTimeSpan)); } public void Clear() { innerDictionary.Clear(); } public int Count { get { return innerDictionary.Count; } } public bool IsReadOnly { get { return false; } } public bool Contains(KeyValuePair&lt;TKey, TValue&gt; item) { throw new NotImplementedException(); } public void CopyTo(KeyValuePair&lt;TKey, TValue&gt;[] array, int arrayIndex) { throw new NotImplementedException(); } public bool Remove(KeyValuePair&lt;TKey, TValue&gt; item) { throw new NotImplementedException(); } public IEnumerator&lt;KeyValuePair&lt;TKey, TValue&gt;&gt; GetEnumerator() { throw new NotImplementedException(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw new NotImplementedException(); } } </code></pre>
    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.
    3. 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