Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Alan Northam

March 2008 - Posts

  • Practical Implementation - spending some cache.

    Take a look at my previous post for the details on a cache helper method that I like to use.  In this post, we'll be putting it to work (the word for today is J - O - B).  Here's a look at the code for reference.


    Enter the ASP.NET AJAX AutoComplete Extender.  It's a perfect candidate for a little cache help.  I certainly wouldn't want the database being hit every time a user typed the next letter in the autocomplete box.  I apologize for wearing you down with all this talk.  I think I've said too much.  Let's look at the code for autocomplete webservice.


    The webservice takes the first part of the prefixText (minimumPrefixLength should match the value in the autocomplete extender control) and queries the database for all matches.  It stores that result set in cache and then filters the list by prefixText if needed.  Finally it will take the top X (count is specified by the control) and return it to the control.

    If a user was searching for "automobile", after typing a-u-t all possible results would be cache so that each successive letter will not hit the database.  Another database hit will be incurred when a user searches for a different 3 letter prefix. 

    I'm attaching the full sample code along with a Sql Express database that contains some huge word list I found online (to be attached later, technical difficulties).  Enjoy and try not to spend it all in one place.
     

  • Cold, Hard, Cache!

    Don't get me wrong, I really like the Asp.Net Cache (or HttpRuntime.Cache or Uncle Daddy if you want to call it that... you're a little odd aren't you?), but sometimes it just can't be trusted.  I mean, I just gave you (the cache) my precious object a second ago and now you claim you don't have it?  Did you lose it?  Did you sell it on eBay?  Did you pawn it to support your habit?  Please at least tell me you got a decent price for it. 

    One of the great things about the cache is also what makes it so untrustworthy.   Objects stored there are referenced with WeakReference so if your sever starts getting low on memory GC will collect objects from the cache to free some up.  Also take into account that if you put an object in Cache and set it to expire after X minutes, when you go back to retreive it, do you really know how long it's been?

    So what we want is a way to tell the cache "I want this specific object (a yellow Tonka dumptruck, metal, not plastic) and if you don't have it, here are instructions on how to get it".

    Something like this:

    Usage would look like this:

    I call it the code equivalent of "Trust but verify".

  • ADO.NET, Nullable Types, Casting DBNull and You

    I know this may be a little too scary for some of you so I apologize in advance.  Take a deep breath and try to imagine that you can't use NHibernate and you're stuck using ADO.NET (circa 2001, did they even have computers back then?). 

    If you've ever tried to read the value from a column in a datarow, you've probably run into something like this (a quick Google search will bring up page after page of variations):


    Wow, that's ugly. What if I have several nullable columns? What if I have a lot of nullable columns? There has to be a better way.

    Let's suppose I have a class MyClass (original huh?) and it has a nullable DateTime property and a nullable integer property.


    I like a good one line solution.  Of course, you really can't cast DBNull.Value (which would be the value of row["ColumnX"]  if the column is null in the database) as a nullable int (or DateTime?).  (edit: thanks Chris) The cast above is actually failing but casting with "as" doesn't throw an exception and returns a null value which is just what we want.

    "as"... you're my hero...*sniff* 

     

More Posts

Our Sponsors

Proudly Partnered With