views:

196

answers:

3

So I have a process which runs at midnight which sets a start and end point for a flash object. This only needs to run once a day, so I'm obviously caching the result set.

However, the problem I'm running into is, if the data is still cached after midnite, it's not pulling in the most correct data, until the cache expires.

I basically need the cache to expire at 11:59:59PM, so that at 12:00am it gets the correct data.

I'm guessing a SQL Cache Dependency on the table I'm pulling the data from would be ideal, however I have never set that up before.

Is there a way to tell the cache to remove a specific item at exactly midnite?

Thanks guys!

--Absolute Expiration---

I think I got it:

DateTime expireWeights = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59, 999);
Cache.Insert("CacheItemName", itemToCache, null, expireWeights, System.Web.Caching.Cache.NoSlidingExpiration);
+1  A: 

If you look here, under "Time-based Dependency", it shows how to set the item to expire at a specific time. Is that what you're looking for?

Steven Sudit
Not really. Time based dependencies aren't absolute. I can't tell it to clear at exactly Midnite, I can only say "clear it in 10 minutes, or 2 hours" right?
Jack Marchetti
A better link would be: http://msdn.microsoft.com/en-us/library/4y13wyk9.aspx - however you'll have to work out the correct DateTime each time you insert the item, Today's date, time of 23:59:59, and ensure that you set the TimeSpan to `Cache.NoSlidingExpiration`
Zhaph - Ben Duguid
@JackM: Ok, but if you know the current time, you can trivially calculate how many minutes until midnight. Also, what Zhaph said about NoSlidingExpiration is exactly right.
Steven Sudit
+4  A: 

You can set an absoluteExpiration time on the Cache object, which is a DateTime.

You can also combine an absoluteExpiration with a SqlCacheDependency.

Regarding the issue of it not pulling the new data when the cache expires: you can wire up a CacheItemRemovedCallback to receive notification of when it expires, and refresh the cache at that time.

RickNZ
so something like this? DateTime expireWeights = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59, 999); Cache.Insert("CacheItemName", list, null, expireWeights, System.Web.Caching.Cache.NoSlidingExpiration);
Jack Marchetti
Sure, except I would use UtcNow instead of Now and I would use a single instance of it instead of creating three different ones.
RickNZ
+1  A: 

I don't understand why you have a problem with absolute expiration? You can indicate the exact datetime in which the item will expire from the cache. Therefore, the following line of code will insert "myObject" into the cache under the key "MyKey" and will expire at midnight on the next day (Regardless of when it enteres the cache).

Cache.Insert("MyKey", myObject, null, DateTime.Today.AddDays(1), TimeSpan.Zero);
Robin Day
I guess my confusion was if you are adding days, if it was entered at 1pm, it would expire the next day at 1pm
Jack Marchetti
Yes, if you add Days to DateTime.Now then yes it will, but if you add Days to DateTime.Today it will already have stripped the time off.
Robin Day
Or maybe DateTime.Today.AddSeconds(86399) to get 23:59:59? Dunno. Seems like pulling the current month day and year from DateTime.UtcNow might be a little cleaner. Really the same end result, though.
RickNZ
Also, if you need it to expire at 23:59:59.999 rather than midnight the next day then you could use System.DateTime.Today.AddDays(1).AddMilliseconds(-1) I find it a lot "nicer" than building a date using year, month, day, hour, minute, second, millisecond as you have it in your edits.
Robin Day
One more option... if it needs to be in UTC then you can always use System.DateTime.UtcNow.Date.AddDays(1).AddMilliseconds(-1)
Robin Day