Robert Coleridge
Microsoft Developer Network
August 1999
The d4cache COM component for Phase 4 contains one class: Cache. This reference contains three sections: methods, properties, and enumerated values. For further details on the design and implementation details of the object, see my article "Creating a Page Cache Object."
Method | Description |
Add | Add an item (key, value pair) to the cache. |
Clear | Clear all items from the cache. |
CreateGeneralIndex | Used to create a general index based on specified values. |
EnumHitRange | Specify the range used when enumerating based on hit counts. |
Flush | Flush old items from the cache. |
Initialize | Initialize the cache to a specified size. |
Remove | Remove a specified item from the cache. |
Property | Description |
CacheEnabled | A flag determining whether caching can occur. |
Count | The number of items in the cache. |
EnumBy | An enumerated value determining what sort order is used to enumerate through the cache. |
EnumType | An enumerated value determining what information is returned when enumerating through the cache. |
Err | The last known error value. |
EventsEnabled | A flag determining whether events will be fired. |
FlushEnabled | A flag determining whether flushing can occur. |
FlushDeltaTime | The time by which items are flushed from the cache. |
Hits | The hit count belonging to a particular cached item, specified by the item's key value. |
HitsOrd | The hit count belonging to a particular cached item, specified by the item's index value. |
Key | The key associated with a particular cached item. |
LastIndex | The index of the last access cache item. |
Value | The value or data belonging to a particular cached item, specified by the item's key value. |
ValueOrd | The value or data belonging to a particular cached item, specified by the item's index value. |
RaiseErrors | A flag determining whether COM errors can be raised. |
EnumSorted | An enumerated value determining what sort order is used to enumerate through the cache. |
Enumerated type | Description |
ENUMBY | A list of values used to determine the sort order of the cache. |
ENUMTYPES | A list of values used to determine the information returned during enumeration of the cache. |
SORTON | A list of values used to determine the key used in the general index sort. |
Events | Description |
CacheClearing | Fired when the cache is being cleared. |
CacheCleared | Fired when a clear ends. |
CacheExtending | Fired when the cache is being extended. |
CacheExtended | Fired when a cache extension is finished. |
CacheFlushing | Fired when a flush begins. |
CacheFlushed | Fired when a flush ends. |
CacheSorting | Fired when the cache is being sorted. |
CacheSorted | Fired when a sort ends. |
ItemAdded | Fired when an item is added to the cache. |
Adds an item and its associated unique key to the cache.
object.Add Key,Value
or
Boolean = object.Add(Key, Value)
Key: Associated unique key for item.
Value: String to be stored.
Dim oCache as new MSDNContainer.D4Cache
If oCache.Initialize(2000) then
oCache.Add "Key", "This is the item data"
End if
This function returns a Boolean flag depicting success or failure.
Removes a specified item from the cache.
object.Remove sKey
or
Boolean = object.Remove(sKey)
Key: Associated unique key for item to be removed.
Dim oCache as new MSDNContainer.D4Cache
If oCache.Initialize(2000) then
'
cache values added
oCache.Remove "Key"
End if
This function returns a Boolean flag depicting success or failure.
Clears out the internal cache.
object.Clear
or
Boolean = object.Clear()
None
Dim oCache as new MSDNContainer.D4Cache
If oCache.Initialize(2000) then
'
cache values added / manipulated
oCache.Clear
End if
This function returns a Boolean flag depicting success or failure.
Initializes the internal structures of the cache.
object.Initialize SizeOfCache[, GrowBy]
or
Boolean = object.Initialize(SizeOfCache[, GrowBy])
SizeOfCache: The initial number of items that can be stored in the cache.
GrowBy: The number of elements the cache is to grow each time it fills up.
Dim oCache as new MSDNContainer.D4Cache
If oCache.Initialize(2000) then
'
cache values added
End if
This function returns a Boolean flag depicting success or failure.
Flushes out the cache according to preset parameters. Similar to the Clear method but removes only those items that exceed the preset parameters, such as tie in cache.
object.Flush
or
Boolean = object.Flush()
None
Dim oCache as new MSDNContainer.D4Cache
If oCache.Initialize(2000) then
'
cache values added
oCache.Flush
End if
This function returns a Boolean flag depicting success or failure.
Creates a generalized index based on specified values.
object.CreateGeneralIndex(SortOn)
or
Boolean = object.CreateGeneralIndex(SortOn)
SortOn: The sort parameter based on SORTON values.
Dim oCache as new MSDNContainer.D4Cache
If oCache.Initialize(2000) then
'
cache values added
oCache.CreateGeneralIndex(icSORT_KEY)
End if
This function returns a Boolean flag depicting success or failure.
Returns the unique key associated with the specified index in the cache.
sKey = object.Key(n)
Dim oCache as new MSDNContainer.D4Cache
If oCache.Initialize(2000) then
'
cache values added
Debug.Print oCache.Key(1)
End if
Returns the value associated with the specified key.
Value = object.Item(sKey)
or
Value = object(sKey)
Dim oCache as new MSDNContainer.D4Cache
If oCache.Initialize(2000) then
'
cache values added
Debug.Print oCache.Value("Key")
' or using the default method
Debug.Print oCache("Key")
End if
Returns the value associated with the specified index.
Value = object.Value(lIndex)
Dim oCache as new MSDNContainer.D4Cache
If oCache.Initialize(2000) then
'
cache values added
Debug.Print oCache.ValueOrd(123)
End if
This function returns the same data as the Value property except that the data is accessed ordinally rather than by the associated key.
Returns the hit count associated with the specified key.
Value = object.Hits(sKey)
Dim oCache as new MSDNContainer.D4Cache
If oCache.Initialize(2000) then
'
cache values added
Debug.Print oCache.Hits("Key")
End if
Returns the hit count associated with the specified index.
Value = object.HitsOrd(lIndex)
Dim oCache as new MSDNContainer.D4Cache
If oCache.Initialize(2000) then
'
cache values added
Debug.Print oCache.HitsOrd(123)
End if
This function returns the same hit count as the Hit property except that the data is accessed ordinally rather than by the associated key.
Returns the number of items in the cache.
n = object.Count
Dim oCache as new MSDNContainer.D4Cache
If oCache.Initialize(2000) then
'
cache values added
Debug.Print "There are " & oCache.Count & " objects in the cache"
End if
Sets or resets the flag that determines the type of error traps raised.
flag = object.RaiseErrors
or
object.RaiseErrors boolean
Dim oCache as new MSDNContainer.D4Cache
If oCache.Initialize(2000) then
oCache,RaiseErrors = False
oCache.Add "Key", "This is the item data"
'add again to demonstrate
oCache.Add "Key", "This is the item data"
'no error raised since turned off therefore must check err value
If oCache.Err then
Debug.Print "A duplicate key error would have occurred"
End if
oCache,RaiseErrors = True
On Error Resume Next
oCache.Add "Key", "This is the item data"
'add again to demonstrate
oCache.Add "Key", "This is the item data"
'an error would be raised and must be trapped for
If Err then
Debug.Print "A duplicate key error would have occurred"
End if
End if
Set this property to true when the cache is used in a scripting language to prevent raising of untrappable errors.
Sets or obtains the flush delta time-out.
n = object.FlushDeltaTime
or
object.FlushDeltaTime = n
Dim oCache as new MSDNContainer.D4Cache
oCache.FlushDeltaTime = 20 ' 20 seconds
If oCache.Initialize(2000) then
'
cache values added
'do some lengthy work that exceeds the specified timeout . . .
oCache.Add "new key", "Item data"
'When the preceding add took place the the cache
'would have been flushed of any data older than 20 seconds
End if
The time is given in milliseconds.
Sets or obtains the sort order for the enumeration done by the _NewEnum interface.
n = object.EnumSorted
or
object. EnumSorted = n
Dim oCache as new MSDNContainer.D4Cache
Dim v As Variant
oCache.EnumSorted = icENUM_DESCENDING_ACCESS_TIME
If oCache.Initialize(2000) then
'
cache values added
'The following will iterate through the cache
'going from oldest to newest
For Each v In oCache
Debug.Print v
Next v
End if
This property utilizes the ENUMBY enumerated constants, which are defined as follows:
icENUM_NONSORTED: The data returned from the _NewEnum interface is returned in an implementation-derived sequence and therefore cannot be guaranteed to be in any order.
icENUM_ASCENDING_KEY: The data returned from the _NewEnum interface is sorted in an ascending sequence based on the keys associated with each item.
icENUM_DESCENDING_KEY: The data returned from the _NewEnum interface is sorted in a descending sequence based on the keys associated with each item.
icENUM_ASCENDING_INDEX: The data returned from the _NewEnum interface is returned in a forward sequence and is simply the index values of the actual key index.
icENUM_DESCENDING_INDEX: The data returned from the _NewEnum interface is returned in a reverse sequence and is simply the index values of the actual key index.
icENUM_ASCENDING_ACCESS_TIME: The data returned from the _NewEnum interface is sorted in an ascending sequence based on the access time of each item.
icENUM_DESCENDING_ACCESS_TIME: The data returned from the _NewEnum interface is sorted in a descending sequence based on the access time of each item.
icENUM_GI_ASCENDING_HITCOUNT: The data returned from the _NewEnum interface is sorted in the general index in ascending sequence based on the hit count of each item.
icENUM_GI_DESCENDING_HITCOUNT: The data returned from the _NewEnum interface is sorted in the general index in descending sequence based on the hit count of each item.
Obtains the index ID for the last item accessed.
n = object.LastIndex
Dim oCache as new MSDNContainer.D4Cache
Dim v As Variant
oCache.EnumSorted = icENUM_DESCENDING_ACCESS_TIME
If oCache.Initialize(2000) then
'
cache values added
'The following will iterate through the cache
'going from oldest to newest
For Each v In oCache
Debug.Print v, oCache.LastIndex
Next v
End if
Sets or obtains the enumeration type for the _NewEnum interface.
n = object.EnumType
or
object. EnumType = n
Dim oCache as new MSDNContainer.D4Cache
Dim v As Variant
oCache.EnumSorted = icENUM_ASCENDING_ACCESS_TIME
oCache.EnumType = icENUM_VALUE
If oCache.Initialize(2000) then
'
cache values added
'The following will iterate through the cache
'and retreive the associated value
For Each v In oCache
Debug.Print v
Next v
End if
This property utilizes the ENUMTYPES enumerated constants, which are defined as follows:
icENUM_HITCOUNT: The data returned from the _NewEnum interface is the hit count for the relevant item.
icENUM_KEY: The data returned from the _NewEnum interface is the key that was stored with the Add method.
icENUM_VALUE: The data returned from the _NewEnum interface is the value or data that was stored with the Add method.
Obtains the value of the last known error.
n = object.Err
Dim oCache as new MSDNContainer.D4Cache
If oCache.Initialize(2000) then
oCache,RaiseErrors = False
oCache.Add "Key", "This is the item data"
'add again to demonstrate
oCache.Add "Key", "This is the item data"
'no error raised since turned off therefore must check err value
If oCache.Err then
Debug.Print "A duplicate key error would have occurred"
End if
oCache,RaiseErrors = True
On Error Resume Next
oCache.Add "Key", "This is the item data"
'add again to demonstrate
oCache.Add "Key", "This is the item data"
'an error would be raised and must be trapped for
If Err then
Debug.Print "A duplicate key error would have occurred"
End if
End if
This value is reset to 0 upon entry to all methods and relevant properties. The value contained, if non-zero, indicates an error.
Sets or obtains a flag that determines whether flushing will occur.
n = object.FlushEnabled
or
object.FlushEnabled = n
Dim oCache as new MSDNContainer.D4Cache
oCache.FlushDeltaTime = 20 ' 20 seconds
oCache.FlushEnabled = False
If oCache.Initialize(2000) then
'
cache values added
'do some lengthy work that exceeds the specified timeout . . .
oCache.Add "new key", "Item data"
'When the preceding add took place the the cache
'would have been flushed of any data older than 20 seconds
'but since flushing was disabled all data reamins in the cache.
End if
One of the uses for this property is in front-end loading of the cache. With this property it is possible to load the cache before it is required and then turn flushing back on, because flushing only takes place on cache addition.
Sets or obtains a flag that determines whether caching will occur.
n = object.CacheEnabled
or
object.CacheEnabled = n
Setting this flag to false will cause all subsequent calls to the Add method to fail until caching is turned back on.
Sets or obtains a flag that determines whether events are fired.
n = object.EventsEnabled
or
object.EventsEnabled = n
Dim oCache as new MSDNContainer.D4Cache
If oCache.Initialize(2000) then
'
cache values added
Debug.Print "There are " & oCache.Count & " objects in the cache"
Else
Debug.print oCache.Err
End if
The internal value is reset on each method call to the object.
This type is used for determining how enumerated data will be returned when using the IEnumVariant interface returned via the _NewEnum method. The following constants are defined:
icENUM_NONSORTED: The data returned from the _NewEnum interface is returned in an implementation-derived sequence and therefore cannot be guaranteed to be in any order.
icENUM_ASCENDING_KEY: The data returned from the _NewEnum interface is sorted in an ascending sequence based on the keys associated with each item.
icENUM_DESCENDING_KEY: The data returned from the _NewEnum interface is sorted in a descending sequence based on the keys associated with each item.
icENUM_ASCENDING_ACCESS_TIME: The data returned from the _NewEnum interface is sorted in an ascending sequence based on the access time of each item.
icENUM_DESCENDING_ACCESS_TIME: The data returned from the _NewEnum interface is sorted in a descending sequence based on the access time of each item.
icENUM_ASCENDING_INDEX: The data returned from the _NewEnum interface is sorted in an ascending sequence based on the keys associated with each item. This data is simply the index number into the array.
icENUM_DESCENDING_INDEX: The data returned from the _NewEnum interface is sorted in a descending sequence based on the keys associated with each item. This data is simply the index number into the array.
icENUM_ASCENDING_HITCOUNT: The data returned from the _NewEnum interface is sorted in an ascending sequence based on the hit counts for an item.
icENUM_DESCENDING_HITCOUNT: The data returned from the _NewEnum interface is sorted in a descending sequence based on the hit counts for an item.
This type is used for determining what type of data, whether key or value, will be returned when using the IEnumVariant interface returned via the _NewEnum method. The following constants are defined:
icENUM_VALUE: The data returned from the _NewEnum interface is the value or data that was stored with the Add method.
icENUM_KEY: The data returned from the _NewEnum interface is the key that was stored with the Add method.
icENUM_HITCOUNT: The data returned from the _NewEnum interface is the hit count for the specified item.
This type is used for determining what data is used to sort the general index.
icSORT_TIME: Sort based on the access time associated with an item.
icSORT_KEY: Sort based on the key associated with an item.
icSORT_HITCOUNT: Sort based on the hit count associated with an item.
Fired when an item is added to the cache.
Private Sub object_ItemAdded(ByVal NewItemIndex As Long)
NewItemIndex: Index of item just added.
This event is fired after the item has been added to the cache.
Fired when the cache is about to be flushed.
Private Sub object_CacheFlushing()
Fired when the cache has finished flushing.
Private Sub object_CacheFlushed()
Fired when the cache is about to be extended.
Private Sub object_CacheExtending()
Fired when the cache has finished extending.
Private Sub object_CacheExtended()
Fired when the cache is about to be cleared.
Private Sub object_CacheClearing()
Fired when the cache has finished clearing.
Private Sub object_CacheCleared()
Fired when the cache is about to be sorted.
Private Sub object_CacheSorting()
Fired when the cache has finished sorting.
Private Sub object_CacheSorted()
Comments? We welcome your feedback at duwamish@microsoft.com.