Saving / caching data between requests - general approach

I am developing an Asp.net application (MVC, but that doesn't really matter). I have a custom IHttpModule that is responsible for PostAuthenticateRequest to change the principal and identity of the user.

I save the UserID and UserName in the authentication cookie when the user logs in. I have IUser (implemented at the DAO and Business Objects level, each with its additional members), which I need in all classes of Business Service. When the user wants something, I have to provide an instance of the IUser object (usually from the Business Objects level), so it’s not enough to specify the identifier from the auth ticket.

So, I’m thinking about how and where it would be better to save IUser user data?

  • I do not want to retrieve it every time from the database (based on UserID credentials)
  • I cannot save it in a session, since I have to work inside PostAuthenticateRequest, where Session is not ready yet
  • I want all functionality to be encapsulated in a custom IHttpModule

The choice I see:

  • Cache
  • Cookie
  • (session) - by switching from PostAuthenticateRequest to the PostAcquireRequestState event and changing the main / personal character there, but I would like to avoid this.

Processes that seem to complicate:

  • User inputs, user data are extracted from the database and somehow saved for subsequent requests
  • User outputs, user data should be deleted from the saved environment automatically
  • ,

HttpModule ( ), , reset .

, , / / . .

  • ?
  • SO ?
+5
1

, , cookie Http Cache (HttpContext.Current.Cache).

, "UserCache". HttpModule ( ...) , , , http. , , HttpContext.Current.Cache. ​​ .

, , , .

public class UserCache
{
  public IUser GetUser(object userKey)
  {
    return HttpContext.Current.Cache[userKey];
  }

  public void AddUser(object userKey, IUser user)
  {
    /* this could pull the key from the user object as well. */
    HttpContext.Current.Cache.Add(/* add the object with key and a sliding expiration that is slightly greater than session timeout */);
  }

  public void ExpireUser(object userKey)
  {
    HttpContext.Current.Cache.Remove(userKey);
  }

  /* If you don't want to do SQL cache dependency */
  public void UpdateUser(object userKey, IUser user)
  {
    HttpContext.Current.Cache.Insert(/* ... */);
  }
}

( , DI, ), , . , SQL-, .

. .

HttpModule, , - EndRequest, , , cookie, , , . , MSDN WAY 1,1 , , .

SO , , , (http://highscalability.com/stack-overflow-architecture).

+4

All Articles