.NET Samples - ASP.NET Caching

This section describes the following samples:

Datacache2 Sample

Datacache1 Sample

FragmentCache1 Sample

Outputcache2 Sample

Outputcache3 Sample

Outputcache1 Sample

Datacache2 Sample

This sample demonstrates how to store and retrieve objects with dependencies in the ASP.NET cache. In this case, the cache entry is dependent on a file. Whenever the file is changed, the existing cache entry is purged and re-created.

Namespaces used in this sample:

System; System.Data; System.IO

Classes used in this sample:

File; Stream; TextBox; Control; RequiredFieldValidator; Message; Cache; FileStream; Font; DataSet; Path; DataGrid; StreamReader; Object; EventArgs; DataView; Xml; Table; Import; TextWriter; Page; Style; Header; StreamWriter; CacheDependency; Exception; CheckBox; Label; DataRow

Datacache1 Sample

This sample demonstrates how to store and retrieve arbitrary objects, such as DataSet objects, in the ASP.NET cache.

Namespaces used in this sample:

System.Data; System; System.Data.SqlClient

Classes used in this sample:

Cache; DataSet; DataGrid; DataView; SqlDataAdapter; Font; DataAdapter; Import; SqlConnection; Object; Style; Page; EventArgs; Table; Header

FragmentCache1 Sample

This sample shows simple fragment caching in ASP.NET. The sample caches the output from a control that retrieves data from a SQL Server database, while keeping the dynamic properties of the parent page. You can see that the page is dynamic because the time is updated whenever the page is refreshed, while the control is only updated every 60 seconds.

Namespaces used in this sample:

System.Data; System; System.Data.SqlClient

Classes used in this sample:

DataGrid; Control; Page; SqlDataAdapter; Object; Font; DataSet; SqlConnection; DataAdapter; Import; String; EventArgs; Style; Table; DataView; Header; Cache

Outputcache2 Sample

This sample shows the same technique for output caching as the sample above, but queries a database and displays the results in a grid. Caching the response allows the server to respond to subsequent requests without retrieving from the database again.

Namespaces used in this sample:

System.Data; System; System.Data.SqlClient

Classes used in this sample:

DataGrid; SqlDataAdapter; Font; DataSet; SqlConnection; DataAdapter; Import; Cache; Style; Table; DataView; Page; EventArgs; Header; String; Object

Outputcache3 Sample

This sample modifies the previous example to allow the user to selectively query the database for authors in different U.S. states. This sample demonstrates caching requests that vary by the name/value pairs in the query string using the VaryByParam attribute of the **@OutputCache** directive.

Namespaces used in this sample:

System.Data; System; System.Data.SqlClient

Classes used in this sample:

DataGrid; String; Font; DataSet; SqlConnection; DataAdapter; Import; SqlDataAdapter; Cache; Table; DataView; Page; EventArgs; Header; Object; Style

Outputcache1 Sample

This sample demonstrates a simple way to output cache responses using the **@OutputCache** directive. The example simply displays the time when the response was generated. To see output caching in action, invoke the page and note the time at which the response was generated. Then refresh the page and note that the time has not changed, indicating that the second response is being served from the output cache.

Namespaces used in this sample:

(none)

Classes used in this sample:

Cache; Page; Object; EventArgs; String