在Ruby on Rails中,如何在会话之间将对象保留在内存中 [英] In Ruby on Rails how can I persist objects in memory between sessions

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

问题描述

我正在尝试构建如下工作的东西(最终是gem,但现在是一个应用程序).

I'm trying to build something (ultimately a gem but for now an application) that works as follows.

例如,假设数据库记录是狗的品种.每个品种都有一个狗父母班和一个孩子班.直到运行时才知道实际的品种.

Suppose for example the DB records are breeds of dog. There's a Dog parent class and a child class for each breed. The actual breeds aren't known until runtime.

服务器启动时,它将从数据库加载记录,并根据记录实例化类的实例,例如我可能有两只小猎犬和贵宾犬.当有人来到服务器时,他们可能想要访问这些狗实例之一.

When the server begins it will load up records from the DB and instantiate instances of classes based on the records, e.g. I may have two beagles and poodle. When someone comes to the server they may want to access one of those dog instances.

为什么不只是动态创建实例?就我而言,狗"基本上是持有算法和数据的类.该算法不会改变,数据很少会更改(以天为单位),但是算法本身的执行(将同时使用数据和一些动态数据(例如时间戳))将每秒访问多次.

Why not just create the instance on the fly? In my case the "dogs" are basically classes that hold an algorithm and data. The algorithm doesn't change, the data changes rarely (on the order of days), but the execution of the algorithm itself, which uses both data and some dynamic data passed in such as a timestamp, will be accessed multiple times a second.

每次仅执行一个请求而仅在下一个请求上再次执行该请求时,必须重新创建该对象的实例并加载数据,这是很愚蠢的(请求不会更改对象的状态).当我可以重复使用同一对象时,我会每秒创建和销毁多个对象.

It would be silly to have to recreate an instance of the object and load the data each time just to do a request only to do it again on the next request (the requests don't change the state of the object). I'd be creating and destroying multiple objects a second when I could just reuse the same object.

将其保留在会话中没有意义,因为想要长卷毛狗的人不需要在会话对象中拥有beagles信息;这无关紧要(并且不会扩展).

it doesn't make sense to keep it in the session, since someone wanting a poodle shouldn't need to have the beagles information in her session object; it's irrelevant (and doesn't scale).

如何将这些对象保留在内存中?我基本上想要一个查找表来保存实例.在Java中,我将使用位于内存中的某种类型的哈希图或数组创建单例.在Rails中,我通过在lib文件夹中创建一个singleton类来尝试此操作.我认为-我可能没有理解这一权利-当会话消失时,实例(它是一个单例的事实是没有根据的)丢失了.

How do I persist these objects in memory? I basically want a lookup table to hold the instances. In Java I would create a singleton with some type of hashmap or array that sits in memory. In rails I tried this by creating a singleton class in the lib folder. I think--I may not be understanding this right--that the instance (the fact that it's a singleton is moot) is being lost when the session disappears.

我找到的最接近的答案是 http://www.ruby-forum.com/topic/129372 基本上将所有内容都放在类字段和方法中.以某种方式似乎不正确.

The closest answer I found was http://www.ruby-forum.com/topic/129372 which basically puts everything in class fields and methods. Somehow that doesn't seem right.

TIA!

补充:我来自Java.在Java中,我只创建一个位于堆上或JNDI树中的对象,随着HTTP请求的到来,它们将由servlet或EJB或每个请求项处理,然后可以访问持久对象.我似乎找不到对应的滑轨.

Addition: I come from Java. In Java I'd just create an object that sits on the heap or maybe in a JNDI tree and as HTTP requests came in they'd be handled by a a servlet or EJB or some per-request item which could then access the persistent object. I can't seem to find the equivalent in rails.

推荐答案

也许您的示例在其简单性方面令人困惑.我认为您的对象非常复杂,并且您的基准测试表明,对每个请求进行构造都是不合理的.

Maybe your example is confusing in its simplicity. I assume your objects are quite complicated and that your benchmarking shows that constructing them is unreasonable to be done on each request.

在生产模式下,不会在请求之间卸载类,但会卸载这些类的实例.因此,使用班级里的班级成员听起来就像去我这里的方式.只需使用它来存储您的实例.

In production mode, classes are not unloaded between requests, but instances of those classes are. So using class members of a class sounds like the way to go to me. Just use it to store your instances.

class ObjectCache
  @@objects = {:beagle => Beagle.new, :poodle => Poodle.new}

  def lookup key
    @@objects[key.to_sym]
  end
end

这篇关于在Ruby on Rails中,如何在会话之间将对象保留在内存中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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