Thursday, December 29, 2011

Tracking Customers with Application Variables




Like Session variables, Application variables can be used to store information over multiple pages. Unlike Session variables, however, Application variables aren't associated with a particular user. The values stored in an Application variable can be assigned and retrieved by every user of your Web site.

To create an Application variable, you use the Application object. For example, to create an Application variable named "myVar", you would use the following statement:

Application( "myVar" ) = "Hello World"

To retrieve an Application variable, you also use the Application object. The following statement displays the contents of the Application variable named "myVar":

Response.Write Application( "myVar" )

When the value of an Application variable is changed, it is changed for every user of your Web site. For example, imagine that Ruth retrieves a page from your Web site which assigns the value blue to the Application variable named favoriteColor. Now, suppose that Andrew comes along and retrieves a page that assigns the value red to the Application variable favoriteColor. After Andrew changes the value of the favoriteColor Application variable, the value of this variable will be changed for everyone. After Andrew retrieves the page, the favorite- Color variable also has the value red for Ruth.

Because the same Application variable can be changed by different users of your Web site, conflicts can occur. For example, a common use of Application variables is for tracking the number of times a page has been viewed. The ASP code display simple page counter

<%
 Application( "counter" ) = Application( "counter" ) + 1
%>

Page Counter
 This page has been viewed
<%=Application( "counter" )%> times.


There is an important problem with the ASP code above. Imagine that two people request the page at the same time. Ruth requests the page and the counter Application variable has the value 345. At the same time, Andrew requests the page, and the application variable has the value 345. After both visitors retrieve the page, the Application variable will have the value 346. However, because two people have requested the page, it should have the value 347. Fortunately, there is an easy way to fix this problem. The Application object has two methods named Lock and Unlock. The Lock method locks all the Application variables and prevents anyone except the current user from reading or modifying them. The Unlock method releases the
Application variables once again.

  
<%
 Application.Lock
 Application( "counter" ) = Application( "counter" ) + 1
 Application.Unlock
 %>
Page Counter

This page has been viewed
<%=Application( "counter" )%> times.

The ASP above is the same as the ASP code we have earlier except that both the Lock and Unlock methods of the Application object are called. The Lock method is called in line 2. This prevents anyone else from reading or modifying the counter Application variable. After the Application variable has been modified in line 3, the Unlock method is called in line 4 to release the Application variables.

It is important to understand that calling the Lock method locks all the Application variables in memory. You cannot selectively lock Application variables.

After you call the Lock method, all Application variables will continue to be locked until either the Unlock method is called or the page finishes processing. This means that you cannot accidentally lock all Application variables forever within an ASP script.

You should also be aware that locking Application variables doesn't prevent other users from modifying an Application variable. If a number of users attempt to modify an Application variable at the same time, and each user requests a page that calls the Lock method, all the modifications will happen. However, the modifications will take place serially rather than concurrently.


Storing Arrays in Application Variables

One common use of Application variables is to store frequently accessed but infrequently modified database records in memory. Retrieving database records can be a slow process. If the records do not change often, I recommend that you retrieve the database records only once and store them in an Application array. This way, the records can be retrieved very quickly from the Application array the next time they are requested.

The script below demonstrates how you can assign an array to an Application variable named myArray.

<%
 DIM myArray( 10 )
 myArray( 0 ) = "Hello World!"
 Application( "myArray" ) = myArray
 %>

Although you can directly read the value of an element contained in an Application array, you can't modify it. For example, the following statement will have no effect:

Application( "myArray" )(2) = "Goodbye!"

If you want to change the value of an element in an Application array, you must first assign the Application array to a local array. For example, the script in below properly changes the value of an element contained in an Application array.

Modifying an Element in an Application Array

<%
   Application.Lock
   myArray = Application( "myArray" )
   myArray( 0 ) = "Goodbye!"
   Application( "myArray" ) = myArray
   Application( "myArray" ).Unlock
%>


The script above modifies an element of an Application array. In line 3, the Application array named myArray is assigned to a local array with the same name. Next, in line 4, an element of the local array is modified. Finally, in line 5, the local array is assigned to the Application array once again.

Removing Application Variables from Memory You should be careful when creating Application variables. Application variables take up memory. Unlike a Session variable, an Application variable is never automatically removed from memory.

Prior to the version of Active Server Pages included with Windows 2000, there was no way to remove an Application variable from memory using an ASP script. Application variables remained in memory until the Web service was stopped, the Global.asa file was modified, or your ASP Application was unloaded.

The new version of Active Server Pages included with Windows 2000 includes two new methods you can use to remove Application variables from memory: the Remove() and the RemoveAll() methods. The Remove() method removes a particular Application variable from memory. The RemoveAll() method removes all Application variables from memory.

For example, the script below creates two Application variables and then removes one of them.

Using the Remove() Method

<%
 Application( "myvar1" ) = "Red"
 Application( "myvar2" ) = "Blue"
 Application.Contents.Remove( "myvar1" )
 %>


In lines 2 and 3, two Application variables are created. In line 4, the Remove() method is used to remove the Application variable created in line 2. To remove all Application variables from memory, you can use the RemoveAll() method. The script below demonstrates how this method can be used.

 <%
 Application( "myvar1" ) = "Red"
 Application( "myvar2" ) = "Blue"
 Application.Contents.RemoveAll()
%>

No comments:

Post a Comment