Click here to Skip to main content
15,880,427 members
Articles / Programming Languages / C# 4.0

How to Purge Cache in Akamai Proxy Server using C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
24 Apr 2011CPOL2 min read 59.9K   4   20
How to use C# to programmatically purge cache of the Akamai proxy server

Purge Request to Akamai

Purge request to Akamai server is a process to send instruction to the proxy server to free-up some item in the cache so that the new request will get a new copy of data. This is an HTTP command used by Akamai proxy server. Purge cache in Akamai proxy server is very easy, all you need to know is what programming language you use and add the appropriate Web Service.

Akamai Web Service

Language URL
Pearl https://ccuapi.akamai.com/ccuapi.wsdl
Microsoft Visual Basic 6, SOAP 1.0 https://ccuapi.akamai.com/ccuapi-list.wsdl
Microsoft Visual Basic 6, SOAP 3.0 https://ccuapi.akamai.com/ccuapi-list-2001.wsdl
Microsoft Visual Basic .NET https://ccuapi.akamai.com/ccuapi-dotnet.wsdl
Java https://ccuapi.akamai.com/ccuapi-axis.wsdl

Steps to Purge Cache in Akamai Proxy Server in C#

1. Open Microsoft Visual Studio (I am using 2008)

I am going to use C#, so I need to run Visual Studio to create a project and write code to purge cache items:

Image 1

2. Add Akamai Web Service

Akamai is exposing one of their web services that has functionality to trigger purge cache request in Akamai proxy server. All we need to do is to add this service as reference in our .NET application.

Image 2

Right click on the C# project and then click on the Add Service Reference item, then the Add Service Reference popup window will appear.

Image 3

In the Address input box, please input https://ccuapi.akamai.com/ccuapi-dotnet.wsdl and then click on the Go button to load the web service. At the bottom, you'll see Namespace input box you can modify the namespace a sper your convenience, let's say AkamaiPurgeCache. You will be using this namespace upon accessing the methods and classes inside this service.

3. Write Code to Initiate Purge Request to Akamai Proxy Server

Please modify the needed information, especially the credentials and the item you want to purge.

C#
public void PurgeCache() 
{ 
    try 
    { 
        string username = "username"; 	//use the username to access 
					//the control.akamai.com 
        string password = "password"; 	//use the password to access the control.
					//akamai.com 
        string email = "mysample@email.com,
	mysample@anotheremail.com"; //for multiple emails use comma as separator 
        string[] fileToPurge = new string[]
	{"nstore.abc.com/1234/folder/myfile.html"}; //this is the item in 
					//akamai proxy that you want to purge
        string action = "action=remove"; 	//other value is action=invalidate 
        string domain = "domain=production";//other value is domain=staging 
        
        //type: type=cpcode or type=arl >> arl: akamai resource locator. 
        //url: uniform resource locator. 
        //To use the type cpcode option, your administrator must enable 
        //purge-by-cpcode access for your username 
        //through Akamai EdgeControl. 
        string purgeType = "type=arl"; //other value is type=cpcode 
        
        string[] options = new string[]{action, domain, email, purgeType}; 
        AkamaiPurgeCache.PurgeApi purgeAPI = new AkamaiPurgeCache.PurgeApiClient(); 
        AkamaiPurgeCache.PurgeResult purgeResult = purgeAPI.purgeRequest
		(username, password, string.Empty, options, fileToPurge); 
        
        if (purgeResult.resultCode >= 300) 
        { 
            System.Text.StringBuilder sb = new System.Text.StringBuilder(); 
            sb.AppendFormat("Error Code: {0}",purgeResult.resultCode); 
            sb.AppendFormat("Error Message: {0}", purgeResult.resultMsg); 
            sb.AppendFormat("Session ID: {0}", purgeResult.sessionID); 
            sb.AppendFormat("URI Index: {0}", purgeResult.uriIndex); 
            throw new Exception(sb.ToString()); 
        } 
    }
    catch(Exception e) { //exception code here.. } 
}

History

  • 23rd April, 2011: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Philippines Philippines
Watch Anime...

Comments and Discussions

 
QuestionAbove code not working for me Pin
suhel_khan19-Apr-17 2:54
professionalsuhel_khan19-Apr-17 2:54 
QuestionI need to Purge all files in my site instead of a single file Pin
surekumar26-Feb-17 21:06
surekumar26-Feb-17 21:06 
QuestionGeting this error | System.ServiceModel.EndpointNotFoundException Pin
Sweety .12-Mar-14 22:41
Sweety .12-Mar-14 22:41 
QuestionIn option of "email", you should add a title. Pin
jealter15-Jan-14 20:09
jealter15-Jan-14 20:09 
Questionthrow new Exception(sb.ToString()) actually throws exception Pin
mykey20053-Jul-13 3:47
mykey20053-Jul-13 3:47 
AnswerRe: throw new Exception(sb.ToString()) actually throws exception Pin
mykey20053-Jul-13 5:26
mykey20053-Jul-13 5:26 
GeneralRe: throw new Exception(sb.ToString()) actually throws exception Pin
Jephunneh Malazarte23-Jul-13 23:37
Jephunneh Malazarte23-Jul-13 23:37 
QuestionHow to clear cache at folder level Pin
User@Rupesh6-May-13 8:03
User@Rupesh6-May-13 8:03 
QuestionFor website Akamai cache key is different Pin
anushripatil5-Dec-11 23:45
anushripatil5-Dec-11 23:45 
AnswerRe: For website Akamai cache key is different Pin
Jephunneh Malazarte6-Dec-11 2:27
Jephunneh Malazarte6-Dec-11 2:27 
GeneralRe: For website Akamai cache key is different Pin
anushripatil6-Dec-11 17:10
anushripatil6-Dec-11 17:10 
Questionthank you so much for this code! Pin
ExoticmE15-Oct-11 22:30
ExoticmE15-Oct-11 22:30 
AnswerRe: thank you so much for this code! Pin
Jephunneh Malazarte16-Oct-11 4:12
Jephunneh Malazarte16-Oct-11 4:12 
QuestionAdd On Tips when purging Pin
Jephunneh Malazarte6-Sep-11 0:03
Jephunneh Malazarte6-Sep-11 0:03 
GeneralLimitation of Akamai Purge Request using CCUAPI Pin
Jephunneh Malazarte2-Jun-11 0:04
Jephunneh Malazarte2-Jun-11 0:04 
General"Invalid Option" response Pin
Chayim19-May-11 9:10
Chayim19-May-11 9:10 
GeneralRe: "Invalid Option" response Pin
Jephunneh Malazarte1-Jun-11 23:59
Jephunneh Malazarte1-Jun-11 23:59 
GeneralRe: "Invalid Option" response Pin
mcintyre23117-Oct-12 23:40
mcintyre23117-Oct-12 23:40 
GeneralRe: "Invalid Option" response Pin
Jephunneh Malazarte2-Jun-11 0:15
Jephunneh Malazarte2-Jun-11 0:15 
ammm chayim "if you don't mind" please lemme know if issue still persist.
thanks...
Nothing!!!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.