Contained WithinFind More DocumentationFeatured Support Resources | Scarica il manuale in formato PDF (1494 KB)
Chapter 8 Dynamic Results Caching FunctionsThe functions described in this chapter enables you to write a results caching plug-in for Sun Java System Web Server. A results caching plug-in, which is a Service SAF, caches data, a page, or part of a page in the web server address space, which the Web Server can refresh periodically on demand. An Init SAF initializes the callback function that performs the refresh. This chapter has the following sections: About Results Caching Plug-insA results caching plug-in can generate a page for a request in three parts:
Without this feature, a plug-in would have to generate the whole page for every request unless an IFRAME is used, where the header or footer is sent in the first response along with an IFRAME pointing to the body. In this case, the browser must send another request for the IFRAME. If the body of a page has not changed, the plug-in needs to generate only the header and footer and to call the dr_net_write function instead of net_writewith the following arguments:
The Web Server constructs the whole page by fetching the body from the cache. If the cache has expired, the Web Server calls the refresh function and sends the refreshed page back to the client. An Init SAF that is visible to the plug-in creates the handle to the cache. The Init SAF must pass the following parameters to the dr_cache_init function:
Dynamic Result Cache FunctionsThis section describes the dynamic result cache functions. dr_cache_destroy() FunctionThe dr_cache_destroy function destroys and frees resources associated with a previously created and used cache handle. This handle cannot be used in subsequent calls to any of the above functions unless another dr_cache_init is performed. Syntax void dr_cache_destroy(DrHdl *hdl); ParametersDrHdl *hdl is a pointer to a previously initialized handle to a cache. For more information, see dr_cache_init() Function. Returnsvoid Exampledr_cache_destroy(&myHdl); dr_cache_init() FunctionThe dr_cache_init function creates a persistent handle to the cache, or NULL on failure. This function is called by an Init SAF. SyntaxPRInt32 dr_cache_init(DrHdl *hdl, RefreshFunc_t ref, FreeFunc_t fre,
CompareFunc_t cmp, PRUint32 maxEntries,
PRIntervalTime maxAge);
Returns1 if successful. 0 if an error occurs. ParametersThe following table describes parameters for the dr_cache_init function.
Exampleif(!dr_cache_init(&hdl, (RefreshFunc_t)FnRefresh, (FreeFunc_t)FnFree,
(CompareFunc_t)FnCompare, 150000, PR_SecondsToInterval(7200)))
{
ereport(LOG_FAILURE, "dr_cache_init() failed");
return(REQ_ABORTED);
}
dr_cache_refresh() FunctionThe dr_cache_refresh function provides a way to refresh a cache entry when the plug-in requires it. This refresh can be achieved by passing NULL for the ref parameter in dr_cache_init and by passing DR_CHECK in a dr_net_write call. If DR_CHECK is passed to dr_net_write and it returns with DR_EXPIR, the plug-in should generate new content in the entry and call dr_cache_refresh with that entry before calling dr_net_write again to send the response. You can use the plug-in to replace the cached entry even if it has not expired based on some other business logic. The dr_cache_refresh function is useful in this case. The plug-in then does the cache refresh management actively. SyntaxPRInt32 dr_cache_refresh(DrHdl hdl, const char *key,
PRUint32 klen, PRIntervalTime timeout,
Entry *entry, Request *rq, Session *sn);
Return Values1 if successful. 0 if an error occurs. Parameters
ExampleEntry entry;
char *key = "MOVIES"
GenNewMovieList(&entry.data, &entry.dataLen); // Implemented by
// plugin developer
if(!dr_cache_refresh(hdl, key, strlen(key), 0, &entry, rq, sn))
{
ereport(LOG_FAILURE, "dr_cache_refresh() failed");
return REQ_ABORTED;
}
dr_net_write() FunctionThe dr_net_write function sends a response back to the requestor after constructing the full page with hdr, the content of the cached entry as the body located using the key, and ftr. The hdr, ftr, or hdl can be NULL, but not all of them can be NULL. If hdl is NULL, no cache lookup is done. The caller must pass DR_NONE as the flag. By default, this function refreshes the cache entry if it has expired by making a call to the ref function passed to dr_cache_init. If no cache entry is found with the specified key, this function adds a new cache entry by calling the ref function before sending out the response. However, if the DR_CHECK flag is passed in the flags parameter and if either the cache entry has expired or the cache entry corresponding to the key does not exist, dr_net_write does not send any data out. Instead, the function returns with DR_EXPIR. If ref which is passed to dr_cache_init is NULL, the DR_CHECK flag is not passed in the flags parameter, and the cache entry corresponding to the key has expired or does not exist, then dr_net_write fails with DR_ERROR. However, dr_net_write refreshes the cache if ref is not NULL and DR_CHECK is not passed. If ref which is passed to dr_cache_init is NULL and the DR_CHECK flag is not passed but DR_IGNORE is passed and the entry is present in the cache, dr_net_write sends out the response even if the entry has expired. However, if the entry is not found, dr_net_write returns DR_ERROR. If ref which is passed to dr_cache_init is not NULL and the DR_CHECK flag is not passed but DR_IGNORE is passed and the entry is present in the cache, dr_net_write sends out the response even if the entry has expired. However, if the entry is not found, dr_net_write calls the ref function and stores the new entry returned from ref before sending out the response. SyntaxPRInt32 dr_net_write(DrHdl hdl, const char *key, PRUint32 klen, const char *hdr,
const char *ftr, PRUint32 hlen, PRUint32 flen,
PRIntervalTime timeout, PRUint32 flags,
Request *rq, Session *sn);
Return ValuesIO_OKAY if successful. IO_ERROR if an error occurs. DR_ERROR if an error in cache handling occurs. DR_EXPIR if the cache has expired. ParametersThe following table describes parameters for the dr_net_write function.
FlagsThis section describes the flags for dr_net_write function.
Exampleif(dr_net_write(Dr, szFileName, iLenK, NULL, NULL, 0, 0, 0,
DR_CNTLEN | DR_PROTO, rq, sn) == IO_ERROR)
{
return(REQ_EXIT);
}
fc_open() FunctionThe fc_open function returns a pointer to PRFileDesc that refers to an open file (fileName). The fileName must be the full path name of an existing file. The file is opened in read mode only. The application calling this function should not modify the currency of the file pointed to by the PRFileDesc * unless the DUP_FILE_DESC is also passed to this function. In other words, the application at minimum should not issue a read operation based on this pointer that would modify the currency for the PRFileDesc *. If a read operation is required, that might change the currency for the PRFileDesc *, then the application should call this function with the argument DUP_FILE_DESC. On a successful call to this function, a valid pointer to PRFileDesc is returned and the handle FcHdl is properly initialized. The size information for the file is stored in the fileSize member of the handle. SyntaxPRFileDesc *fc_open(const char *fileName, FcHdl *hDl,
PRUint32 flags, Session *sn, Request *rq);
Return ValuesPointer to PRFileDesc, or NULL on failure. Parametersconst char *fileName is the full path name of the file to be opened. FcHdl*hDl is a valid pointer to a structure of type FcHdl. PRUint32 flags can be 0 or DUP_FILE_DESC. Session *sn is a pointer to the session. Request *rq is a pointer to the request. fc_close() FunctionThe fc_close function closes a file opened using fc_open. This function should only be called with files opened using fc_open. Syntaxvoid fc_close(PRFileDesc *fd, FcHdl *hDl; Return Valuesvoid ParametersPRFileDesc *fd is a valid pointer returned from a prior call to fc_open. FcHdl *hDl is a valid pointer to a structure of type FcHdl. This pointer must have been initialized by a prior call to fc_open. fc_net_write() FunctionUse fc_net_write function is to send a header or a footer and a file that exists somewhere in the system. The fileName should be the full path name of a file. SyntaxPRInt32 fc_net_write(const char *fileName, const char *hdr,
const char *ftr, PRUint32 hlen,
PRUint32 flen, PRUint32 flags,
PRIntervalTime timeout, Session *sn, Request *rq);
Return ValuesIO_OKAY if successful. IO_ERROR if an error occurs. FC_ERROR if an error in file handling occurs. Parameters
FlagsThis section describes the flags for fc_net_write() function.
Exampleconst char *fileName = "/docs/myads/file1.ad";
char *hdr = GenHdr(); // Implemented by plugin
char *ftr = GenFtr(); // Implemented by plugin
if(fc_net_write(fileName, hdr, ftr, strlen(hdr), strlen(ftr),
FC_CNTLEN, PR_INTERVAL_NO_TIMEOUT, sn, rq) != IO_OKEY)
{
ereport(LOG_FAILURE, "fc_net_write() failed");
return REQ_ABORTED;
}
|