Self-deleting hashtable entries
-
Hi! I have a hashtable and add values from time to time like this:
users.Add("Bob", 4);
users.Add("John", 10);Now I want each entry I add into this list to be removed after a certain amount of time, e.g. 10 minutes. So I add "Bob", and Bob will be autoamtically removed from the hashtable after 10 minutes, same for John. What is the best way to do this? regards
-
Hi! I have a hashtable and add values from time to time like this:
users.Add("Bob", 4);
users.Add("John", 10);Now I want each entry I add into this list to be removed after a certain amount of time, e.g. 10 minutes. So I add "Bob", and Bob will be autoamtically removed from the hashtable after 10 minutes, same for John. What is the best way to do this? regards
Run a clean-up function off a timer that scans the list for items older than 10 minutes, and deletes them. You'll have to keep track of how old an item is, of course. How you do that is really up to you, but I'd make the "value" portion of the table entry actually a class instance that holds your useful data plus a DateTime value.
-
Hi! I have a hashtable and add values from time to time like this:
users.Add("Bob", 4);
users.Add("John", 10);Now I want each entry I add into this list to be removed after a certain amount of time, e.g. 10 minutes. So I add "Bob", and Bob will be autoamtically removed from the hashtable after 10 minutes, same for John. What is the best way to do this? regards
I think the System.Web.Caching.Cache class is what are you looking for ... ;-) You can use it in WinForms application as shown below:
using System.Threading; using System.Web; using System.Web.Caching; namespace WindowsApplication8 { /// /// Cache for Windows Forms /// public sealed class WinCache { private static HttpRuntime _httpRuntime; public static Cache Cache { get { EnsureHttpRuntime(); return HttpRuntime.Cache; } } private static void EnsureHttpRuntime() { if( null == _httpRuntime ) { try { Monitor.Enter( typeof( WinCache ) ); if( null == _httpRuntime ) { // Create an Http context to give us access to the cache. _httpRuntime = new HttpRuntime(); } } finally { Monitor.Exit( typeof( WinCache ) ); } } } } }
Then you can easily insert the values into WinCache in following way:WinCache.Cache.Insert("Bob", 4, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero); WinCache.Cache.Insert("John", 10, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero);
DevIntelligence.com - My blog for .Net Developers -- modified at 17:31 Tuesday 24th January, 2006 -
I think the System.Web.Caching.Cache class is what are you looking for ... ;-) You can use it in WinForms application as shown below:
using System.Threading; using System.Web; using System.Web.Caching; namespace WindowsApplication8 { /// /// Cache for Windows Forms /// public sealed class WinCache { private static HttpRuntime _httpRuntime; public static Cache Cache { get { EnsureHttpRuntime(); return HttpRuntime.Cache; } } private static void EnsureHttpRuntime() { if( null == _httpRuntime ) { try { Monitor.Enter( typeof( WinCache ) ); if( null == _httpRuntime ) { // Create an Http context to give us access to the cache. _httpRuntime = new HttpRuntime(); } } finally { Monitor.Exit( typeof( WinCache ) ); } } } } }
Then you can easily insert the values into WinCache in following way:WinCache.Cache.Insert("Bob", 4, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero); WinCache.Cache.Insert("John", 10, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero);
DevIntelligence.com - My blog for .Net Developers -- modified at 17:31 Tuesday 24th January, 2006tarasn wrote:
I think the System.Web.Caching.Cache class is what are you looking for ... You can use it in WinForms application as shown below:
I'm not sure if you should be applauded for code reuse or censored for abusing existing code into a place where it has no business being. (mainly because I don't know anything about the web classes)
-
tarasn wrote:
I think the System.Web.Caching.Cache class is what are you looking for ... You can use it in WinForms application as shown below:
I'm not sure if you should be applauded for code reuse or censored for abusing existing code into a place where it has no business being. (mainly because I don't know anything about the web classes)
I don't see any problem with my answer .You can create Console application and check the code that I sent .Instead reinventing the wheel ( and wasting the time ) you can use well implemented class .I highly recommend you read more on MSDN about Cache class - probably it solves your .Net problem too . :-D DevIntelligence.com - My blog for .Net Developers
-
I think the System.Web.Caching.Cache class is what are you looking for ... ;-) You can use it in WinForms application as shown below:
using System.Threading; using System.Web; using System.Web.Caching; namespace WindowsApplication8 { /// /// Cache for Windows Forms /// public sealed class WinCache { private static HttpRuntime _httpRuntime; public static Cache Cache { get { EnsureHttpRuntime(); return HttpRuntime.Cache; } } private static void EnsureHttpRuntime() { if( null == _httpRuntime ) { try { Monitor.Enter( typeof( WinCache ) ); if( null == _httpRuntime ) { // Create an Http context to give us access to the cache. _httpRuntime = new HttpRuntime(); } } finally { Monitor.Exit( typeof( WinCache ) ); } } } } }
Then you can easily insert the values into WinCache in following way:WinCache.Cache.Insert("Bob", 4, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero); WinCache.Cache.Insert("John", 10, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero);
DevIntelligence.com - My blog for .Net Developers -- modified at 17:31 Tuesday 24th January, 2006 -
Run a clean-up function off a timer that scans the list for items older than 10 minutes, and deletes them. You'll have to keep track of how old an item is, of course. How you do that is really up to you, but I'd make the "value" portion of the table entry actually a class instance that holds your useful data plus a DateTime value.
-
This looks cool, but what's all the web stuff in there? I don't plan on using any http capabilities in my console application (and thus no WinForms). regards
The WinCache class can be used in console application. DevIntelligence.com - My blog for .Net Developers