简体   繁体   中英

What should I use instead of session?

I'm working on a project which I need to build a list of menus according with the profile of the logged user. But, currently I have to build this list each time the page is loaded and this is not too good for my project.

What I want to do is get this list only when the user logs in and use it each time that I need it. My idea is store this list in the Session when the user logs in. But I know the session isn't a good practice.

I've tried to use the session, cookie and OutputCache in ActionResult but only the session works.

Is there a way to store my list in a "global variable", or something like that, and use it always I need it?

I can suggest two options for you problem.

  1. Use Session object for each user. or
  2. Use Caching mechanism - If you have implemented some caching mechanism in you application, you can create one object per one user storing the navigation menu(Something like Redis, AppFabric or .NET caching).

Object related to the menu should be create only one time for the user(You can create it during the login).

I would create a database storing the built list when profiles are created/changed (could be something as simple as a JSON string?) referenced by the user id.

Simply then a case of selecting this with a single user id value - a simple data type can get a complex datatype.

Thanks for all help.

To solve my issue, I had to use the Session . Why? Because each user has his own '(SidebarViewModel)Session["sidebar"]' which contains all specific menus, name, ids and avatar.

I've tried to use the "OutputCache" (My own attribute class), but as my menu is a ChildAction I couldn't clear my cache on logoff and it's not so good to the user who can change his own avatar when he wants to. I also tried to use the Application but it was almost the same case of the Sessions.

PS: I don't like the Sessions, but I think it's the best way for now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM