2014-09-25 78 views
1

我有一个从类(Popsicles)生成的对象,它正在传递给另一个类(PopsicleProcessor),只有Popsicle对象的ID,这是'保证'(UUID)是独特。如何从PopsicleProcessor类访问Popsicle对象,而不必像传递整个Popsicle对象那样做一些不好的事情?Ruby/Rails通过ID查找对象

我的应用程序没有数据库来查找这些对象,它们纯粹存在于应用程序的内存中,直到处理完成。

我真的不知道如何实现这一点,特别是因为它被卸载到Resqueue队列上,它将再次启动应用程序的整个实例 - 因此传递对象本身会导致巨大的性能损失(缩放) ,从我所知道的。

一般来说,这是怎么做的(因为Resqueue根本不应该有所作为)。

我想能够从另一个类访问对象实例变量和方法。

下面是我现在正在做的事情,这是行不通的。

popsicles.rb

class Popsicles 

    require "securerandom" 

    @someID 


    def initialize params 

     @someID = SecureRandom.uuid 

    end 

    def doSomething 

     Resque.enqueue(popsicleProcessor, @someID) 

    end 

end 

popsicles_processor.rb

# Worker class 
class PopsiclesProcessor 

    # Requires a @queue instance variable 
    @queue = :popsicle_queue 

    # Needs to take same argument we passed to enqueue method 
    def self.perform(popsicle_id) 

    # Get the object somehow 

    # Now we can call other methods like changeFlavour, or whatever 

    end 

end 

回答

2

,你的工人在不同的进程中运行的事实,实际上是相关的,因为这将有自己的记忆没有访问主应用程序进程中创建的Popsicle对象的空间。如果您只想通过另一个进程中的ID加载它们,您需要有一些方法来保存Popsicle对象。如果不能使用数据库,则必须序列化对象或将其数据的各个组件传递给工作者,以便可以在工作进程中重新构建它。