Title: Caching
1Caching
2Caching
- 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
3Things that can be stored in Cache
- Web Forms
- Parts of web forms
- Application Data
4Caching 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
5Another 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
6Multiple 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
7Vary 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
8Cache 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??)
9HttpCachePolicy members vs OutputCache directive
attributes
10Controlling 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
11Where are items stored?
- Default anywhere they are accepted
- Client
- Proxy server
- Host Server
12Caching parts of a page
- Given Large table that seldom varies
- Put table in user control and cache user control
response - Fragment caching
13Caching 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
14Caching 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
15Cache Object
- Intrinsic object
- Stores frequently used items (quick retrieval)
- Global available from anywhere in web app
- Similar to Application Object
16Ways 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
17Using Cache Object
18Parameters for Add and Insert
19Using Cache
- Demo 7 UsingCache.aspx
- Walkthru
- Stores items in Cache Load event
- Retrieve item from Cache PreRender event
20How 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
21Cache Priority Settings
22Using Cache Data before it is Removed from Memory
- Add/Insert Methods
- onRemoveCallback
- Demo 8 DataCache1.aspx
23Updating 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
24CacheDependency 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
25Using 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
26Demo 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 -
27Page_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
28CacheCustList 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
29butGetOrders_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)