Tech and a few other things RSS 2.0
# Monday, January 31, 2011



Cache.Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemUpdateCallback)

http://msdn.microsoft.com/en-us/library/cc491414.aspx

My new favorite method in .Net cache'ing. Before I explain why I love it I'm going to show you how it works in a very easy way, because that's all you really care about anyways....I know this because I'm the exact same way annnnnnd Google Analytics tells me so by your actions visitors.

The place I like to put this method is in my constructor on my data layer, this way it's called with frequency.

public static class DataLayer
{
static DataLayer()
{
HttpContext.Current.Cache.Insert("AnyStringYouLike",
"AnyStringYouLike",
null,
DateTime.Now.AddMinutes(5),
System.Web.Caching.Cache.NoSlidingExpiration,
new CacheItemUpdateCallback(FunctionYouWantToCall));
}

public static void FunctionYouWantToCall(string aKey,
CacheItemUpdateReason aReason,
out
object anObject,
out CacheDependency dependency,
out DateTime expiration,
out
TimeSpan aTimeSpan)
{

if (DateTime.Now.Day == 22)
{
if (AFeedIMade.GenerateFeed())
{
ErrorHelper.LogError("AFeedIMade", "Success");
}
else
{
ErrorHelper.LogError("AFeedIMade", "Failure");
}
}

dependency = null;
expiration = DateTime.Now.AddMinutes(20);
aTimeSpan = Cache.NoSlidingExpiration;
anObject = "AnyStringYouLike";
}
}



What's happening here?
  1. I insert into cache a delegate that runs a function I specify I would like to run at a specific time. I do this by saying check the cache initially every five minutes, but once it jumps into the function every 20.
  2. Once it's in the function if the day is the 22nd of the month then I run the code to generate my feed, if it's not then I simply set the cache to check it every 20 minutes.

The first question you may ask your self is why don't you set the cache to check the time every 22 days or so. The problem with that is what if you set it back 22 days on the 21st day. Then you miss the day you want to check, the 22nd for that month, and your feed (or whatever you are having the method do) gets delayed by at least one month. Since every month is different in length, it's just not a smart decision. For good measure I've found that having the cache checked every x minutes under 60 minutes is the best way to go. It's less likely for a specified time frame to be missed in the function.

Last note, if by chance you are calling the cache outside of the websites context don't forget you can use:
HttpContext.Current

Like so:
HttpContext.Current.Cache.Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemUpdateCallback)


Monday, January 31, 2011 3:10:50 PM (Central Standard Time, UTC-06:00)  #    Comments [0] - Trackback
.Net | ASP.NET
# Friday, December 10, 2010


Problem:
The project file 'path ' cannot be opened.  The project type is not supported by this installation.

Solution:
The way I corrected this issue of not being able to open a ASP.NET MVC 1.0 project goes like this.
I was using VS 2008

  • Install SP1 for VS 2008
  • Install MVC 2.0 framework
  • I realized I had an MVC 1.0 project and I used this tool to change the project to MVC 2.0.

The tool works great, props to Eilon Lipton never met the guy but I'm down with anyone that will make my life easier.

http://weblogs.asp.net/leftslipper/archive/2009/10/19/migrating-asp-net-mvc-1-0-applications-to-asp-net-mvc-2.aspx


Other Possible Solutions:
Run this command from the command promp:

devenv /ResetSkipPkgs

This will try to load any Visual Studio packages that failed previously at some point, such as the WPF project flavor package, which would cause the error message you're seeing. (1)


Explanation:
Listen folks MVC is a fun style of programming, but unless you are using unit tests, the most compelling reason to use MVC, MVC becomes more of a hindrance than helpful. Now any biased advocate of ASP.NET MVC will most likely tell you different, but I feel I am very unbiased. Don't wanna believe me, Kevin Pang gives a great list (http://www.kevinwilliampang.com/2009/04/21/should-you-use-asp-net-mvc/) of reasons on why or why not to use ASP.NET MVC.

The biggest lesson here, don't just use a technology because you think it's cool, use it because you know it's the right fit for the problem. In this case if you are creating an ASP.NET MVC application, make sure you create unit tests (the first thing asp.net MVC asks you when you create a new project) otherwise you are just using a technology because you wanna and nobody likes those people, especially after they leave the project.


Friday, December 10, 2010 2:03:22 PM (Central Standard Time, UTC-06:00)  #    Comments [0] - Trackback
.Net | ASP.NET | ASP.NET MVC
Navigation
About the author/Disclaimer
        

My name is Ben Coffman. I like to build things: programs, programming teams, programming departments and maybe one day a company with lots of programmers. When I turn the internet off I focus on my family, random hobbies, and sharing moments in life.

Blogs I follow:

1. 2andahalfd.com

2. Jeff Lamarche

3. Scott Hanselman

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

© Ben Coffman

Archive
<February 2012>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
All Content © 2012,

Sign In