DynamicXmlBuilder Cache


Caching DynamicXmlBuilder

The DynamicXmlBuilder allows for caching an Xml response in a file saved on the web server. For busy web services, this can provide a dramatic benefit in speed and a reduction in network traffic and SQL Server activity. These benefits are balanced, of course, by the loss of real time data.

If you have a web service where real time data is not absolutely essential, then caching the Xml response may be appropriate. The cache interval is in minutes, and you may set the value at any time needed: 10 minutes, 60 minutes, 24 hours, or whatever makes sense.

If the web method runs reasonably fast, then it is probably fine to set the interval as low as 30 or 60 minutes. For a web service that might be called hundreds or thousands of times a day, you can reduce your hits to SQL Server dramatically.

The caching engine saves an XML file of the response of the web service; the next time the web service is called, the DynamicXmlBuilder looks to see if a saved file exists; if the file does not exist, then it runs the service and then saves the result as an XML file. If an XML file can be found, then the last update date of the file is checked; if the file was created more recently than the cache duration, then we simply return that file, thereby bypassing additional SQL calls and network traffic. If the file is older than the cache duration, then the web service is called, fresh data is returned, and the XML file is saved.

For web services that have parameters, an XML file is saved for each combination of parameters. For this reason, it makes sense to enable caching only for web method with no parameters, or with a small number of parameters to which a reasonably small number of values are passed.

If, for example, you have a web service with a Customer Key parameter, and the web service is called over and over for different customers every time, then there is little point in caching these responses because the web service will be asked for different data every time it is called. If, by contrast, you have a web service that returns an Event Calendar, and that web service is called thousands of times per day, then you might want to cache it on an hourly basis. If you are concerned that the data will be stale, then set the interval at 15 minutes. Instead of having 1,000 SQL hits daily, you will reduce the hits to fewer than 100, and the web service will return data faster.

This option is available beginning with the 2006 Q1 build of netFORUM. The option can be set in code by setting a value for the public property iMinutesToCache in the DynamicXmlBuilder. The option is not configurable from the iWeb user interface at this time. To avoid code re-compiles if this setting needs to change, the developer is encouraged to create a new <appSettings> element in the web.config file that resides in the web services application. This new element will be used to set the iMinutesToCache setting of the Dynamic XmlBuilder, thereby allowing for changing the option without needing to recompile.

For example, in the <appSettings> of web.config, add an element:

<EventCalendarMinutesCache>60</EventCalendarMinutesCache>
   

Next, in the DynamicXmlBuilder code for the Event Calendar web method, add the following:

int iCacheMinutes = 0
try
{
iCacheMinutes = System.Convert.ToInt32(ConfigurationSettings.AppSettings[“EventCalendarMintuesCache”].ToString());
}
catch
{}
 
xmlBuilder.iMinutesToCache = iCacheMinutes;
   

Cache Folder

The xml files will be stored here assuming the CacheRootFolder system option is set to the default value:

\\NFSite\CacheRoot\Cache\xml
   

See Also