我有一个从类(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