Denny.NET

I can haz ASP.NET goodness?

About the author

Denny Ferrassoli
Developer at Casting Networks. MCP / .NET
E-mail me Send mail
Add to Technorati Favorites

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

How to use the CacheItemRemovedCallBack

.NET provides a callback method when a cached item is being removed from memory. Knowing when your cached item is removed is a good way to help manage caching performance. Below we'll take a look at how to implement the callback function that notifies us when a cached object is removed. We'll then display which cached object was removed along with the reason.

First add a label: lblInfo and a button: btnRemove to your web form.

Now move to the code-behind. First import the System.Web.Caching namespace.

In your Page_Load add the following:

    Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            Dim evtRemove As CacheItemRemovedCallback
            evtRemove = New CacheItemRemovedCallback(AddressOf RemoveCallback)

            ' Add our new cached object, notice the onRemove callback
            Cache.Add("CacheMe", _
                "1 + 2 = 3", _
                Nothing, _
                Caching.Cache.NoAbsoluteExpiration, _
                System.TimeSpan.FromSeconds(5), _
                Caching.CacheItemPriority.Default, _
                evtRemove)
        End If
    End Sub

What we do there is create the CacheItemRemovedCallback which points to our RemoveCallBack method (yet to be created) which will handle an event when the cached item is removed. We then add our cache object as normal but we pass our CacheItemRemovedCallback object. We set the expiration for 5 seconds for the sake of testing, assuming you'll be playing around with the code.

Next create the RemoveCallback method:

    Protected Sub RemoveCallback(ByVal key As String, ByVal value As Object, ByVal reason As System.Web.Caching.CacheItemRemovedReason)
        Dim nfo as String = "Cache item: [" & key & "] <br />value: [" & value.ToString() & "] <br />was [" & reason.ToString() & "]"
        lblInfo.Text = nfo.ToString()
    End Sub

The RemoveCallback returns a key (the name of the cached object), a value (the value of the cached object), and a reason (DependencyChanged, Expired, Removed, UnderUsed). Then we create a string to show what and why the cached object was removed by using the above values respectively.

Finally add the button that removes the cached object:

    Protected Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        ' Remove the cached item
        Cache.Remove("CacheMe")
    End Sub

The button's execution causes the cached item to be removed which in turn fires our callback function that we created above (RemoveCallback).

That's all there is to it. Now compile and run your app and play with the button. Also try letting the cache expire and refresh the page to see the different reasons for removing the cached object.

Update (5/29/2007 9:45AM): Some great advice from McGurk from the comments section of this post. I'm going to post the comments here because it's important to remember.

Whenever talking about cache and the CacheItemRemovedCallback it is always wise to remind people that you should NEVER re-add the item removed in this callback. Doing so can cause your application to bring down the worker process. Often this is because the item is being removed due to memory pressure, and re-adding the item will result in the cache immediately removing the item, which will result in the callback being called again. You can see the nasty loop you can get into.

For critical items set your priority on the item to an appropriate level. Use a pattern where you attempt to retrieve an item from the cache, and if it is not present retrieve it from storage (file, database, etc.), re-insert it in the cache and then return it to callers.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:
Categories: ASP.NET | Server-Side
Posted by SuperGhost on Tuesday, May 22, 2007 9:00 PM
Permalink | Comments (4) | Post RSSRSS comment feed

Tips: Debugging JavaScript

I've finally been making headway on my project. It's been slow and I haven't been able to devote much time to it recently but it is coming along and it looks sweet!

Anyways, I read a recent post on Mads Kristensen's blog about debugging JavaScript in Visual Studio 2005. I've been debugging JS in the method described on his blog for quite some time now and thought I would share some tips I picked up on.

Tip #1: Debugging inside an iterator
The problem is I was using Prototype 1.5.0's extensibility to work with my arrays. There are quite a few iterators that were introduced in Prototype including (to name a few) .each and .find. These two iterators allow you to iterate through each item in your array. Here is a quick example of how these functions are used:

[1,2,3,4,5].each(function(w) {  // This will show 5 alert boxes
        alert( w );
    }
);


[1,2,3,4,5].find(function(w) {  // This will return 3
        return w == 3;
    }
);

Placing a breakpoint on the alert( w ); line actually breaks on the entire function. So you can't get into the iterator to see where your issue is. Instead you'll get an error message further down the line in Prototype's method .each. So an easy way to break into the function and see what's happening every iteration is to use debugger; in your JS like so:

[1,2,3,4,5].each(function(w) {  // This will show 5 alert boxes, breaking before each alert
        debugger;
        alert( w );
    }
);


Tip #2: Quick Watch, Add Watch
Yes! You can use Visual Studio's Watch windows to see your JS values, methods, properties, etc... Just break into the debugger and highlight your variable, right-click and select either Add Watch or Quick Watch (or you can just hover over the variable for a second or two). You can then see all methods, properties, values and etc belonging to that object. This helps tremendously when that hard to find JS bug comes up.


Tip #3: AJAX and Web Services
Finally, you can debug your AJAX Web Service calls too! Just use the same methods mentioned above to debug JS and you can step into, over and through all of your JS code, including your ScriptResource.axd and Web Service scripts!

kick it on DotNetKicks.com

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Client-Side | ASP.NET
Posted by SuperGhost on Wednesday, May 09, 2007 9:00 PM
Permalink | Comments (1) | Post RSSRSS comment feed

Install: Windows Vista Business

Just got back from a short, but very much needed, vacation. I went fishing at a nearby lake and it was sooo relaxing! This was the first actual vacation I have ever taken from work. I didn't catch any fish this time so the score is Fish: 1, Denny: 0. We'll see how it goes next time around ;)

I received my free copy of Windows Vista Business last week (powertogether.com ~ no more freebies available sorry). Although I had no intentions of installing it yet I ended up trying to upgrade my BIOS and destroyed my XP boot configuration. Destroying the boot config gave me an excuse to do a fresh install of Vista. I had some second thoughts about getting Visual Studio, SQL Server, IIS and my other apps to run on Vista but figured it would pan out.

After a quick installation (I was surprised) Vista was installed and on my first boot I had a blank screen. After a few reboots I realized I was running a dual screen setup and that the Vista startup screen was actually showing on my second monitor, which at the time was off (silly me). So after feeling dumb I completed the setup and it booted right up. The nVidia FX5500 graphics card held out for the Vista Aero interface though I will definitely be upgrading to an 8800. I had no problems installing Visual Studio 2005 or IIS though I did run across a few snags with SQL Server 2005.

I believe SQL Server 2005 kept killing my BOOTMGR and Vista would then need to be repaired from the CD after a reboot. I had to download the SQL Server 2005 service pack 2 (don't use the Windows Update method!). After trying a few Windows Updates on SQL Server 2005 I decided to manually download SP2 and install it which worked perfectly. I had to create a new instance because apparently the default instance was already used (not sure why).

Either way everything works great now and I run Visual Studio 2005 in Administrator mode for debugging purposes. Also VS2005 seems to have some graphics glitches after debugging though this may just be my video card. You have to install IIS7 from Windows Features but IIS7 rocks! I noticed a lot of my apps installed much quicker in Vista than in XP. It may be better support for my dual core or perhaps XP was just bloated. The Vista SideBar comes in very handy and there are quite a few gadgets available for it.

Overall I like Vista a lot more than I originally thought I would.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,
Categories: General
Posted by SuperGhost on Tuesday, May 08, 2007 9:00 PM
Permalink | Comments (4) | Post RSSRSS comment feed