Forum Discussion
dhoard
11 years agoVisitor
I decided to do the purge on the put in the cache (video playback). O(n) to purge the oldest entry, which could only occur when putting something new in the cache. For my usage in a the playback notification message loop, the purge would happen on the first playback notification message.
Anyone see any issues with the performance of this code ?
Anyone see any issues with the performance of this code ?
'
' Object to implement a persistent cache of a maximum size
'
Function PersistentCache(p_name As String, p_size As Integer) As Object
this = {
name : p_name
size : p_size
registrySectionData : CreateObject("roRegistrySection", p_name + ".data")
registrySectionTimestamp : CreateObject("roRegistrySection", p_name + ".timestamp")
Put: PersistentCache_Put
ContainsKey: PersistentCache_ContainsKey
Get: PersistentCache_Get
Remove: PersistentCache_Remove
Clear: PersistentCache_Clear
}
return this
End Function
Sub PersistentCache_Put(p_key As String, p_value As String)
m.registrySectionData.Write(p_key, p_value)
m.registrySectionTimestamp.Write(p_key, CreateObject("roDateTime").AsSeconds().ToStr())
If (m.registrySectionData.GetKeyList().Count() > m.size) Then
oldestKey = m.registrySectionData.GetKeyList()[0]
oldestTimestamp = m.registrySectionTimestamp.Read(oldestKey).ToInt()
For Each key In m.registrySectionData.GetKeyList()
tempTimestamp = m.registrySectionTimestamp.Read(key).ToInt()
If (tempTimestamp < oldestTimestamp) Then
oldestKey = key
oldestTimestamp = tempTimestamp
End If
End For
m.registrySectionData.Delete(oldestKey)
m.registrySectionTimestamp.Delete(oldestKey)
End If
m.registrySectionData.Flush()
m.registrySectionTimestamp.Flush()
End Sub
Function PersistentCache_ContainsKey(p_key As String) As Boolean
return m.registrySectionData.Exists(p_key)
End Function
Function PersistentCache_Get(p_key As String) As Object
If (m.registrySectionData.Exists(p_key)) Then
return m.registrySectionData.Read(p_key)
Else
return Invalid
End If
End Function
Sub PersistentCache_Remove(p_key As String)
m.registrySectionData.Delete(p_key)
m.registrySectionTimestamp.Delete(p_key)
m.registrySectionData.Flush()
m.registrySectionTimestamp.Flush()
End Sub
Sub PersistentCache_Clear()
For Each key In m.registrySectionData.GetKeyList()
m.registrySectionData.Delete(key)
m.registrySectionTimestamp.Delete(key)
End For
m.registrySectionData.Flush()
m.registrySectionTimestamp.Flush()
End Sub