Adding Cache Rules

This VBScript example creates a new FPCURLSet object in the FPCURLSets collection of a Forefront TMG computer serving as a proxy, adds URLs to the URL set, and creates two new FPCCacheRule objects representing cache rules for caching content with a fixed range of Time to Live (TTL) values from all sites on the External network, except the sites in the new URL set. The script includes a single subroutine, called AddCacheRule.

This example is included as the AddCacheRule.vbs script in the Samples\Admin folder of the Forefront TMG Software Development Kit (SDK).

The following procedure lists the steps used to add two cache rules in the code example that follows.

To add cache rules

  1. Define values from the FpcIncludeStatus and FpcTimeScale enumerations. For more information about using values defined in Forefront TMG enumerated types in scripts, see Using Enumerated Types in Scripts.
  2. Create an instance of the FPC COM object, which provides access to the other Forefront TMG administration COM objects.
  3. Declare an FPCArray object, an FPCCacheRules collection, an FPCURLSet object, an FPCURLSets collection, and two FPCCacheRule objects.
  4. Get references to the existing FPCArray object, FPCCacheRules collection, and FPCURLSets collection.
  5. Verify whether an FPCURLSet object named Not for Caching already exists. If it exists, remove it.
  6. Call the Add method of the URL sets collection to create a new set of URLs named Not for Caching that will be excluded from caching.
  7. Verify whether an FPCCacheRule object named Shorter TTL Cache Rule already exists. If it exists, remove it.
  8. Repeat the preceding step for an FPCCacheRule object named Excluded Cache Rule.
  9. Call the Add method of the cache rules collection to create a new cache rule named Shorter TTL Cache Rule that will allow the caching of content with a shorter TTL in response to requests sent to sites on the External network, except for the URLs specified in the Not for Caching URL set.
  10. Repeat the preceding step for a cache rule named Excluded Cache Rule that will prohibit the caching of content from the URLs specified in the Not for Caching URL set.
  11. Set the Description, URLSets, and HTTPConfiguration properties of the cache rules.
  12. Set the NeverCacheResponse property on the Excluded Cache Rule FPCCacheRule object to True.
  13. Call the Save method on the cache rules collection to write the changes to the new cache rules to persistent storage.

The following code can be saved to a .vbs file and run from a command prompt on a computer running Forefront TMG with the Microsoft Firewall service installed.

Sub AddCacheRule()
	' Define enumeration values.
	Const fpcInclude = 0
	Const fpcExclude = 1
	Const fpcTimeInHours = 3
	' Create the root object.
	Dim root  ' The FPCLib.FPC root object
	Set root = CreateObject("FPC.Root")
	'Declare the other objects needed.
	Dim isaArray	' An FPCArray object
	Dim rules	 ' An FPCCacheRules collection
	Dim urlsets	 ' An FPCURLSets collection
	Dim urlset	' An FPCURLSet object
	Dim newRule1	' An FPCCacheRule object
	Dim newRule2	' An FPCCacheRule object
	' Get references to the array object, the cache 
	' rules collection, and the URL sets collection.
	Set isaArray = root.GetContainingArray()
	Set rules = isaArray.Cache.CacheConfiguration.CacheRules
	Set urlsets = isaArray.RuleElements.URLSets
	' If a URL set named "Not for Caching" already exists, remove it.
	On Error Resume Next
	Set urlset = urlsets.Item("Not for Caching")
	If Err.Number = 0 Then
		WScript.Echo "The Not for Caching URL set exists. Removing it ..."
		urlsets.Remove "Not for Caching"
		urlsets.Save
	End If
	WScript.Echo "Creating a new URL set for exclusion from caching ..."
	Set urlset = urlsets.Add("Not for Caching")
	urlset.Add "http://www.northwindtraders.com"
	urlset.Add "http://www.northwindtraders.com/*"
	urlsets.Save
	' If a cache rule named "Shorter TTL Cache Rule" already exists, remove it.
	On Error Resume Next
	Set newRule1 = rules.Item("Shorter TTL Cache Rule")
	If Err.Number = 0 Then
		WScript.Echo "Shorter TTL Cache Rule exists. Removing it ..."
		rules.Remove "Shorter TTL Cache Rule"
		rules.Save
	End If
	' If a cache rule named "Excluded Cache Rule" already exists, remove it.
	On Error Resume Next
	Set newRule2 = rules.Item("Excluded Cache Rule")
	If Err.Number = 0 Then
		WScript.Echo "Excluded Cache Rule rule exists. Removing it ..."
		rules.Remove "Excluded Cache Rule"
		rules.Save
	End If
	WScript.Echo "Creating new cache rules ..."
	Set newRule2 = rules.Add("Excluded Cache Rule")
	Set newRule1 = rules.Add("Shorter TTL Cache Rule")
	' Set the descriptions of the new cache rules.
	newRule1.Description = "This rule caches content with a shorter TTL " & _
						 "in response to requests sent to sites on the " & _
						 "External network except for specific URLs." 
	newRule2.Description = "This rule prohibits caching of content " & _
						 "from specific URLs." 
	' Add the External network to the first rule.
	newRule1.DestinationSelectionIPs.Networks.Add "External", fpcInclude
	' Add the new URL set as an exception to the objects referenced by 
	' the URLSets property of the first cache rule and as an included
	' object in the second cache rule.
	newRule1.UrlSets.Add "Not for Caching", fpcExclude
	newRule2.UrlSets.Add "Not for Caching", fpcInclude
	' Set shorter TTLs for all requests to which the first rule applies.
	newRule1.HTTPConfiguration.ApplyBoundsToObjectsWithExpiration = True
	newRule1.HTTPConfiguration.MinInterval = minTTL
	newRule1.HTTPConfiguration.MaxIntervalUnits = fpcTimeInHours
	newRule1.HTTPConfiguration.MaxIntervalValue = maxTTL
	' Set no caching for all requests to which the second rule applies.
	newRule2.NeverCacheResponse = True
	' Save the changes to the new cache rules.
	rules.Save
	WScript.Echo "Done!"
End Sub 
AddCacheRule

Send comments about this topic to Microsoft

Build date: 11/30/2009

© 2008 Microsoft Corporation. All rights reserved.