APGen Documentation Previous Topic: Introduction to ASPCache Next Topic: Item Handles Parent Topic: Introduction to ASPCache    Introduction to ASPCache
Collection Functionality
See Also:

ASPCache provides very high-performance collection functionality.  Data in the ASPCache object is shared across all pages in the ASP application, much like the ASP Application object.

The ASPCache object uses string keys.  Every item in the ASPCache must be stored under a string key.  String key comparison is case-insensitive, so Cache("key1") refers to the same item as Cache("Key1")

Assuming that an ASPCache object named "Cache" is instantiated in global.asa:

REM  *** global.asa ***

<OBJECT RUNAT=Server SCOPE=Application ID=Cache PROGID=ASPCache></OBJECT>

Data can be stored in the ASPCache object:

' Store a string in the Cache
Cache("key1") = "Value1"

' Store an object in the Cache
Dim oXmlDoc
Set oXmlDoc = Server.CreateObject("Microsoft.FreeThreadedXMLDOM")
' ...
Cache("xmlDoc") = oXmlDoc

Data can be retrieved from the ASPCache object:

' Retrieve a string from the Cache
Dim sValue
sValue = Cache("key1")

' Retrieve an object from the Cache
Set oXmlDoc = Cache("xmlDoc")

Data can be added to the ASPCache object:

' Add more data to the Cache
Dim bAdded
bAdded = Cache.Add("key2", "Value2")

The Add method will fail if the key is already present in the ASPCache object.

Data can be removed from the ASPCache object:

' Remove an item from the Cache
Dim bRemoved
bRemoved = Cache.Remove("key3")

Values stored in ASPCache are Variants, so they can be of any COM-supported type, including COM objects.  We recommend against storing Apartment-threaded and Multi-threaded objects in ASPCache - Both-threaded objects should be used.  Storing apartment-threaded objects is sub-optimal because access to apartment-threaded objects is serialized using a single thread.  Using multi-threaded objects is sub-optimal because access to multi-threaded objects uses a cross-apartment proxy.  Use both-threaded objects that aggregate the Free-Threaded Marshaler - this is optimal because there is no serialization or proxy.

Items in the cache can be accessed using the string key or an opaque handle.  Lookup by string key is fast - our algorithm is O(log n) and faster than hash tables. 

Handles allow O(1) access, which is the fastest possible access.  Items must be added using a unique string key, but items can be overwritten, retrieved, and removed using handles.  This code snippet uses a string key for the first lookup, then uses a handle for later reference to the same item in the cache:

' Retrieve a handle, then get info about a particular item in the Cache
Dim h, sKey, vValue, nHits, tInCache, tSinceLastUse

h = Cache.HandleOf("key1")      ' Get the handle for a particular item - O(log n)
vValue = Cache(h)               ' The Value for this item              - O(1)
nHits = Cache.Hits(h)           ' The # of hits on this item           - O(1)
tInCache = Cache.TimeInCache(h) ' How long this object has been in the cache - O(1)
tSinceLastUse = Cache.TimeSinceLastUse(h) ' How long since last use    - O(1)

For more information on handles, see Item Handles.

ASPCache Enumeration

The ASPCache object can be enumerated using VBScript For ... Each.  The ASPCache enumerator provides access to the handles of all objects in the collection, so item keys can be retrieved using the KeyOf method.

' Enumerate all items in the Cache
Dim h, sKey, vValue
For Each h in Cache
     sKey = Cache.KeyOf(h)
     If IsObject(Cache(h)) Then
          Set vValue = Cache(h)
     Else
          vValue = Cache(h)
     End If
Next

To enumerate the ASPCache object in JScript, use the JScript Enumerator object:

var e, h, sKey, vValue;
e = new Enumerator(Cache);
for (;!e.atEnd();e.moveNext())
     {
     h = e.item();
     sKey = Cache.KeyOf(h);
     vValue = Cache(h);
     }