Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 1 down vote accepted

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.

share|improve this answer

Session or Application objects are a good fit for your requirements. These objects can be loaded in your global.asax

Put them in application onstart event handler:

Sub Application_OnStart
  application("vartime")=""
  application("users")=1
End Sub

Restarting your application in IIS will reload your session or application wide vars.

share|improve this answer

Caching depends on the type of information, their volatility and their scope. There are data that never or rarely change, data that change frequently, and data that are not meant for caching because they are changing all the time. To cache all these types of information there are two possible ways: to cache in the user session (only to single user), and to cache in application state (global to all users). Caching with these methods can be performed "on-first use" which gets data on first request and caches it for subsequent use, or "pro-actively" with a singleton on different thread that gets data in parallel and caches it for any caller which is definitely give a great boost for application performance.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.