APGen Documentation Previous Topic: Flushing Next Topic: Page Fragment Caching Parent Topic: Introduction to ASPCache    Introduction to ASPCache
Shrinking
See Also:

ASPCache shrinking ensures that only the most frequently used MaxSize items remain in the cache.  Less-frequently used items are removed until the cache reaches its desired size.  Shrinking is a method for ensuring that the cache doesn't hold on to too many resources.

Like flushing, shrinking removes the items that are least used.  This frees memory and other valuable resources, while retaining the items that are frequently used and thus more valuable.

Like expiration and flushing, shrinking occurs on a background thread.  Unlike expiration and flushing, shrinking temporarily bumps up the background scavenging thread to normal priority so the shrinking operation will occur promptly, even while web pages are being served.  We decided on this behavior after much stress testing - this behavior prevents overloaded web servers from caching too many objects and paging (provided the MaxSize setting is appropriate).

By default, shrinking is disabled.  This corresponds to a MaxSize of 0.  To enable shrinking, set the MaxSize:

' Limit cache to 500 items
Cache.MaxSize = 500

This example adds 10000 items to the cache, then reduces the MaxSize to cause a shrinking operation:

' Disable shrinking while adding these items
Cache.MaxSize = 0

' Add 10000 items to the cache
Dim sKey, value, i
For i= 1 To 10000
     sKey = "key" & i
     value = "value" & i
     
     ' Add the new Key-Value pair
     Cache.Add sKey, value
Next

' Set the MaxSize to 500
' This will initiate a shrinking operation on the background thread
Cache.MaxSize = 500