Introduction to ASP.NET

Download Report

Transcript Introduction to ASP.NET

Caching

MacDonald Ch. 26 MIS 424 Professor Sandvig

Overview What is Caching Where to cache When to cache Data caching in ASP.NET

1. Page caching 2. Page Fragment caching 3. Cache Class Create you own cache

What is Caching Cache = “store” – Armies: cache weapons – Squirrels: cache nuts – Computers: cache data Benefits of caching data: – Reuse Process once, reuse many times Faster, cheaper

Where to Cache Client-side – Browsers Images Pages – Reduces download times Server-side – Reduces server processing load

When to Cache on server Frequently hit pages – Content changes infrequently NYTimes.com

ESPN.com

CMS (see Drupal Boost ) Data is expensive to retrieve – MIS 424 Weather forecast Amazon Best Sellers

When NOT to cache Pages that change frequently – Shopping cart – Amazon.com

Customized for each user Infrequently accessed data – Cache must be stored  Overhead – cost depends upon caching method

Caching Tools in ASP.NET

Caching Options 1. Page 2. Page fragment 3. Data class 4. Create your own   Database Application or session object

Page Caching Caches entire page Page directive: <%@ Page OutputCache Duration=“300” VaryByParm=“none” %> – Duration in seconds Stored on disk – Low overhead

Page Caching - parameters VaryByParm= – “None” Single cache for all parameter combinations – “*” Different cache for each parameter combination – “ParameterList” Different cache for listed parameters One cache for other parameters

Page Caching - parameters Example: – 14_CachePage.aspx source , output Location: – Server – Client – Downstream – proxy servers – Any – None

Page Caching - parameters Also may specify cache for header attributes: – Language – Browser version – Etc.

ASP.NET Caching 1.

Page 2.

Page fragment 3.

Data class 4.

Create your own

Page Fragment Caching Cache part of a page – Content in user control Syntax similar to Page cache Example: – Music Store menu

Page Fragment Caching Benefits: – Useful for menus, headers, footers… – Easy to implement – Stored on disk Weaknesses – OnClick events frozen – Timeout fixed duration only

ASP.NET Caching 1.

Page 2.

Page fragment 3.

Cache class 4.

Create your own

Cache Class Cached data accessible throughout application – Use data on multiple page Cache any type of data object – strings, datasets, dataviews, … Set expiration date – Several options

Cache Class Cache.Insert method Parameters for: – Expiration date – absolute & sliding – File dependencies Database tables – Subroutine to run when expires Documentation

Cache Class - Syntax Cache.Add(Key, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback) Example: – CacheExample.aspx source , output

Create Your Own Program own rules Customize storage location – Database – File – Combination of Cache locations Expiration rule – Fixed time of day, week, month… – Number of hits… Example: Weather forecast

Summary Several Caching options .NET

– Page – Page fragment – Cache object – Roll your own Dramatic performance improvements when data expensive to retrieve