Caching Web Pages

You can use the DTW_CACHE_PAGE built-in function to cache a Web page. When Net.Data sees the DTW_CACHE_PAGE function in the macro, it contacts the Cache Manager and begins saving the HTML output for the macro in memory. After Net.Data successfully processes a macro, the HTML output is sent to the browser and the Cache Manager caches the output in one transaction as shown in Figure 28.

Figure 28. DTW_CACHE_PAGE Function Initiates Caching


Figure dtwa1109 not displayed.

Caching a Page

Specify Net.Data-generated pages to be written to the cache using the Net.Data DTW_CACHE_PAGE() built-in function.

The DTW_CACHE_PAGE() function caches all of the output from the macro following the function statement, once it determines that the page does not already exist in the cache or has expired. If the page does not exist in the cache or is older than the specified age, Net.Data sends the output page back to the browser, generates new output pages from the macro execution, and stores the page in the cache.

If the Cache Manager finds the cached page and it is still current, it displays the cache contents and Net.Data exits out of the macro. This behavior ensures that no unnecessary processing is done after the Web page has been retrieved from the cache.

Performance tip: Place DTW_CACHE_PAGE() as the first, or one of the first statements in a macro, to minimize the cost of executing the macro.

To cache a page:

  1. In the HTML block of a macro, prior to the HTML coding, insert the following function statement:
    @DTW_CACHE_PAGE("cache_id", cached_page_id, "age", status)
    

    Use this function to specify that Net.Data is to cache all HTML output from the macro that follows this statement. Place this statement early in the macro if you want to cache all HTML output.

    Parameters:

    cache_id
    A string identifying the cache in which the page will be placed. You can associate cache IDs with macros or groups of macros.

    cached_page_id
    A string containing an identifier used to identify the page in the cache in a subsequent @DTW_CACHE_PAGE cache request, for example the page's URL.

    age
    A string variable containing a length of time in seconds that specifies when a page is considered out-of-date. If the requested page has been in the cache for longer than the value of age, Net.Data executes the macro, regenerates the page, and caches the generated page, replacing the out-of-date page. If the requested page has been in the cache for less than or the same as the value of age, Net.Data retrieves the page from the cache and sends it to the browser. In this case, Net.Data ends macro execution immediately.

    status
    A string variable returned by Net.Data to indicate whether or not the page was cached successfully.

Example:

%HTML(cache_example) {
 %IF (customer == "Joe Smith")
 @DTW_CACHE_PAGE("mymacro.d2w", "http://www.mypage.org", "-1", status)
 %ENDIF
 ...
 <html>
 
<head>
 <:title>This is the page title</title>
 </head>
 
<body>
 <center>
 <h3>This is the Main Heading</h3>
 <p>It is $(time). Have a nice day!
 </body>
 
</html>
 %}

Advanced Caching: Determining Dynamically Whether to Cache

The DTW_CACHE_PAGE() function initiates caching from its location in the macro. Normally, you place the function at the beginning of the macro for better performance and to ensure that all HTML output is cached.

For advanced caching applications, you can place the DTW_CACHE_PAGE() function in the HTML output sections when you need to make the decision to cache at a specific point during processing, rather than at the beginning of the macro. For example, you might need to make the caching decision based on how many rows are returned from a query or function call.

Example: Places the function in the HTML block because the decision to cache depends on the expected size of the HTML output

% DEFINE { ...%}
 
...
 
%FUNCTION(DTW_SQL) count_rows(){
  select count(*) from customer
%REPORT{
 %ROW{
  @DTW_ASSIGN(ALL_ROWS, V1)
%}
%}
%}
 
%FUNCTION(DTW_SQL) all_customers(){
  select * from customer
%}
 
%HTML(OUTPUT) {
<html>
<head>
 <title>This is the customer list
 </head>
 <body>
 
@count_rows()
 
 %IF ($(ALL_ROWS) > "100")
 @DTW_CACHE_PAGE("mymacro.d2w", "http://www.mypage.org", "-1", status)
%ENDIF
 
@all_customers()
 
 </body>
 </html>
%}

In this example, the page is cached or retrieved based on the expected size of the HTML output. HTML output pages are considered cache-worthy only when the database table contains more than 100 rows. Net.Data always sends the text in the OUTPUT block, This is the customer list, to the browser after executing the macro; the text is never cached. The lines following the function call, @count_rows(), are cached or retrieved when the conditions of the IF block are satisfied. Together, both parts form a complete Net.Data output page.


[ Top of Page | Previous Page | Next Page | Index ]