Using the Data Cache

9/25/2010 11:33:27 AM
Using the data cache in the simplest and most naive way supported by ASP.NET is very much like accessing the Session object. Remember, accessing the Session object involves using an indexer (the square bracket syntax) and a consistent index to store and retrieve data. The data cache works in exactly the same way (although it has some other features for managing items in the cache).

The strategy for caching a piece of data usually involves these steps:

  1. Look in the cache for the data element.

  2. If it's there, use it (bypassing the expensive database round-trip).

  3. If the data element is unavailable in the cache, make a round-trip to the database to fetch it.

  4. If you had to fetch the data, cache the data element so that it is available next time around.

The next example modifies the UseDataList page so that it stores the data item in the cache after acquiring it for the first time. Although the first time Page_Load is called it might take a while (on a computer's time scale), subsequent calls are much faster.

Using the cache

  1. Open the UseDataList.aspx.cs file and go to the GetInventory method.

  2. Modifying the method to use the cache is fairly straightforward. The following listing highlights the changes. First, check to see whether the item is in the cache. If searching the cache for the DataSet turns up a valid object reference, you can bypass the database lookup code and return the referenced DataSet. If searching the cache turns up a null object reference, go ahead and make the round-trip to the database. When the database lookup finishes, you'll have a good DataSet (provided the query succeeds). Cache it before returning the reference to the caller. If you include the Trace statements, you can see exactly how big an impact caching can make. The changes you need to make are shown in bold type:

    protected DataTable GetInventory()
    DataTable dt = null;

    Trace.Warn("Page_Load", "looking in cache");
    dt = (DataTable)Cache["InventoryDataTable"];
    Trace.Warn("Page_Load", "done looking in cache");
    if (dt == null)
    Trace.Warn("Page_Load", "Performing DB lookup");
    dt = new DataTable();
    string strConnection =
    @"Data Source=
    Integrated Security=True;
    User Instance=True";
    DbProviderFactory f =

    using (DbConnection connection = f.CreateConnection())
    connection.ConnectionString = strConnection;
    DbCommand command = f.CreateCommand();
    command.CommandText = "Select * from DotNetReferences";
    command.Connection = connection;

    IDataReader reader = command.ExecuteReader();
    Cache["InventoryDataTable"] = dt;
    Trace.Warn("Page_Load", "Done performing DB lookup");
    return dt;

Most View
MSI GX60 Review - Radeon HD 7970M In A $1,200 Gaming Notebook (Part 7)
Lenovo IdeaPad Yoga 13 - Convertible Laptop (Part 2)
Livescribe Wi-Fi Smartpen - Is The Pen Mightier Than The Keyboard?
Budget Portable Hard Drivers Review
Asus VivoBook S200E-C157H Touchscreen Laptop
Turn An Old Computer Into A Server Using Ubuntu (Part 1)
Soundbar LG NB3530A Review
Tweetbot - Tasty Tweets
Nikon 1 J2 With Stylish Design And Dependable Image And Video Quality
Android 4.0 Ice Cream Sandwich Guided Tour (Part 3)
Top 10
Sharepoint 2013 : Farm Management - Disable a Timer Job,Start a Timer Job, Set the Schedule for a Timer Job
Sharepoint 2013 : Farm Management - Display Available Timer Jobs on the Farm, Get a Specific Timer Job, Enable a Timer Job
Sharepoint 2013 : Farm Management - Review Workflow Configuration Settings,Modify Workflow Configuration Settings
Sharepoint 2013 : Farm Management - Review SharePoint Designer Settings, Configure SharePoint Designer Settings
Sharepoint 2013 : Farm Management - Remove a Managed Path, Merge Log Files, End the Current Log File
SQL Server 2012 : Policy Based Management - Evaluating Policies
SQL Server 2012 : Defining Policies (part 3) - Creating Policies
SQL Server 2012 : Defining Policies (part 2) - Conditions
SQL Server 2012 : Defining Policies (part 1) - Management Facets
Microsoft Exchange Server 2010 : Configuring Anti-Spam and Message Filtering Options (part 4) - Preventing Internal Servers from Being Filtered