Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear the cache at a specific time in ASP.NET 2.0

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);
like image 997
Jack Marchetti Avatar asked Dec 08 '09 15:12

Jack Marchetti


People also ask

How do I cache data in .NET Core?

ASP.NET Core supports several different caches. The simplest cache is based on the IMemoryCache. IMemoryCache represents a cache stored in the memory of the web server. Apps running on a server farm (multiple servers) should ensure sessions are sticky when using the in-memory cache.


2 Answers

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.

like image 172
RickNZ Avatar answered Sep 30 '22 20:09

RickNZ


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);
like image 33
Robin Day Avatar answered Sep 30 '22 21:09

Robin Day