Proxy Purge API

From Sense/Net Wiki
Jump to: navigation, search
  •  
  •  
  •  
  •  
  • 100%
  • 6.0.5
  • Enterprise
  • Community
  • Planned

Overview

Purge action
When a proxy server is configured for your portal, content can be cached on the proxy - for example images, html pages. These items are stored in the cache for a configured time (see Proxy Cache Configuration page for more information). If the content changes (an image is replaced or an article is changed), the portal can send a purge message to the proxy to remove a content from its cache.

Please note that the portal does not send purge messages automatically. If you want to send a purge message when a content changes you need to do it programatically, e.g. in a NodeObserver.

Details

The Purge From Proxy action is designed to purge URLs immediately from the proxy cache. This feature uses a configurable algorithm to collect the URL list for a particular content that needs to be sent to the proxy for purging.

Proxy servers

You can find information about configuring proxy servers on the Proxy Cache Configuration page. As a developer you can access the configured proxy servers by the following static list:

PortalContext.ProxyIPs

How to purge a URL from the proxy cache

1. Purge a single URL with result
You can purge a single URL from the proxy cache using the following API call:
var result = HttpHeaderTools.PurgeUrlFromProxy(url);
In the sample above result is an array of the results returned by the individual proxy servers that are configured in the web.config. The order of the results is the same as the proxy IPs in the PortalContext.ProxyIPs list mentioned above.

The possible result values are:

  • OK - value was found in the proxy cache and as sucessfully purged
  • MISS - value was not in the cache
  • Any other message - an error occured, for example proxy is not configured correctly.
2. Purge a single URL asynchronously
If you need a fast mechanism to purge a single URL using asynchronous web request without getting any results, use
HttpHeaderTools.PurgeUrlFromProxyAsync(url);
3. Purge multiple URLs with result
You can purge multiple URLs from the proxy cache using the following API call:
var urlList = urlCollector.GetUrls(node);
var result = HttpHeaderTools.PurgeUrlsFromProxy(urlList);
In this case the result is a dictionary that contains all the given URLs as keys and the same proxy result lists (as in case of the single purge call) as values.
4. Purge multiple URLs asynchronously with delay
It is possible to purge multiple URLs using fast asynchronous web requests, with a configured delay. This can be useful, if there are other caching mechanisms on the portal, that can only be purged with a certain delay, so there is a risk of purging the url from the proxy first, and the proxy will "save" the response of the next request before the portal cache could be refreshed - thus saving the old value instead of the new value. Using delay for proxy purging can avoid this:
var urlList = urlCollector.GetUrls(node);
HttpHeaderTools.BeginPurgeUrlsFromProxyWithDelay(urlList);
The delay can be adjusted with the PurgeUrlDelayInSeconds web.config key.

PurgeUrlCollector

Proxy purge Portlet is designed to purge URLs immediately from the proxy cache. This portlet uses the PurgeUrlCollector class to collect the URL list for a particular content that needs to be sent to the proxy for purging.

Custom URL collector class

The default collecting algorithm is explained on the Purge From Proxy action page. If you want to change this algorithm you can do so by inheriting from the PurgeUrlCollector class and overriding one or more of the following methods:

  • GetUrls(Node context)
  • GetUrls(string path) (where path is a content path, e.g. /Root/MyContent) - both methods call the collector methods below.
  • CollectUrlsByContentPath(IEnumerable<string> pathList)
  • CollectUrlsByContentPath(IEnumerable<string> pathList, IEnumerable<string> siteUrls) (where pathList is a list of content paths, e.g. /Root/MyFolder/MyContent and siteUrls is a list of site urls, e.g. www.example.com)

You can decide to completely ignore the default algorithm by not calling the base methods here or just extend them by calling the base method and adding additional URLs to the default list.

public class CustomPurgeUrlCollector : PurgeUrlCollector
{
    public override List<string> GetUrls(Node context)
    {
        var urlList = base.GetUrls(context);
 
        if (context == null)
            return urlList;
 
        urlList.Add("www.example.com/mycustomurl");
 
        return urlList;
    }
}

Type configuration

After creating your own custom URL collector class, you have to change the Unity type registration settings in the web.config.

  • Add new type entry to unity/typeAliases section
<typeAlias alias="CustomPurgeUrlCollector" type="MyProject.CustomPurgeUrlCollector, MyProject"/>
  • Change the PurgeUrlCollector configuration entry under unity/containers/container[name="Providers"] section to the following:
<type type="PurgeUrlCollector" mapTo="CustomPurgeUrlCollector">
	<lifetime type="singleton"/>
</type>

After the configuration changes above, the proxy purge portlet will call your custom URL collector method when performing a manual proxy purge.

References