''' <summary>
''' This class implements a list with keys that expire
''' after a certain amount of time.
''' </summary>
''' <remarks>The timed list holds keys to items stored elsewhere.</remarks>
Public Class TimedList
''' <summary>
''' Event raised when a key has expired.
''' </summary>
''' <param name="sender"></param>
''' <param name="itemKey"></param>
''' <remarks></remarks>
Public Event ItemExpired(ByVal sender As TimedList, ByVal itemKey As String)
''' <summary>
''' List with items.
''' </summary>
''' <remarks></remarks>
Protected m_list As SortedList(Of String, TimedItem)
''' <summary>
''' Our pulse timer.
''' </summary>
''' <remarks></remarks>
Protected WithEvents m_timer As System.Timers.Timer
''' <summary>
''' Lock object.
''' </summary>
''' <remarks></remarks>
Protected Shared m_lock As New Queue
''' <summary>
''' Default class constructor.
''' </summary>
''' <remarks></remarks>
Public Sub New()
m_list = New SortedList(Of String, TimedItem)
End Sub
''' <summary>
''' Starts our timer.
''' </summary>
''' <param name="sweepInterval">Sweep interval in seconds.</param>
''' <remarks></remarks>
Public Sub StartTimer(ByVal sweepInterval As Integer)
m_timer = New Timers.Timer(sweepInterval * 1000)
m_timer.AutoReset = True
m_timer.Start()
End Sub
''' <summary>
''' Stops the timer.
''' </summary>
''' <remarks></remarks>
Public Sub StopTimer()
SyncLock queue.Synchronized(m_lock)
m_timer.Stop()
m_timer.Dispose()
m_timer = Nothing
End SyncLock
End Sub
''' <summary>
''' Adds a new key to the timed list.
''' </summary>
''' <param name="key">Key to add.</param>
''' <param name="expirySeconds">Seconds after which the item expires.</param>
''' <remarks></remarks>
Public Sub AddItem(ByVal key As String, ByVal expirySeconds As Integer)
SyncLock queue.Synchronized(m_lock)
m_list.Add(key, New TimedItem(key, Now.AddSeconds(expirySeconds)))
End SyncLock
End Sub
''' <summary>
''' Removes a key from the list.
''' </summary>
''' <param name="key"></param>
''' <remarks></remarks>
Public Sub RemoveItem(ByVal key As String)
SyncLock queue.Synchronized(m_lock)
m_list.Remove(key)
End SyncLock
End Sub
''' <summary>
''' Renews a keys' lifetime.
''' </summary>
''' <param name="key"></param>
''' <param name="expirySeconds"></param>
''' <remarks></remarks>
Public Sub RenewItem(ByVal key As String, ByVal expirySeconds As Integer)
SyncLock queue.Synchronized(m_lock)
If m_list.ContainsKey(key) = False Then
Throw New Exception("Item with key " + key + " was not found")
End If
m_list(key).Expiry = Now.AddSeconds(expirySeconds)
End SyncLock
End Sub
''' <summary>
''' Clears all items in the list.
''' </summary>
''' <remarks></remarks>
Public Sub Cleanup()
SyncLock queue.Synchronized(m_lock)
m_list.Clear()
End SyncLock
End Sub
''' <summary>
''' Called when the timer elapses.
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub m_timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles m_timer.Elapsed
m_timer.Stop()
Dim remList As New List(Of String), en As IEnumerator(Of KeyValuePair(Of String, TimedItem)) = Nothing
SyncLock m_lock.SyncRoot
Try
en = m_list.GetEnumerator()
en.Reset()
While en.MoveNext
If en.Current.Value.HasExpired Then
Try
RaiseEvent ItemExpired(Me, en.Current.Key)
Catch ex As Exception
Debug.WriteLine("An exception has occurred while notifying caller that an item has expired (" + ex.ToString + ")")
End Try
remList.Add(en.Current.Key)
End If
End While
Catch ex As Exception
Debug.WriteLine("+=====================================================================+")
Debug.WriteLine("TimedList exception")
Debug.WriteLine(ex.ToString)
Debug.WriteLine("+=====================================================================+")
Finally
If en IsNot Nothing Then
en.Dispose()
en = Nothing
End If
For Each key As String In remList
m_list.Remove(key)
Next
remList = Nothing
End Try
End SyncLock
m_timer.Start()
End Sub
End Class
''' <summary>
''' This class is used to represent a timed item.
''' </summary>
''' <remarks></remarks>
Public Class TimedItem
Implements IComparable(Of TimedItem)
''' <summary>
''' Item key.
''' </summary>
''' <remarks></remarks>
Protected m_key As String
''' <summary>
''' Item expiry.
''' </summary>
''' <remarks></remarks>
Protected m_expiry As DateTime
''' <summary>
''' Get/set the item key.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property Key() As String
Get
Return m_key
End Get
Set(ByVal value As String)
m_key = value
End Set
End Property
''' <summary>
''' Get/set the item expiry.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property Expiry() As DateTime
Get
Return m_expiry
End Get
Set(ByVal value As DateTime)
m_expiry = value
End Set
End Property
''' <summary>
''' Default class constructor.
''' </summary>
''' <param name="key">Item key.</param>
''' <param name="expiry">Item expiry.</param>
''' <remarks></remarks>
Public Sub New(ByVal key As String, ByVal expiry As DateTime)
Me.Key = key
Me.Expiry = expiry
End Sub
''' <summary>
''' Returns True if the item has expired, False otherwise.
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Function HasExpired() As Boolean
Return (DateTime.Compare(Now, m_expiry) >= 0)
End Function
''' <summary>
''' Implemented to allow for sort/search operations.
''' </summary>
''' <param name="other">Other instance of TimedItem.</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function CompareTo(ByVal other As TimedItem) As Integer Implements System.IComparable(Of TimedItem).CompareTo
Return String.Compare(Me.Key, other.Key)
End Function
End Class