Transcript System development with Java
ASP.NET
Rina Zviel-Girshin Lecture 7
• Caching • Web-Security
Overview
Rina Zviel-Girshin @Paralex 2
Client ASP.NET Execution Model Server
public class Hello{ protected void Page_Load( Object sender, EventArgs e) {…} } Hello.aspx.cs
First request Postback Output Cache
3
Caching
• Caching in general is a temporary storage of state for faster retrieval.
• Caching is a technique widely used in computing to
increase performance
by keeping frequently accessed or expensive data in memory. • In Web application caching is used to retain pages or data across HTTP requests and reuse them without the expense of recreating them.
• Cashing web-applications occurs 1. On client (browser caching) 2. On a server between the client and the web server (proxy caching) 4
ASP.NET Caching
• Browser’s and proxy’s caching are not directly managed by ASP.NET.
• However page and data caching can be managed directly through .aspx pages.
• ASP.NET has three kinds of caching that can be used by Web applications: –
output caching
by a request. - caches the dynamic response generated –
fragment caching
- caches portions (fragments) of a response generated by a request. –
data caching
- caches arbitrary objects programmatically. Rina Zviel-Girshin @Paralex 5
Output caching
•
Output caching
is useful when the contents of the entire page can be cached. • On a heavily accessed site, caching frequently accessed pages for even a minute at a time can result in performance improvement. • While a page is cached by the output cache, subsequent requests for that page are served from the output page without executing the code that created it.
Rina Zviel-Girshin @Paralex 6
Output caching
• The syntax for output caching: – Add a high-level
@OutputCache
directive-
<%@ OutputCache … %>
• When output caching is enabled, an output cache entry is created on the first
GET
request to the page. • Subsequent requests are served from the output cache entry until the cached request expires.
– an expiration policy that indicates that the page expires in X minutes from the time it is cached.
–
<%@ OutputCache Duration="60 "…//60 seconds duration
– If another request is received after that time, the page code is executed and the page can be cached again.
Rina Zviel-Girshin @Paralex 7
Example
<%@ OutputCache Duration="60" VaryByParam="none" %> Using the Output Cache
Last generated on:
Values of VaryByParam can be –a parameter, a list of parameters with ; sign , none and * 8
Output
• Refreshing of the page does not change the time, indicating that the second response is being served from the output cache.
Rina Zviel-Girshin @Paralex 9
Fragment caching
• Sometimes it is not practical to cache an entire page - perhaps portions of the page must be created or customized for each request. – current stock prices, current time, currency rate,… • It is often worthwhile to identify objects or data that are expensive to construct and are eligible for caching – Menu bars, pictures, banner advertisements, static info,… • Once these items are identified, they can be created once and then cached for some period of time. Rina Zviel-Girshin @Paralex 10
Implementation
• • To implement a page fragment caching you have to define user controls.
– In addition to the built-in server controls you can easily define your own controls called
user controls
.
• Define different areas of your page by creating a separate user control for each area.
• Within each user control define an OutputCache directive to indicate how this control is cached.
A user control
is almost identical to a normal
.aspx
page with two differences: – the user control has the
.
ascx
extension rather than .
aspx
– The file may not have ,
or