Internet Technologies:cxp

Caching dynamic content Foundation

CXP supports caching of the content that is generated. You can cache the page either on the client-side (browser) making the request, or by making use of CXP server-side caching. Caching offers you a powerful way for improving the performance of your web applications. Caching makes it possible to satisfy subsequent requests for a page directly from the cache, so that the code that originally created the page content does not have to be executed again.

Client-side caching is controlled via the :httpResponse:clientCachePolicy member variable. Use the method :disable()of the object stored in this member to disable client-side caching, or use :enable( 3600 ) for caching your content for a certain timeframe. In this example, the content will be cached for 3600 seconds or one hour.

Server-side caching is more flexible and allows you to specify various conditions under which the cached data is invalidated and the creation of new content is enforced by again executing the CXP page. The CXP core provides a set of predefined caching policies as outlined below.

CXP caching policies
Provider Description
CxpCacheTimeoutByRequests The cache is invalidated after a certain number of requests
CxpCacheTimeoutByInterval Cache invalidation occurs after a certain period of time
CxpCacheTimeoutByFileChanged The cache gets invalidated as soon as one or more referenced files are changed

In order to use a caching provider for your CXP page, simply add a provider configuration to the .config file of your CXP page. If you need to cache all files in a specific directory, add the configuration to the application.config file instead. The following .config file configures the "timeout by request" provider so that three page requests are delivered from the cache. Every fourth request leads to a CXP page execution followed by another three requests delivered from cache.

<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?> 
<config> 
<cachepolicy 
  provider="CxpCacheTimeoutByRequests" 
  requests ="3" /> 
</config> 

The following .config file demonstrates configuration of the "timeout by interval" provider. In this case, we set up an interval of five seconds. All subsequent requests for a cached CXP page occuring within this timeframe are delivered from the cache. Once the timeout elapses, the cache is reset and the page content is recreated upon the next request.

<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?> 
<config> 
<cachepolicy 
  provider="CxpCacheTimeoutByInterval" 
  interval ="5" /> 
</config> 

The CxpCacheTimeoutByInterval provider is great to avoid race conditions for pages which are under heavy load. An example would be a page which is requested multiple times per second, but whose content generation takes hundreds of milliseconds.

The final example demonstrates using the "timeout by file change" provider. For this, we need to specify one or more files on the file system. In the example below, we reference a local file in the root of our c: drive as well as a file on a remote NAS. Keep in mind that your cxp-worker process needs the proper rights for accessing the file location(s). The first time the CXP page is executed, the caching provider ensures that the content produced by CXP is cached until the last update date/time of one of the referenced files changes.

<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?> 
<config> 
<cachepolicy 
  provider="CxpCacheTimeoutByFileChanged" 
  file ="c:\newsletter.txt" 
  file ="\\nas\shared\global-weather-warning.txt" /> 
</config> 

Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.