ASP Code
  Home arrow ASP Code arrow Caching with ASP.NET and ASP by Salim Naim
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP CODE

Caching with ASP.NET and ASP by Salim Naim
By: aspfree
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 7
    2001-02-07

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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

     

     

     


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More ASP Code Articles
    More By aspfree

     

    IBM® developerWorks developerWorks - FREE Tools!


    Check out the new Jazz space on developerWorks

    <a href="http://zeus.developershed.com/shonuff.php?blackbird=3853&zoneid=442&source=&dest=http%3A%2F%2Fwww.ibm.com%2Fdeveloperworks%2Fspaces%2Fjazz%3FS_TACT%3D105AGY31%26S_CMP%3DDEVSHED&ismap="><img src="http://images.devshed.com/corp/img/news/jazz01.gif" alt="developerWorks Jazz space" align="left"></a>You've heard the buzz about Jazz... want to know more about it from a developer's perspective? Check out the Jazz space on developerWorks. This space is an up-to-date resource for developers, including technical information about Jazz and products built on Jazz, like Rational Team Concert Express. The Jazz space includes content from a wide variety of sources, including links, feeds, and comments from experts.
    FREE! Go There Now!


    NEW! Calling all CC Power Users – and those that would like to be!

    Join this Rational Talks to You teleconference, featuring Paul Boustany and Mark Krasovich, to speak to the experts about becoming a Rational ClearCase power user. Get a chance to ask your questions and learn tips and tricks for using Rational ClearCase in Agile development
    FREE! Go There Now!


    NEW! Application Development Tools for the Mainframe Developer

    You probably have thousands of lines of COBOL code loaded with business intelligence and being used to run your business, along with an army of developers maintaining these applications. Learn how to prepare your applications and developers so you can keep that competitive edge and move to a service-oriented architecture with the IBM Rational Enterprise Modernization solutions. Replay is available for 9 months.
    FREE! Go There Now!


    NEW! BlammoSplat: Build a community Web site of OpenLaszlo animations, Part 3: The community animation

    Learn to enable users to both rate existing animations and to combine existing animations into new snippets. This is the third in a series of three tutorials that chronicle the building of a site that enables collaborative discussion and animation building using Domino and OpenLaszlo.
    FREE! Go There Now!


    NEW! Did you say mainframe? e-kit

    Learn how you can extend modern application lifecycle management to IBM System z through the IBM Rational Software Delivery Platform (SDP). The Did you say mainframe? e-kit includes podcasts, webcasts, tutorials, white and red papers, demos, and articles designed to help ease the challenges of modernizing your enterprise. This complimentary kit for mainframe developers is a practical, how-to guide for making the most of an existing development environment, including the skills and infrastructure already in place at an established enterprise.
    FREE! Go There Now!


    NEW! Download a free trial of Lotus Quickr 8.0

    Visit IBM developerWorks to download a free trial version of Lotus Quickr 8.0, which enables collaboration by transforming the way everyday business content such as documents, rich media, photos, and video can be shared. Lotus Quickr makes it faster and easier to share content of all types (not just documents) within virtual teams. It is designed to make it easier to collaborate across organizational boundaries, while continuing to work within the context of familiar desktop applications.
    FREE! Go There Now!


    NEW! Trial download: IBM Rational Functional Tester V7.0.1

    Get a free trial download of the latest version of IBM Rational Functional Tester V7.0.1. Rational Functional Tester is an automated functional and regression testing solution for QA teams concerned with the quality of their Java, Microsoft Visual Studio .NET, and Web-based applications.
    FREE! Go There Now!


    NEW! Trial download: IBM Rational Manual Tester V7.0.1

    Try the latest version of IBM Rational Manual Tester V7.0.1 by downloading a free trial from IBM developerWorks. This manual test authoring and execution tool promotes test step reuse to reduce the impact of software change on testers and business analysts and addresses the needs of teams performing at least a portion of their testing manually.
    FREE! Go There Now!


    NEW! Webcast: Calling All Testers! Find Application Vulnerabilities Early in the Development Process Where they are Easier to Fix and Less Risky to your Business

    In this webcast, IBM Rational will discuss the importance of Web application security and will share techniques and best practices to introduce application security testing into current QA processes including: understanding common security vulnerabilities and techniques to integrate security testing with defect tracking and remediation systems in an effort to safeguard sensitive online information.
    FREE! Go There Now!


    NEW! Webcast: Eclipse: Empowering the universal platform

    The Eclipse community is constantly working to extend Eclipse's functionality. In this webcast, learn about some of the most important and feature-rich projects under development. From multi-language support to plug-in development, tune in to see what Eclipse is capable of now.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    ASP CODE ARTICLES

    - ASP Forms
    - ASP: The Beginning
    - Getting Remote Files With ASP Continued
    - Inbox and Outbox Manipulation in ASP
    - Relational DropDownList Using VB.NET
    - Ad Tracking URL Hits
    - Use ViewState to display one record per page...
    - Send Email using ASP.NET formatted in HTML
    - ASP File Explorer
    - ASP/XML Interview questions by Srivatsan Sri...
    - Various methods of setting Date values to a ...
    - Conditional DataGrid Item and using checkbox...
    - Fill .NET Listbox with SQL DataReader
    - Filling Dropdown box using Code-Behinds in C#
    - FLAMES code sample written in .NET What is F...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek