简体   繁体   English

如何存储会话对象?

[英]How do I go about storing session objects?

I have a few a few model classes such as a user class which is passed a dictionary, and wraps it providing various methods, some of which communicate with the database when a value needs to be changed. 我有一些模型类,例如传递字典的用户类,并包装它提供各种方法,其中一些方法在需要更改值时与数据库通信。 The dictionary itself is made from an sqlalchemy RowProxy, so all its keys are actually attribute names taken directly from the sql user table. 字典本身是由sqlalchemy RowProxy构成的,因此它的所有键实际上都是直接从sql用户表中获取的属性名。 (attributes include user_id, username, email, passwd, etc) (属性包括user_id,用户名,电子邮件,密码等)

If a user is logged in, should I simply save this dictionary to a redis key value store, and simply call a new user object when needed and pass it this dictionary from redis(which should be faster than only saving a user id in a session and loading the values again from the db based on that user_id)? 如果用户已登录,我是否应该将此字典保存到redis键值存储,并在需要时简单地调用新的用户对象并从redis传递此字典(这应该比仅在会话中保存用户ID更快)并根据该user_id从db再次加载值?

Or should I somehow serialize the entire object and save it in redis? 或者我应该以某种方式序列化整个对象并将其保存在redis中? I'd appreciate any alternate methods of managing model and session objects that any of you feel would be better as well. 我很感激任何管理模型和会话对象的替代方法,你们认为这些方法也会更好。

In case anyone is wondering I'm only using the sqlalchemy expression language, and not the orm. 如果有人想知道我只使用sqlalchemy表达式语言,而不是orm。 I'm using the model classes as interfaces, and coding against those. 我正在使用模型类作为接口,并对其进行编码。

Unless you're being really careful, serializing the entire object into redis is going to cause problems. 除非你非常小心,否则将整个对象序列化为redis会导致问题。 You're effectively treating it like a cache, so you have to be careful that those values are expired if the user changes something about themselves. 您有效地将其视为缓存,因此如果用户更改了自己的内容,您必须小心这些值已过期。 You also have to make sure that all of the values are serializable (likely via pickle). 您还必须确保所有值都可序列化(可能通过pickle)。 You didn't specify whether this is a premature optimization so I'm going to say that it probably is and recommend that you just track the user id and reload his information when you need it from your database. 您没有指定这是否是一个过早的优化,所以我要说它可能是并且建议您只需跟踪用户ID并在需要时从数据库重新加载其信息。

+1 on Michael's answer. 迈克尔答案+1。

i'd also note a few things- 我还要注意一些事情 -

if you do decide to cache this data, you should treat it as a read-only store. 如果您决定缓存此数据,则应将其视为只读存储。 you should never update the database based on this. 你永远不应该基于此更新数据库。

i have my projects split into two logical sections: 我的项目分为两个逻辑部分:

/account - heavy write operations , data must be current /everything-else - mostly read operations , data should be current / account - 重写操作,数据必须是当前/一切 - 否则 - 主要是读操作,数据应该是最新的

everything in /account always hits the DB for the most up-to-date version. / account中的所有内容总是会在数据库中找到最新版本。 the rest of the site can hit the db or a cache - i don't care. 网站的其余部分可以打到数据库或缓存 - 我不在乎。

for your described needs, and using the expression language, you shouldn't need to create objects from cache data to do any of your operations. 对于您描述的需求,并使用表达式语言,您不需要从缓存数据创建对象来执行任何操作。

http://docs.sqlalchemy.org/en/rel_0_7/core/tutorial.html#inserts-and-updates http://docs.sqlalchemy.org/en/rel_0_7/core/tutorial.html#inserts-and-updates

>>> conn.execute(users.update(). >>> conn.execute(users.update()。

... where(users.c.name=='jack'). ... where(users.c.name =='jack')。

... values(name='ed') ... values(name ='ed')

... ) ...)

why not just do: 为什么不这样做:

where(users.c.id==cached_value) 其中(users.c.id == cached_value)

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

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