APGen Documentation Previous Topic: Item Handles Next Topic: Flushing Parent Topic: Introduction to ASPCache    Introduction to ASPCache
Item Expiration
See Also:

ASPCache expiration allows developers to specify the duration of time an item will be held in the cache.  Each item in an ASPCache can be given an expiration period.  After the item has been in the cache for the expiration period, it is automatically removed.  The expiration period for an item can be extended or shortened at any time.

Expiration differs from flushing: Expiration sets a lifetime for individual items in the cache.  Expiration occurs whether or not an item is frequently used - it occurs after the expiration period has lapsed.  Flushing is a cache-wide cleanup of items that have not been used for some time.

Expiration is useful for specifying the amount of time the data should be considered valid.  Flushing is useful for cleaning up items that are not used enough to warrant keeping them in the cache.

Expiration periods are specified in milliseconds.  Because expiration periods are specified as a 32-bit long value, the maximum expiration period is approximately 24.8 days.  Expiration periods are internally managed as 64-bit integers to avoid date wrap-around bugs.

An item's TimeInCache value specifies how long the item has lived in the cache.  An item's Expiration value specifies how long the item has to live before it expires.

By default, items in the cache have expiration periods of 0.  An expiration period of 0 indicates that expiration is disabled for that item.

Item expiration can be specified when an item is added to the cache:

'  Expire this data in 10 minutes
bAdded = Cache.Add("NASDAQ", sCurNAS, 600000)

Expiration periods can also be read or written using the Expiration() and SetExpiration() methods:

' Retrieve the expiration period
expirationPeriod = Cache.Expiration("NASDAQ")

If (bAfterHours) Then
     ' Set the item to expire tomorrow morning at 8am
     expirationPeriod = DateDiff("s", Now, dtTomorrowMorning) * 1000
     Cache.SetExpiration("NASDAQ", expirationPeriod)
End If 

How Items Are Expired

Items are not removed from the cache exactly when they expire - removal may occur at any time after the item has expired.  An item cannot be accessed after it has expired - the expired item is considered invalid, and it is removed if any method attempts to access it.

Items that have expired are removed in one of two ways:

  1. When the background cleanup thread runs, any expired items are removed.  By default, the background cleanup thread does not run - FlushEnabled must be set to True for the cleanup thread to run.  The frequency of cleanup is specified by FlushInterval.  Since the cleanup thread is a lower priority thread, you can set the FlushInterval for frequent cleanup if prompt cleanup is important, and it won't slow down your ASP requests (though you may notice higher CPU usage, it only uses cycles that are unused by higher priority threads).
  2. If an expired item is retrieved, set, or otherwise accessed, and the cleanup thread has not yet removed the item, it is removed and the method behaves as if the item wasn't there.  In this case, the item is removed at the first access after it has expired.

Note that for background cleanup to occur, FlushEnabled must be set to True.