Data Caching – How to Cache Data That Rarely Changes

asp.net-mvccachingentity-frameworksql server

In my ASP.NET application there is some data that doesn't change often and so there is no point in querying the database to re-check it every time.

In my current situation I am checking user credentials on every request to see if they have access for what they are attempting to do (i.e. view a particular page).

The security information is stored in the database and I'm noticing a performance hit on every page load. The load time goes from 20ms to 200ms because of the database queries.

It seems to me that I should cache the security-related information so that pages are rendered faster (the data can be refreshed when the application restarts), but I don't know how to go about this. I've searched around for caching methods but no solutions I've seen are about what I'm trying to do.

One way is to store the security information as read-only in a static class or persistent repository that is created and loaded on application startup.

Another way is to use the Tracing and Caching Provider Wrappers for Entity Framework, but according to an answer on StackOVerflow called How to make Entity Framework cache some objects user Alex James says

sometimes this approach is overkill

Is there any disadvantage to storing the data in a few read-only variables (memory concerns?) and any clear advantage to using the caching provider?

Best Answer

You want to store a reference in the ASP.NET session state.

http://msdn.microsoft.com/en-us/library/ms178581.ASPX

ASP.NET session state enables you to store and retrieve values for a user as the user navigates ASP.NET pages in a Web application.

Do the database registration first, and then include sufficient pointers to the security validation in the session state. A date for "last security check" may be sufficient, depending on what you're doing.