Caching - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Caching

Description:

Insert method. Uses (optional) parameters to set policies and duration. Add method ... Use Add/Insert dependency parameter. Accepts a CacheDependency object ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 31
Provided by: tompe9
Category:
Tags: caching | insert

less

Transcript and Presenter's Notes

Title: Caching


1
Caching
  • Chapter 12

2
Caching
  • For high-performance apps
  • Caching storing frequently-used items in memory
  • Accessed more quickly
  • Cached Web Form bypasses
  • servers processing HTML
  • Servers running web form to create a response
  • Response is processed directly from memory

3
Things that can be stored in Cache
  • Web Forms
  • Parts of web forms
  • Application Data

4
Caching Web Forms
  • Good Caching Candidates
  • Frequently used
  • Contains fairly static data
  • First series of Demos ? Use OutputCache directive
  • Demos show different ways to control caching
  • Demo 1 WebFormCaching1.aspx
  • Response loaded into memory for 60 secs (on
    client)
  • Subsequent requests during period get stored
    response
  • After 60 secs new response is cached

5
Another way to control Caching
  • Previous examples used OutputCache directive
  • From code, use Response objects Cache property
  • Returns a HttpCachePolicy object
  • HttpCachePolicy members are similar to attributes
    of OutputCache directive

6
Multiple Caches from Single Web Form
  • Demo2 WebFormCaching2.aspx
  • VaryByParam attribute
  • New form cached when query string or HTTP POST
    varies
  • Demo up to 5 different cached responses
  • One for each time zone

7
Vary by Response Header
  • Demo 3 VaryByHeader.aspx
  • Different forms cached depending on Language
    Requested in Response.Header
  • Change regional settings in Windows Control Panel
  • One form cached per language requested

8
Cache Response / Custom String
  • Demo 4 CustomString.aspx
  • Custom string set in Global.aspx
  • Demo caches a response based on the browser used
    to request the page
  • (Netscape Mozilla??)

9
HttpCachePolicy members vs OutputCache directive
attributes
10
Controlling Caching thru Code
  • Demo 5 UsingResponseCache
  • Uses Response.Cache object
  • Sets Cache duration at 5 seconds
  • Similar to Duration attribute of OutputCache
    directive
  • lt_at_ OutputCache Duration"5" VaryByParam"None"
    Location"Any" gt

11
Where are items stored?
  • Default anywhere they are accepted
  • Client
  • Proxy server
  • Host Server

12
Caching parts of a page
  • Given Large table that seldom varies
  • Put table in user control and cache user control
    response
  • Fragment caching

13
Caching Part of a Web Form with a User Control
  • Place controls and content in a web user control
  • Set caching attributes for that control
  • Create an instance of the user control on the web
    form
  • Demo 6 PartialCaching1.aspx
  • Uses PartialCaching1.ascx

14
Caching Application Data
  • Cache frequently used data
  • Usually Read-Only
  • To store changed cache data before it is removed
    from memory
  • Use CacheItemRemovedCallback delegate to save
    changes

15
Cache Object
  • Intrinsic object
  • Stores frequently used items (quick retrieval)
  • Global available from anywhere in web app
  • Similar to Application Object

16
Ways to store data in Cache object
  • Assignment
  • Assign a value to a key in the Cache object
  • Cache(NewItem) Some String Data
  • Insert method
  • Uses (optional) parameters to set policies and
    duration
  • Add method
  • Parameters are required
  • Returns a reference to cached data

17
Using Cache Object
18
Parameters for Add and Insert
19
Using Cache
  • Demo 7 UsingCache.aspx
  • Walkthru
  • Stores items in Cache Load event
  • Retrieve item from Cache PreRender event

20
How Long does an item stay in Cache?
  • Add/Insert Parameters
  • Really these give only indirect control
  • When memory gets low, ASP.NET
  • Removes expired members from cache
  • Unloads other members based on their priority and
    when last accessed

21
Cache Priority Settings
22
Using Cache Data before it is Removed from Memory
  • Add/Insert Methods
  • onRemoveCallback
  • Demo 8 DataCache1.aspx

23
Updating Cache when Data Changes
  • Data stored in cache may represent data stored
    elsewhere (say in a database).
  • Establish relationship between cached data and
    external
  • File
  • Folder
  • Group of files
  • Use Add/Insert dependency parameter
  • Accepts a CacheDependency object

24
CacheDependency object
  • Identifies file, folder, or set of files to watch
    for change
  • ASP.NET checks timestamps of objects in
    CacheDependency object
  • Compares with DateTime for cached item
  • If later, unloads the item from the cache

25
Using CacheDependency object
  • Demo 9 DataCache2.aspx
  • NewItem in Cache is linked to NewItem.TXT file
  • When new data is written to the file, data is
    removed from Cache

26
Demo 10 C
  • Demo10 CachingLab.aspx
  • Displays a list of customers from Northwind db
  • Users select from dropdown list
  • Click button to view orders in data grid
  • Web Form cached for 1 minute
  • Since response can vary by which customer is
    selected, use VaryByParam to store multiple
    responses one for each selected customer

27
Page_Load Event
  • CustList is a ListItemCollection object
  • If this is the first time through (IsPostback)
  • If the list is not in the cache
  • Store it in the cache (CacheCustList())
  • Else
  • take the Customer List out of the cache
  • Move the CustList Details to the DropDownList

28
CacheCustList Method
  • Connect to NorthWind
  • Create a DataReader for the Customer records
  • For each row provided by the datareader
  • Add the Customer Data to the CustList
  • Put the CustList in the Cache 5 min
  • Close the connection

29
butGetOrders_Click
  • Create a new dataset to hold the orders for the
    selected customer
  • If the Order Data for this customer is not in the
    Cache, If IsNothing(Cache(drpCustList.SelectedItem
    .Value))
  • Put it there CacheOrders
  • Else
  • Set the dataset the Cached orders
  • Bind the order data to the grid

30
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com