Click here to Skip to main content
15,889,651 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
As we know cache is a server side handling state management.How we update cache when some values changes in database.and page get loaded with new data in cache.Please give some solution an c# code also.
Posted
Updated 20-Sep-12 12:41pm
v2

1 solution

Interesting question, with several possible answers.
A reason you would use a caching system is so that you don't have to poll the database on each page request. However sometimes data does change, and your cache will be outdated, and in some applications that can be quite a problem.

If you are using the build-in caching features of ASP.NET you won't have to do much work. The default way to handle this problem is by having your cache clear itself after a certain time, the timeout, the ASP.NET Cache does this by default.
You can read more about it here: http://msdn.microsoft.com/en-us/library/6hbbsfk6(v=vs.100).aspx and here http://msdn.microsoft.com/en-us/library/system.web.caching.cache(v=vs.100).aspx[^]
The trick is to find out what a good timeout is, the answer really depends on the number of database changes per minute, and importance of outdated information. For me 20 minutes of timeout typically works fine, but it really depends on the kind of data you are handling.

If 100% accurate data is really important, there is another way to do it, but it requires the only way to change data in the database is via your application.
If so, you can simply clear the cache each time a user changes the database, this way it will never be outdated. This will however affect your performance, but depending on the scale of the application and the number of changes per minute, this might be acceptable. To remove the items from the cache, simply call .Remove with your key for the database values, see the 2nd link for more information on that.

I hope this helped :-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900