Caching - North Texas PC User`s Group

Download Report

Transcript Caching - North Texas PC User`s Group

Caching
Chapter 12
Caching
• For high-performance apps
• Caching: storing frequently-used items in
memory
– Accessed more quickly
• Cached Web Form bypasses:
– server’s processing HTML
– Server’s running web form to create a response
• Response is processed directly from memory
Things that can be stored in Cache
• Web Forms
• Parts of web forms
• Application Data
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
Another way to control Caching
• Previous examples used OutputCache
directive
• From code, use Response object’s Cache
property
• Returns a HttpCachePolicy object
• HttpCachePolicy members are similar to
attributes of OutputCache directive
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
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
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??)
HttpCachePolicy members vs
OutputCache directive attributes
Controlling Caching thru Code
•
•
•
•
Demo 5 – UsingResponseCache
Uses Response.Cache object
Sets Cache duration at 5 seconds
Similar to Duration attribute of
OutputCache directive:
• <%@ OutputCache Duration="5"
VaryByParam="None" Location="Any" %>
Where are items stored?
• Default – anywhere they are accepted
– Client
– Proxy server
– Host Server
Caching parts of a page
• Given: Large table that seldom varies
• Put table in user control and cache user
control response
• Fragment caching
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
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
Cache Object
• Intrinsic object
• Stores frequently used items (quick
retrieval)
• Global – available from anywhere in web
app
• Similar to Application Object
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
Using Cache Object
Parameters for Add and Insert
Using Cache
•
•
•
•
Demo 7 – UsingCache.aspx
Walkthru
Stores items in Cache – Load event
Retrieve item from Cache – PreRender
event
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
Cache Priority Settings
Using Cache Data before it is
Removed from Memory
• Add/Insert Methods
– onRemoveCallback
• Demo 8 – DataCache1.aspx
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
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
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
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
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
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
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