views:

495

answers:

2

Hi,

I am developing a web app using ASP.NET 2.0 (C#), where on home page I am displaying recently added records. Adding of records frequency is around 1-5 records per day, so I decided not to put much overhead on the sql server by fetching recent records every time from db server.

So, To make the data cached I have used XML files, I have generated the XML file from dataset, (ds.WriteXML function in .NET), now lets say today (10 Jan 2008 12:30:00) I have created a file recent-cache.xml is created. So, the recent cache file is valid for one day.

Then if the difference between current date and last modified date is greater than or equal 1 day then cache xml file must be generated again, with the new data from the db server.

So, I want the code using which I can get the last modfied date of the xml file and then find the difference between both (current and file last modified-date) dates.

And also please tell me what I thought is the better solution, or we can do anything else, some other easy and speedy technique.

Thanks

A: 
double daysDiff = (DateTime.Now - (new FileInfo(path).LastWriteTime)).TotalDays;
xcud
+1  A: 

You might consider using the ASP.NET Cache API, which exists to do the sort of job you're describing. You can add any object (like an XmlDocument, or a DataSet) to the Cache collection and specify how long you want it in there like so:

Cache.Insert("MyCacheKey", myObjectToCache, null, DateTime.Now.AddDays(1), null);

Then you could get at your cached data with a function like this:

const string CACHE_KEY = "MyCacheKey";

private DataSet RecentlyAdded()
{
    if(Cache[CACHE_KEY] == null)
        Cache.Insert(CACHE_KEY, GetRecentlyAddedFromDatabase(), null, DateTime.Now.AddDays(1), null);
    return Cache[CACHE_KEY];
}

The caching API has lots of other neato features but this would accomplish what you want without having to roll your own file-based solution.

Note that if your app shuts down before the day is up, the Cache will shut down with it, and the "recently added" database query will have to run again the next time the data is requested.

edit: changed cache key to a string constant so it only has to be specified once.

Barry Fandango
Hi Barry, very good trick. But I am afraid, that Is caching data using the way you suggested will put load on my web server (I am on shared hosting with limited bandwidth)? OR it will take consumes the same resources as my FILE based method will do?
Prashant
One more thisn, "edit: for nicer code, consider using a string constant for the key." I am not getting this line clearly, Can you please explain? Thanks!
Prashant
Hi Prashant,The ASP.NET Cache will probably hold the item in memory, so if you have a dataset that is hundreds of megabytes or something you might want to go to disk instead to save memory. I think the item have to be very large though, to justify that.
Barry Fandango
- edited to show string constant example.
Barry Fandango
A bit more information from your first question: switching from file to cache doesn't have anything to do with bandwidth, it's all happening inside the server.
Barry Fandango
Thanks Barry, Its really helpful. Thanks again :)
Prashant