I am trying to optimize some methods in my model so they don''t repeat CPU intensive algorithms every time I call the method in the same request/response cycle. Eg. ===============def invitations all_pgm_updates.find_all do |update| update.invited? end end ===============I want to do something like: ===============def invitations if @invitations.nil? @invitations = all_pgm_updates.find_all do |update| update.invited? end end @invitations end ===============but isn''t the instance variable, @invitations, going to stick around as long as the record is stored in the session? How do I clear it at the beginning of every request? Models don''t have access to flash, I don''t think, so I need an alternative. -John -- John Smilanick Computing Staff - Webmaster Kavli Institute for Theoretical Physics University of California, Santa Barbara jsmilani@kitp.ucsb.edu (805) 893-6307 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060317/c3795433/attachment.html
try this: def invitations if @@invitations.nil? @@invitations = all_pgm_updates.find_all do |update| update.invited? end end @@invitations end On 3/17/06, John Smilanick <jsmilani@kitp.ucsb.edu> wrote:> I am trying to optimize some methods in my model so they don''t repeat CPU > intensive algorithms every time I call the method in the same > request/response cycle. > Eg. > ===============> def invitations > all_pgm_updates.find_all do |update| > update.invited? > end > end > ===============> I want to do something like: > ===============> def invitations > if @invitations.nil? > @invitations = all_pgm_updates.find_all do |update| > update.invited? > end > end > @invitations > end > ===============> but isn''t the instance variable, @invitations, going to stick around as long > as the record is stored in the session? How do I clear it at the beginning > of every request? Models don''t have access to flash, I don''t think, so I > need an alternative. > > > > > > -John > -- > John Smilanick > Computing Staff - Webmaster > Kavli Institute for Theoretical Physics > University of California, Santa Barbara > jsmilani@kitp.ucsb.edu > (805) 893-6307 > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- _________ Noel R. Morais
Hi John, So, you''re storing the model instance in the session right? And it''s in that model that you want to cache the expensive data, but you don''t want the cache to persist between HTTP requests? The solution is pretty simple: don''t store the model instance in the session. Store the model''s ID instead. in controller: before_filter :get_my_model, :only => [:some_action] def get_my_model if session[:mymodel_id] @mymodel = MyModel.find(session[:mymodel_id]) end end def some_action @mymodel.invitations #builds the model''s @invitations cache @mymodel.invitations #uses the cached @invitations end Hope that helps, Trevor -- Trevor Squires http://somethinglearned.com On 17-Mar-06, at 10:27 AM, John Smilanick wrote:> I am trying to optimize some methods in my model so they don''t > repeat CPU intensive algorithms every time I call the method in the > same request/response cycle. > Eg. > ===============> def invitations > all_pgm_updates.find_all do |update| > update.invited? > end > end > ===============> I want to do something like: > ===============> def invitations > if @invitations.nil? > @invitations = all_pgm_updates.find_all do |update| > update.invited? > end > end > @invitations > end > ===============> but isn''t the instance variable, @invitations, going to stick > around as long as the record is stored in the session? How do I > clear it at the beginning of every request? Models don''t have > access to flash, I don''t think, so I need an alternative. > > -John > > -- > John Smilanick > Computing Staff - Webmaster > Kavli Institute for Theoretical Physics > University of California, Santa Barbara > jsmilani@kitp.ucsb.edu > (805) 893-6307 > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060317/080f5e10/attachment.html