Dan Esparza

Laughter. Code. C# awesome sauce.

Flushing the ASP.NET Output Cache Using Code

When looking to speed up content that will be served from an ASP.NET MVC controller, one of the options I evaluate is the built in Output caching mechanism.  Output caching is great because it’s easy to setup, easy to maintain and it can give a big improvement for a small amount of code.

Sometimes, the cached item will need to be flushed (and regenerated / recached).   The built in mechanisms for handling this — using cache expiration, or varying the cached output by one of many parameters — handle most use cases pretty easily.

But other times I need to expire the Output cache programatically, and it’s not entirely obvious how to do this, so I have documented it, here:

//  Get the url for the action method:
var staleItem = Url.Action("Action", "YourController", new
{
    Id = model.Id,
    area = "areaname";
});

//  Remove the item from cache
Response.RemoveOutputCacheItem(staleItem);

Where “Action” and “YourController” are the names of the Action method and controller that have the [OutputCache] attribute. Notice that this example also passes route data (the Id and the area “areaname”) — this is how you’ll need to specify any parameters to the action method to build a complete url in Url.Action.

Note: that you can use this code from any part of your application — you can expire the output cache of one controller/method from a completely different controlller/method.

Also, you’ll need to remember to add the Location=OutputCacheLocation.Server parameter to the OutputCache attribute, like this:

[OutputCache(Location=System.Web.UI.OutputCacheLocation.Server, Duration = 300, VaryByParam = "Id")]

Using MS Onenote to Manage Bug and Release Information

The problem

Interruptions cause a real problem with lost time during software development. Jeff Atwood summarizes this nicely:

Even adding a single project to your workload is profoundly debilitating by Weinberg’s calculation. You lose 20% of your time. By the time you add a third project to the mix, nearly half your time is wasted in task switching.

As a software developer, I’m constantly trying to avoid getting distracted by production incidents, other bugs, and high priority features that need to get implemented. Don’t get me wrong — I think each of those things is a necessary (evil) part of software development.

The solution: taking notes

We’re already using the computer to program, I figure it makes more sense to take notes on the computer (vs pen and paper). After trying to use Evernote and getting frustrated with the lack of a decent print view or formatting options, I chose MS Onenote and haven’t looked back.

Using Wildcards With Git Operations

I’ve been using the awesome ‘git’ source code control system for the past year now. The transition from Subversion to git was prompted mostly by my desire to use the awesome application hosting platform AppHarbor, but I picked up git with ease and haven’t looked back.

One of the things I’ve learned about using git in conjunction with Visual Studio 2010 is that git usually likes to be in control of certain operations. In particular, git likes to be in control of ‘remove’ operations.

Removing a file from git is as simple as running

rm filepath/filename.ext

from the git command line, but when that is a long path and filename, it can get a bit … tedious.

Luckily, there are a few quick tips I can share when using the git commandline:

First, the tab key provides command-line completion, just like in a Linux ‘bash’ prompt.

Second (and perhaps even more awesome) — you can use bash (or even DOS-style) wildcards for the filename!

This means that you can easily use the following syntax:

rm MyApp.Library/Some/Really/Long/Path/MyLongFile*.*

How cool is that!?

What Version of jQuery Is a Site Using?

To quickly find the version of jQuery that a site is using, you can simply type the following into the browser bar of the site:

javascript:alert(jQuery.fn.jquery)

This will get the version of jQuery and display it in a javascript pop-up dialog.

Find Out if 2 Date Ranges Overlap Using Javascript

I recently discovered an ingeniously simple way to see if 2 date ranges overlap using only Javascript:

var e1start = e1.start.getTime();
var e1end = e1.end.getTime();
var e2start = e2.start.getTime();
var e2end = e2.end.getTime();

return (e1start > e2start && e1start < e2end || e2start > e1start && e2start < e1end);

Simple, eh?

Fun With Javascript Pointers

I just spent the last hour trying to debug some Javascript code that wasn’t working the way I expected. It turns out I was dealing with the shallow copying behavior of Javascript. If you deal in Javascript objects regularly, you need to know this information!

Here is a quick example that illustrates what I saw while debugging today:

// First object, with a simple string property
args1 = {};
args1.test1 = "blah";

// Second object, 'created' from the first
args2 = args1;

// As you might expect...
// args2.test1 is "blah"

// Set the property to something different
// on the second object
args2.test1 = "boo!";

// But wait -- the objects are linked!
// args1.test1 is now "boo!" too!

As you can see — the two objects ended up being ‘linked’ to each other due to the nature of how Javascript ‘shallow copies’ work with objects.

Spotting this in your code is the hard part. The way to work around this is simple (as long as you’re using jQuery):

// First object, with a simple string property
args1 = {};
args1.test1 = "blah";

// Second object, deep copied from the first
args3 = $.extend(true, {}, args1)

// As you might expect...
// args3.test1 is "blah"

// Set the property to something different
// on the second object
args3.test1 = "boo!";

// The objects are not linked
// args1.test1 is "blah" 

For more information, see this answer on StackOverflow (by John Resig himself).

Using Remember the Milk as a Desktop Task List

Using the free and excellent service Remember the Milk as your tasklist is great — but sometimes you need a nice simple tasklist you can keep close at hand on your desktop.

If you use Google Chrome as your browser, there is a very easy way to do this.

In Chrome, visit:

http://www.rememberthemilk.com/services/modules/googleig/

(this should bring up your task list in Remember the Milk in a tiny view)

Then in Chrome, select the ‘wrench’ icon, select ‘Tools’, and then select ‘Create Application Shortcuts’. The next screen to come up will ask you where you’d like to launch your tasklist from.

Press ‘create’ and you’re all done!