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

  • Home
  • SEARCH
  • 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 54703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:15:42+00:00 2026-05-10T17:15:42+00:00

I have a tree of active record objects, something like: class Part < ActiveRecord::Base

  • 0

I have a tree of active record objects, something like:

class Part < ActiveRecord::Base   has_many :sub_parts, :class_name => 'Part'    def complicated_calculation     if sub_parts.size > 0       return self.sub_parts.inject(0){ |sum, current| sum + current.complicated_calculation }     else       sleep(1)       return rand(10000)     end   end  end 

It is too costly to recalculate the complicated_calculation each time. So, I need a way to cache the value. However, if any part is changed, it needs to invalidate its cache and the cache of its parent, and grandparent, etc.

As a rough draft, I created a column to hold the cached calculation in the ‘parts’ table, but this smells a little rotten. It seems like there should be a cleaner way to cache the calculated values without stuffing them along side the ‘real’ columns.

  • 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. 2026-05-10T17:15:42+00:00Added an answer on May 10, 2026 at 5:15 pm
    1. You can stuff the actually cached values in the Rails cache (use memcached if you require that it be distributed).

    2. The tough bit is cache expiry, but cache expiry is uncommon, right? In that case, we can just loop over each of the parent objects in turn and zap its cache, too. I added some ActiveRecord magic to your class to make getting the parent objects simplicity itself — and you don’t even need to touch your database. Remember to call Part.sweep_complicated_cache(some_part) as appropriate in your code — you can put this in callbacks, etc, but I can’t add it for you because I don’t understand when complicated_calculation is changing.

      class Part < ActiveRecord::Base   has_many :sub_parts, :class_name => 'Part'   belongs_to :parent_part, :class_name => 'Part', :foreign_key => :part_id    @@MAX_PART_NESTING = 25 #pick any sanity-saving value    def complicated_calculation (...)     if cache.contains? [id, :complicated_calculation]       cache[ [id, :complicated_calculation] ]     else       cache[ [id, :complicated_calculation] ] = complicated_calculation_helper (...)     end   end    def complicated_calculation_helper     #your implementation goes here   end    def Part.sweep_complicated_cache(start_part)     level = 1  # keep track to prevent infinite loop in event there is a cycle in parts     current_part = self      cache[ [current_part.id, :complicated_calculation] ].delete     while ( (level <= 1 < @@MAX_PART_NESTING) && (current_part.parent_part)) {      current_part = current_part.parent_part)      cache[ [current_part.id, :complicated_calculation] ].delete     end   end end 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 60k
  • Answers 61k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer The best way is: var row = testElement .XPathSelectElements('sheetData/row[@r='2']') .FirstOrDefault();… May 11, 2026 at 9:35 am
  • added an answer HTMLParser has the functionality in the standard library. It is,… May 11, 2026 at 9:35 am
  • added an answer [quote]could do it manually, and horribly, by adding all such… May 11, 2026 at 9:35 am

Related Questions

I have a tree of active record objects, something like: class Part < ActiveRecord::Base
I have a tree representation of pages in a CMS application. I understand how
I have a large tree of Java Objects in my Desktop Application and am
I have a tree that consists of several objects, where each object has a
I have a set of tree objects with a depth somewhere in the 20s.
I have a sort of tree structure that represent a hierarchy of layers in
I have a table in SQL server that has the normal tree structure of
I have a couple of header files, which boil down to: tree.h: #include element.h
I have a tree structure in memory that I would like to render in
I have a tree in flex built from an XML document into an XMLlist

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.