Answers for Day 14

IOTA^_^    

Sams Teach Yourself ASP.NET in 21 Days, Second Edition
By Chris Payne
Table of Contents
Appendix A.  Answers to Quiz Questions


Quiz

1:

What's the difference between server-side and client-side caching?

A1:

Server-side caching caches output before it's sent to the browser. Subsequent requests to that data will retrieve from the cache. Client-side caching stores data (such as pages accessed over the Internet) on a user's hard drive so that it doesn't need to be downloaded again.

2:

Which caching mechanism stores cached data on the computer's hard drive?

A1:

Output caching. (This includes the HttpCachePolicy object.)

3:

What are the pros and cons of using the Cache class instead of output caching?

A1:

It allows you to use sliding expiration, provides more granularity for caching individual items, and allows you to set dependencies on cached items.

The Cache class stores data in memory as opposed to on disk, meaning it uses more valuable resources than page output caching.

4:

What's the difference between the SetExpires method and the SetMaxAge method of the Response.Cache object?

A1:

The SetExpires method sets the exact time that the content should expire, in terms of a DateTime object. The SetMaxAge function sets the time limit before the content expires in terms of a TimeSpan object.

5:

What's the full signature of the Cache.Add method?

A1:

 Cache.Add(key, source, dependencies, AbsoluteExpiration, SlidingExpiration, Priority, OnRemoveCallBack) 
6:

Suppose you have a page that accepts the following querystring parameters: userid, searchstring, pagenumber, and orderby. Using VaryByParams, which items should you cache by, and what's the required code?

A1:

You should cache by searchstring and orderby. These are the only two parameters that wouldn't necessarily change each request and aren't personal information.

The code is as follows:

 Response.Cache.VaryByParams.Item("searchstring") = true Response.Cache.VaryByParams.Item("orderby") = true 

Exercise

Q1:

Using Listing 14.5 as the basis for your testing, measure the performance benefit of retrieving the DataView from the cache instead of creating it explicitly. (Hint: use the Ticks property of the DateTime instance to return a small measure of time 100-nanosecond blocks, or 1-ten-millionth of a second.) Discuss the difference in time.

A1:

The code is as follows:

 1:    <%@ Page Language="VB" %> 2:    <%@ Import Namespace="System.Data" %> 3:    <%@ Import Namespace="System.Data.OleDb" %> 4:    <%@ Import Namespace="System.IO" %> 5: 6:    <script language="VB" runat="server"> 7:       private timStart as long 8:       private timEnd as long 9: 10:       sub Page_Load(Sender as Object, e as EventArgs) 11:          if not Page.IsPostBack then 12:             CreateData() 13:          end if 14:       end sub 15: 16:       sub CreateData 17:          dim source as DataView 18:          timStart = DateTime.Now.Ticks 19: 20:          source = Cache("DataView") 21: 22:          if source is nothing then 23:             dim strConnString as string = "Provider=" & _ 24:                "Microsoft.Jet.OLEDB.4.0;" & _ 25:                "Data Source=c:\ASPNET\data\banking.mdb" 26: 27:             dim objCmd as OleDbDataAdapter = new _ 28:                OleDbDataAdapter("select * from tblUsers", _ 29:                strConnString) 30: 31:             dim ds as DataSet = new DataSet() 32:             objCmd.Fill(ds, "tblUsers") 33: 34:             source = new DataView(ds.Tables(0)) 35:             Cache.Insert("DataView", source) 36: 37:             timEnd = Datetime.Now.Ticks 38:             lblMessage.Text = "Data set created explicitly" & _ 39:                "<br>" & timEnd - timStart 40:          else 41:             timEnd = Datetime.Now.Ticks 42: 43:             lblMessage.Text = "Data set retrieved from " & _ 44:                "cache<br>" & timEnd - timStart 45:          end if 46: 47:          dgData.DataSource = source 48:          dgData.DataBind() 49:       end sub 50: 51:       sub ExpireCache(Sender as Object, e as EventArgs) 52:          dim dv as dataview = Cache.Remove("DataView") 53:          CreateData() 54:       end sub 55:    </script> 56: 57:    <html><body> 58:       <form runat="server"> 59:          <asp:Label  runat="server" 60:             maintainState=false/><p> 61:          <asp:Button  runat="server" 62:             text="Expire Cache" 63:             OnClick="ExpireCache"/><p> 64:          <asp:DataGrid  runat="server" 65:             BorderColor="black" 66:             GridLines="Vertical" 67:             cellpadding="4" 68:             cellspacing="0" 69:             width="450" 70:             Font-Name="Arial" 71:             Font-Size="8pt" 72:             HeaderStyle-BackColor="#cccc99" 73:             ItemStyle-BackColor="#ffffff" 74:             AlternatingItemStyle-Backcolor="#cccccc" /> 75:       </form> 76:    </body></html> 

The average time to create the data explicitly on your machine is 580,835 100-nanosecond blocks (.058 seconds). The average time to retrieve the data from the cache is 0 100-nanosecond blocks. (This doesn't mean it was instantaneous; just that the amount of time was too small for ASP.NET to measure this way.)

The difference is only six-hundredths of a second for nine records from the database. Imagine returning 1,000 records for 100 different simultaneous users. Caching would be very useful.


    IOTA^_^    
    Top


    Sams Teach Yourself ASP. NET in 21 Days
    Sams Teach Yourself ASP.NET in 21 Days (2nd Edition)
    ISBN: 0672324458
    EAN: 2147483647
    Year: 2003
    Pages: 307
    Authors: Chris Payne

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net