在 Rails 的会话中存储对象 [英] Storing Objects in a Session in Rails

查看:44
本文介绍了在 Rails 的会话中存储对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直被教导在会话中存储对象是一个坏主意.相反,应存储 ID,以便在需要时检索记录.

I have always been taught that storing objects in a session was a bad idea. Instead IDs should be stored that retrieve the record when needed.

但是,我有一个应用程序,我怀疑它是此规则的一个例外.我正在构建一个抽认卡应用程序,被测验的单词位于数据库中的一个表中,其架构不会改变.我想将当前正在测验的单词存储在会话中,以便用户可以完成他们开始的地方,以防他们转到单独的页面.

However, I have an application that I wonder is an exception to this rule. I'm building a flashcard application, and the words being quizzed are in a table in the database whose schema doesn't change. I want to store the words currently being quizzed in a session, so a user can finish where they started in case they move on to a separate page.

在这种情况下,是否可以将这些单词作为对象存储在数据库中?如果是这样,为什么?我问的原因是因为测验旨在快速进行,而且我不想浪费一次数据库调用来检索从不改变的记录.然而,也许我不知道大型会议还有其他负面影响.

In this case, is it possible to get away with storing these words as objects in the database? If so, why? The reason I ask is because the quiz is designed to move quickly, and I'd hate to waste a database call on retrieving a record that never changes in the first place. However, perhaps there are other negatives to a large session that I'm not aware of.

*为了记录,我尝试使用 Rails 2.3 中的内置 memcache 方法缓存它,但显然每个项目的最大大小为 1MB.

*For the record, I have tried caching it with the built-in memcache methods in Rails 2.3, but apparently that has a maximum size per item of 1MB.

推荐答案

不在会话中存储对象的主要原因是,如果对象结构发生变化,则会出现异常.考虑以下几点:

The main reason not to store objects in the session is that if the object structure changes, you will get an exception. Consider the following:

class Foo
  attr_accessor :bar
end

class Bar
end

foo = Foo.new
foo.bar = Bar.new
put_in_session(foo)

然后,在项目的后续版本中,您更改 Bar 的名称.您重新启动服务器,并尝试从会话中获取 foo.当它尝试反序列化时,它找不到 Bar 并爆炸.

Then, in a subsequent release of the project, you change Bar's name. You reboot the server, and try to grab foo out of the session. When it tries to deserialize, it fails to find Bar and explodes.

似乎很容易避免这个陷阱,但在实践中,我已经看到它咬了很多人.这只是因为序列化一个对象有时会带来比显而易见的更多的东西(这种事情应该是透明的),除非你对此有严格的规则,否则事情往往会变得混乱.

It might seem like it would be easy to avoid this pitfall, but in practice, I've seen it bite a number of people. This is just because serializing an object can sometimes take more along with it than is immediately apparent (this sort of thing is supposed to be transparent) and unless you have rigorous rules about this, things will tend to get flummoxed up.

它通常不受欢迎的原因是它在 ActiveRecord 中咬人是非常普遍的,因为你的应用程序的结构随着时间的推移而改变是很常见的,并且会话可以在最初一周或更长时间后反序列化已创建.

The reason it's normally frowned upon is that it's extremely common for this to bite people in ActiveRecord, since it's quite common for the structure of your app to shift over time, and sessions can be deserialized a week or longer after they were originally created.

如果您了解所有这些并且愿意投入精力确保您的模型不会改变并且不会序列化任何额外的东西,那么您可能没问题.但要小心:)

If you understand all that and are willing to put in the energy to be sure that your model does not change and is not serializing anything extra, you're probably fine. But be careful :)

这篇关于在 Rails 的会话中存储对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆