Caching with ASP+ and ASP
BY Salim Naim
When we use the word "cache," we are talking about data that is saved in memory on the web server. Just because you can cache data doesn't mean you should. If you have data that is constantly changing, caching the data isn't going to buy you anything. In fact, you would just be wasting memory. However, if you have data that you will access over and over and that does not change often (in this case, often means every few seconds or minutes), this data is a good candidate for caching.
Output caching sometimes referred to as page caching is the term for saving data that is in its final presentation state, you take the input and transform it into what you are going to present, then cache the transformed data -- you save the data as you would display it. This type of caching gives you the most bang for the performance increase because you only retrieve the data and format it once. It saves work (translated into time) for every request you don't have to reprocess. Output caching works especially well for a Web page that will be shown to many in the same manner.
Data Caching is the term for saving specific data programmatically. This type of caching also gives you a greater throughput gain as well as fine control over what to cache and what not to cache.
With the above terms defined now we’ll move to identifying perfect candidates for caching. I often hear people saying that ASP and IIS are too slow and they always point to sites that take long time to render and leave the user staring at a blank page until the browser has enough HTML to start rendering. ASP and IIS are not slow but what they do suffer is the side effect of there easy to program. ASP generally speaking is a very easy server-side programming framework and with VBScript as the scripting engine the entire process of web development is even easier this ends up leaving the process of performance optimization up to the programmer and that’s where slow sites merge from.
ASP sites are usually very dynamic, list box get generated dynamically, menu options get generated as well as general site content this is not going to end, since people expect more functionality out of sites and always draw comparisons to desktop applications And with newer technologies on the scene like XML and XSLT and cross browser issues remain with us (causing people to use server-side transformation), more load is going to be placed on application servers further adding to ASP/IIS reputation.
Caching is not a simple performance enhancement, to fully take advantage of caching you have to understand your content and place judgment on classifying which is purely dynamic and which is semi dynamic and which is fairly static in order to cache/leave accordingly.
IIS and Caching
IIS 4.0 and 5.0 provides out of the box two kinds of caches
- IIS Template Cache
- IIS Script Engine Cache
To best monitor the performance enhancement these two caches provide refer to the following performance monitor counters
- Active server pages: Template Cached
- Active server pages: Template Cache Hit Rate
- Active server pages: Script Engine Cached
These caches provide performance increases however more is need to accommodate site needs
Current Caching Techniques With ASP
ASP out of the box doesn’t provide any caching and relies on IIS caching, this limitation was overcome by many third party vendors such as Post Point Software XCache and other custom components.
Output caching
With XCache one can create output caching as well as partial caching with the use of XCaches’s SDK and Objects.
Example: <HTML> <BODY> Cached: <%Response.Write(Now())%> <BR> Not Cached:<$Response.Write(Now())$> </BODY> </HTML would be saved to the cache as : <HTML> <BODY> Cached:3/27/00 8:47:40 AM <BR> Not Cached:<%Response.Write(Now())%> </BODY> </HTML> |
The introduction of Post points, <$..$> processing blocks and
<!-- #dynamic virtual=”{file..}”ŕ includes allow output caching and provides you with a greater performance increase.
Data Caching
Data caching is fine control over caching, performed programmatically and deals with the cache of any data (result set from a DB query, XSTL transformation, etc..). there are many components on the web that provide such functionality but in my mind the best is one provided by the Microsoft team of Duwamish introduced in the phase 4 of the implementation. What makes this object so good, for starters it’s free and comes with the ATL COM code written in. Secondly this object addresses all main performance and functionality issues. Event handling is provided for the cache, multiple threading model as well as the concurrency with critical section locking.
Example:
Entries in global.asa
Set Application("Cache") = Server.CreateObject("MSDN_DUWAMISH4Lib.Cache") Application("Cache").Initialize 1000000, 1000 Application("Cache").FlushEnabled = False
' Flush Delta time - 1/2 hour expressed in milliseconds 'Application("Cache").FlushDeltaTime = 0.5 * 60 * 60 * 1000 Application("Cache").RaiseErrors = False
once the above entries have been set (a full documentation is available onine at msdn.Microsoft.com), you can use the object to cache any data
szData = Application("Cache")(szKey)
If szData = "" Then
... build up the data to store, query result XSLT transformation etc..
' add data first time
Application("Cache").Add szKey, szData
Else
' just use the data cached without executing the process
End if
With the above techniques and the many similar available, caching in ASP is possible but is not provided as part of the framework.
Caching with ASP+ .NET
With the introduction of ASP+, caching got a whole lot better. The .NET framework now not only relying on IIS caches, it provides Page/Output caching as well as Data caching out of the box.
Page Caching or Output Caching
To Page Cache in ASP+ you can use the OutputCache Directive
<%@ OutputCache Duration="10" %>
or
the programmatic control of the functionality can be provided by the System.Web.HttpCachePolicy class
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60))
Response.Cache.SetCacheability(HttpCacheability.Public)
Output Caching or Data Caching
To cache arbitrary data in ASP+ you can use System.Web.Caching namespace, caching is a joy with Cache class. To cache data it’s as simple as just using the Cache object.
Private Static cacheSynchronize As String = "Key"
szData = Cache("Key")
If szData <> Null Then
'use data
Else
SyncLock(cacheSynchronize)
' add information to cache
Cache("Key") = szData
End SyncLock
End If
Note: Note extra synchronization has been added to account for concurrency.
Now the great advantage of the above techniques is that no third party tool or software was required to provide you this added functionality.
Finishing Note
Caching is great and it’s something that every programmer should consider, however there are some drawbacks to caching. If implemented at a pre mature stage in the site/Application development, caching can hide a lot of bugs and spring them out when you least expected making things very inconsistent.
Example: the output of a COM object or function that doesn’t free resources can be cached covering the memory leaks that exist.
Caching should be the last thing to implement before production it will increase performance and put a smile on your face as well as give time for bugs to be dealt with.
For extra information you can reach me at
http://salimnaim.webjump.com
developerWorks - FREE Tools! |
The IBM DB2 Deep Compression ROI tool is designed for DBA’s and IT management personnel to perform a clinical analysis of the cost savings gained from the Storage Optimization feature of DB2 9 for Linux, UNIX and Windows. The feature, also known as Deep Compression, compresses data that lies within a database by up to 80% at times. FREE! Go There Now!
|
|
|
|
Hear how IBM Rational Project and Portfolio Management integrated solutions help teams put the right tools and processes in place to maximize the effectiveness and efficiency of project teams and ensure that the business vision is being executed correctly. Learn how to automate and integrate requirements prioritization, top-down project planning, communications and controls, and methodology deployment to keep your scope, costs, and schedules under control. Tackle with an end-to-end approach the management of scope and scope changes, usage of methodology to control and empower project teams, and optimization of resources to align activity costs with the overall project plan. FREE! Go There Now!
|
|
|
|
David Barnes, Lead Evangelist for IBM Emerging Internet Technologies will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve. FREE! Go There Now!
|
|
|
|
WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download the latest trial version of IBM Data Studio V1.1 at no cost. IBM Data Studio is a comprehensive data management solution that helps you effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life cycle utilizing a consistent and integrated user interface. Unlike other client-side data management solutions that focus on only one aspect of the application lifecycle or database administration, Data Studio complements the Rational Software Delivery platform, providing unparalleled flexibility for a heterogeneous data server environment across platforms. FREE! Go There Now!
|
|
|
|
Join us for this web seminar to learn how you can defend your web applications from attack. Learn about the 3 most common web application attacks, including how they occur and what can be done to prevent them. We’ll also discuss manual versus automated approaches for scanning and identifying web application vulnerabilities and how IBM Rational AppScan, an automated vulnerability scanner, can help you automate more of what you are doing manually today. FREE! Go There Now!
|
|
|
|
Portfolio Management is about effectively managing portfolio value by aligning portfolio investments with business goals. This complimentary e-kit provides a collection of materials that can help you understand how IBM Rational enables and automates best practices for improved governance and clear visibility into portfolio and project performance across the entire IT project lifecycle. FREE! Go There Now!
|
|
|
|
Learn how to do more with your reusable assets with the free Rational Asset Manager eKit. The eKit includes demos on how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse. Plus you’ll find white papers and a Webcast that discuss the challenges of a Service Oriented Architecture and how Rational Asset Manager can provide quick and effective solutions. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Method Composer V7.2 which helps you deliver customized yet consistent process guidance to your project teams and IT organization, and includes the latest version of IBM Rational Unified Process (RUP), which has provided process guidance to teams since 1996. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference, to hear how Enterprise Generation Language (EGL) eliminates the need for tedious and error-prone low level coding, so developers can focus on business requirements. EGL extends the Rational software development platform with a simplified programming language that enables developers who have little or no experience with Java, Web technologies or Service Oriented Architecture, to create enterprise-class applications and services quickly and easily. It also allows developers who may have little or no mainframe programming experience to quickly create traditional mainframe components. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |