Using Application Variables

Application variables are available to all pages within an application, that is, pages that have the same application name. Because they are persistent, you can pass values between pages with a minimum of effort.

You set the application name in the cfapplication tag, normally on your application's Application.cfm page. The name you establish in the cfapplication tag is accessible elsewhere in the application by using the Application.ApplicationName variable. For example, you use this variable in the cflock tag to restrict access to Application variables to one request at a time.

Unlike Client and Session variables, Application variables do not require that a client name (client ID) is associated with them. Thus, they are available to any clients that specify the same application name. Also, you do not need to use the cfapplication tag to enable Application variables. Using the ColdFusion Administrator you can enable or disable Application variables in all applications; you cannot override this setting for an individual application.

Like Session variables, ColdFusion stores Application variables in the ColdFusion Server's memory and you can set timeout values for these variables either with cfapplication, or by specifying timeouts in the ColdFusion Administrator.

For information on setting timeouts for variables, see Advanced ColdFusion Administration.

Storing application data in Application variables

Application variables are designed to store application-level data. They are a convenient place to store information that all pages of your application might need no matter which client is running that application. Using Application variables, an application could initialize itself, say, when the first user access any page of that application. This information can then remain available indefinitely to all subsequent hits of any pages of that application, by all users, thereby avoiding the overhead of repeated initialization.

Because the data stored in Application variables is available to all pages of an application and remains available until either a specific period of inactivity passes or the ColdFusion Server shuts down, Application variables are very convenient. However, because all clients running an application see the same set of Application variables, they are not useful for client-specific information. To target variables for specific clients, use Session or Client variables.

Application variable timeouts

Application variables have a specific lifetime. If no clients access the application within the specified timeout period, ColdFusion Server destroys its Application variables.

The default timeout period for Application variables is two days. On the Variables page of the ColdFusion Administrator, you can define timeout values for application and Session variables. For more information, see Advanced ColdFusion Administration.

You can set the timeout period for Application variables within a specific application (thereby overriding the default setting in the ColdFusion Administrator) by using the applicationTimeout attribute of the cfapplication tag.

Tips for using Application variables

In general, Application variables are designed to hold information that you seldom write but read often. In most cases, the values of these variables are set once, most often when an application first starts. Then the values of these variables are referenced many times throughout the life of the application or the course of a session.

When using Application variables, keep in mind that these variables are shared by all instances of an application that might be running on a server. Because of this sharing, applications cannot assume that values saved in these variables are not overwritten by other instances of the same application that might be running simultaneously on the server. This is not a problem if you treat these variables as write-once, read-many, but it can be a problem if they are written to indiscriminately.

Getting a list of Application variables

Like the Session scope, the Application scope is registered as a ColdFusion structure. This enables you to use the ColdFusion Structure functions to get a list of Application and Session variables. For example, you can use cfloop with the StructFind function to output a list of Application and Session variables defined for a specific application.



Banner.Novgorod.Ru