APGen Documentation Previous Topic: ASPCache.Flush() Next Topic: CachedFile Object Parent Topic: ASPCache Object    ASPCache Object
ASPCache.WriteFile()
See Also:

Writes the contents of a file to the ASP Response object.  The cache is used to retrieve and store the file's contents.

Syntax

VBScript:

bWritten = Cache.WriteFile( sPath [, bMapPath ] [, bMonitorFileChanges ]
                            [, expireTime ] [, oResponse ] )

JScript:

bWritten = Cache.WriteFile( sPath [, bMapPath ] [, bMonitorFileChanges ]
                            [, expireTime ] [, oResponse ] );

Object

Cache An ASPCache object.

Parameters

sPath The string path of the file to be cached and written to the response stream. 

If bMapPath is True, this path is resolved using Server.MapPath().  If bMapPath is False, this should be a complete path.
bMapPath Optional Boolean parameter - the default is True.  If True, Server.MapPath() is called on sPath to resolve the path.
bMonitorFileChanges Optional Boolean parameter - the default is True.  If True, the cached contents are updated whenever the file on disk changes.  If False, a snapshot of the file is cached.

Monitoring for file changes is quite efficient in Windows 2000, but has a performance penalty under Windows NT 4.  For more information see below.
expireTime Optional integer parameter - the default is 0.  0 corresponds to no expiration.  A positive number sets the number of milliseconds the item will live in the cache.
oResponse Optional object parameter - the default is Nothing.  If an object is passed in, it will receive the contents of the file using its IStream or ASP Response interface.

Passing in the ASP Response object for this parameter makes the WriteFile() method slightly faster than if it is omitted.  If this parameter is not specified, the ASP ObjectContext is used to obtain the ASP Response object.

Return Value

bWritten True if the file was successfully written to the Response object.  False if the file could not be found, or if the Response object could not be written to.

Notes

The WriteFile() method enables easy and high-performance page fragment caching

WriteFile() uses the sPath parameter as a key to retrieve a CachedFile object from the cache.  If an object is not found, a new CachedFile object is created and added to the cache using sPath as the key.  Then CachedFile.Write() is called, which writes the file's contents to the Response object.

Parameters bMapPath, bMonitorFileChanges, and expireTime are only used when a new CachedFile object is created and added to the cache.  They do not affect behavior if a CachedFile object is retrieved from the cache.

Monitoring File Changes

When deciding whether to use True or False for the bMonitorFileChanges parameter, consider the following:

We generally recommend setting bMonitorFileChanges to True for Windows 2000 servers, unless the page fragment is not expected to change.  We recommend setting bMonitorFileChanges to False for Windows NT 4 servers.

Example

This example inserts a product page fragment in an ASP page.  Typically some error handling should be included, in case 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

To make WriteFile() slightly faster, pass in the ASP Response object:

bFragWritten = Cache.WriteFile("products/product" & pfid & ".htm", True, True, 0, Response)