简体   繁体   English

我应该使用什么而不是会话?

[英]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. 我的想法是在用户登录时将此列表存储在Session中。但我知道会话不是一个好习惯。

I've tried to use the session, cookie and OutputCache in ActionResult but only the session works. 我尝试在ActionResult使用会话,cookie和OutputCache ,但只有会话才有效。

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. 为每个用户使用Session对象。 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). 使用缓存机制 - 如果您在应用程序中实现了一些缓存机制,则可以为每个存储导航菜单的用户创建一个对象(类似Redis,AppFabric或.NET缓存)。

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. 我会在创建/更改配置文件时创建一个存储构建列表的数据库(可能像JSON字符串一样简单吗?),由用户id引用。

Simply then a case of selecting this with a single user id value - a simple data type can get a complex datatype. 只需一个用单个用户id值选择它的情况 - 一个简单的数据类型可以得到一个复杂的数据类型。

Thanks for all help. 谢谢你的帮助。

To solve my issue, I had to use the Session . 为了解决我的问题,我不得不使用Session Why? 为什么? Because each user has his own '(SidebarViewModel)Session["sidebar"]' which contains all specific menus, name, ids and avatar. 因为每个用户都有自己的'(SidebarViewModel)Session [“sidebar”]',其中包含所有特定的菜单,名称,ID和头像。

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. 我曾尝试使用“OutputCache”(我自己的属性类),但由于我的菜单是ChildAction,我无法在注销时清除缓存,对于可以在需要时更改自己的头像的用户来说并不是那么好至。 I also tried to use the Application but it was almost the same case of the Sessions. 我也尝试使用该应用程序,但它几乎与Sessions的情况相同。

PS: I don't like the Sessions, but I think it's the best way for now. PS:我不喜欢Sessions,但我认为这是目前最好的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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