APGen Documentation Previous Topic: Shrinking Next Topic: Using ASPCache in VB COM Components Parent Topic: Introduction to ASPCache    Introduction to ASPCache
Page Fragment Caching
See Also:

Page fragment caching enables dynamic insertion of static page fragments in dynamic web pages.  The performance benefits of using this technique are significant, typically resulting in throughput gains of 20 times (compared to generating the page fragment for every request) and reduced database load.  ASPCache™ has been highly optimized for page fragment caching.

Page fragment caching is designed for use with Active Page Generator™ (APGen™), where APGen is used to generate page fragments, and ASPCache is used to cache page fragments and write them to the ASP HTTP stream.  Prudent use of APGen and ASPCache will give you the best possible web site performance.

The WriteFile() method implements page fragment caching:

' Write frag.htm to the ASP Response
Dim bFragWritten
bFragWritten = Cache.WriteFile("frag.htm")

Typically some error handling will be required if the page fragment cannot be found:

' Display the page fragment for the specified product

Dim bFragWritten
bFragWritten = Cache.WriteFile("products/product" & pfid & ".htm")
If Not bFragWritten Then
     Call UnknownProduct( )
End If


Sub UnknownProduct( )
     Response.Write "<h3>Product cannot be found</h3>"
End Sub

Page fragments are stored in the cache using the file path as the key.  To remove a page fragment from the cache, use its path as before:

' Remove the product page fragment from the cache
Dim bRemoved
bRemoved = Cache.Remove("products/product" & pfid & ".htm")

Page fragment caching is implemented by storing a multi-threaded CachedFile object in the ASPCache.  This CachedFile object stores the contents of the page fragment file, and optionally monitors the page fragment file for changes.  This CachedFile object can be accessed in ASP code:

' This code snippet assumes that frag.htm was previously cached
' Retrieve the CachedFile object
Dim oCachedFile
Set oCachedFile = Cache("frag.htm")

'  Read the CachedFile object's properties
Dim sContents, bMonitoringFileChanges
sContents = oCachedFile.Contents
bMonitoringFileChanges = oCachedFile.MonitoringFileChanges

When Cache.WriteFile() is called, the page fragment is written to the ASP HTTP stream using highly optimized C++ code.  Using Cache.WriteFile() is far faster than using the FileSystemObject to load and write a page fragment.