How can I cache a calculated column in rails?

前端 未结 5 1038
感动是毒
感动是毒 2020-12-14 03:06

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

class Part < ActiveRecord::Base
  has_many :sub_parts, :class_name => \"Part\"

  def complicat         


        
5条回答
  •  爱一瞬间的悲伤
    2020-12-14 03:41

    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
      

提交回复
热议问题