Any way to avoid instantiating a class twice?

Hey everyone, I’m trying to find a work around for a performance problem
I’ve been having.

I have a class that builds paths (among other things) dynamically to
many files and folders depending on what sets of information the user
accesses. Once built, this class instantiation contains all the
information necessary for several pages (controllers/views) to operate.
However at times it can take up to 10 seconds to process.

It could be incredibly time consuming to re-instantiate the class every
time I load any related sub-views/controllers.

The information contained in the instantiation is too large to fit into
a session variable and too complex to be passed back in a query string.
Also I should note that it is different for each user, so it can’t be
used as some sort of program-wide global variable.

Is there any way around this situation? Is there some way to keep the
instantiated class alive so it can be used in subsequent actions? Any
help would greatly be appreciated!

Thanks!

Persist that information to some other storage engine on your server,
per
user. The database is the first place I’d think of. Using database
sessions
removes the session limit.

However, look into Memcached, MongoDB, CouchDB, and other options.
They’re
pretty cool.

Also, maybe have someone else take a look at the code you’re writing. It
may
just be that there’s a better way to do what you’re doing. Something
doesn’t
seem right about what you’re doing, but I’m not here to criticize,
especially if I haven’t even seen your code.

Maybe get a mentor at http://railsmentors.org :slight_smile: It’s a free service,
part
of RailsBridge.

On Thu, Oct 1, 2009 at 12:56 AM, Spencer R. <

Is there any way around this situation? Is there some way to keep the
instantiated class alive so it can be used in subsequent actions?

Sure, for the non user-specific parts, you can lazy-load and cache:

class Expensive

def self.share_me
@some_val ||= expensive_calculation
end

end