Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 962171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:27:46+00:00 2026-05-16T01:27:46+00:00

I’m trying to build something (ultimately a gem but for now an application) that

  • 0

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.

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).

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.

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!

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:27:47+00:00Added an answer on May 16, 2026 at 1:27 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.